这是一个与CoreBluetooth合作的项目。我使用我的iPhone 5c运行它。外设是单片机。iPhone可以接收来自单片机的数据。但是单片机不能通过[_peripheral writeValue:value forCharacteristic:_writeCharacteristic type:CBCharacteristicWriteWithResponse];接收来自iPhone的数据
以下是我的代码的一部分。
#define kServiceUUID @"FFE0"
#define kCharacteristicUUID @"FFE1"
#import "BTManager.h"
@interface BTManager()<CBCentralManagerDelegate, CBPeripheralDelegate>{
CBCentralManager *manager;
}
@property (strong,nonatomic) NSMutableArray *peripherals;
@property (nonatomic, strong) CBCharacteristic *writeCharacteristic;
@property (nonatomic, strong) CBPeripheral *peripheral;
@end
@implementation BTManager
#pragma mark - Public Methods
- (void)writeToPeripheral:(NSString *)dataString {
if(_writeCharacteristic == nil){
NSLog(@"writeCharacteristic is nil");
return;
}
NSData *value = [self dataWithHexstring:dataString];
NSLog(@"hex data:%@",value);
[_peripheral writeValue:value forCharacteristic:_writeCharacteristic type:CBCharacteristicWriteWithResponse];
NSLog(@"didWriteTo%@ characteristic:%@ data:%@",_peripheral.name,_writeCharacteristic.description,dataString);
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
if (error) {
NSLog(@"discoverCharacteristic:%@ Error:%@", service.UUID, [error localizedDescription]);
return;
}
NSLog(@"Service:%@",service.UUID);
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:kCharacteristicUUID]]) {
NSLog(@"setNotifyValuefor:%@",characteristic);
_writeCharacteristic = characteristic;
[self.peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}
}发布于 2015-05-18 22:08:38
我不认为你提供的源代码有任何问题。您尝试写入的特定外围设备是什么?
我想我会问的问题是
在peripheral: didWriteValueForCharacteristic: error:中接收任何类型的响应的characteristic?
如果你在上面提到的委托回调中没有看到响应,我会质疑外围设备是否正确地响应了写命令。
https://stackoverflow.com/questions/30271199
复制相似问题