【发布时间】:2015-04-19 02:36:09
【问题描述】:
我正在尝试开发 iOS 应用程序,该应用程序正在检测 BLE 设备并需要调用写入命令。我可以成功连接到 BLE 设备并发现它的服务和特性。但是在连接的外围设备上写入数据期间出现未知错误。下面是我写的代码:
发现的特征:
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
NSArray *characteristics = [service characteristics];
if (peripheral != self.peripheral) {
NSLog(@"Wrong Peripheral.\n");
return ;
}
if (error != nil) {
NSLog(@"Error %@\n", error);
return ;
}
for (CBCharacteristic *characteristic in characteristics) {
if ([[characteristic UUID] isEqual:RWT_POSITION_CHAR_UUID]) {
self.positionCharacteristic = characteristic;
}
}
}
这是BLE设备写入数据的函数
- (void)writeCommand{
// See if characteristic has been discovered before writing to it
if (!self.positionCharacteristic) {
return;
}
NSString *cmd1=@"CQ+WHORU\r";
NSData *dataToWrite = [cmd1 dataUsingEncoding:NSUTF8StringEncoding];
CBCharacteristic *chr = self.positionCharacteristic;
NSLog(@"%@,%lu...%@",chr.UUID,chr.properties,chr);
NSInteger dataSize = [[NSByteCountFormatter stringFromByteCount:dataToWrite.length countStyle:NSByteCountFormatterCountStyleFile] integerValue];
if (dataSize > 130) {
NSLog(@"Cannot send more than 130 bytes");
}
else
{
[self.peripheral writeValue:dataToWrite forCharacteristic:self.positionCharacteristic type:CBCharacteristicWriteWithResponse];
}
}
- (void)peripheral:(CBPeripheral *)peripheral
didWriteValueForCharacteristic:(CBCharacteristic *)characteristic
error:(NSError *)error
{
if (error)
{
NSLog(@"Error writing characteristic value: %@", [error localizedDescription]);
}
else
{
NSLog(@"Successfully writing characteristic value");
}
}
在“didWriteValueForCharacteristic”委托方法中,我遇到以下错误:
写入特征值时出错:未知错误。
这个错误的可能原因是什么?
另外,如果我尝试定义在 BLE 设备上发送数据的特征,例如:
CBMutableCharacteristic *writeChar = [[CBMutableCharacteristic alloc]initWithType:[CBUUID UUIDWithString:@"ffe1"] properties:CBCharacteristicPropertyWriteWithoutResponse|CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsWriteable|CBAttributePermissionsReadable];
int8_t cmd = *[cmd1 UTF8String];
NSData *data = [NSData dataWithBytes:&cmd length:sizeof(cmd)];
NSInteger dataSize = [[NSByteCountFormatter stringFromByteCount:dataToWrite.length countStyle:NSByteCountFormatterCountStyleFile] integerValue];
if (dataSize > 130) {
NSLog(@"Cannot send more than 130 bytes");
}
else
{
[self.peripheral writeValue:dataToWrite forCharacteristic:writeChar type:CBCharacteristicWriteWithResponse];
// [self.peripheral writeValue:dataToWrite forCharacteristic:writeChar type:CBCharacteristicWriteWithResponse];
}
然后我收到此错误:
**CoreBluetooth[WARNING] CBMutableCharacteristic: 0x1552a270 UUID = Unknown (<ffe1>),Value = (null), Properties = 0x14, Permissions = 0x3, Descriptors = (null), SubscribedCentrals = (
)> is not a valid characteristic for peripheral <CBPeripheral: 0x15549930 identifier = FC71890D-9F71-5E16-086D-D491E1EF7599, Name = " DEMOBLE1", state = connected**
【问题讨论】:
-
你检查过特性的属性,确认它支持writeWithResponse吗?
-
尝试使用特征写入选项 * CBCharacteristicWriteWithoutResponse*,可能是 BL 设备不支持 * CBCharacteristicWriteWithResponse*
-
是的,我尝试了 CBCharacteristicWriteWithoutResponse,但它不起作用。在 android 开发中,如果我发送上面的命令,那么它会在 updateValue 委托方法中回复一些值,但我没有得到任何值。这意味着写入功能不起作用。
标签: ios objective-c iphone bluetooth-lowenergy core-bluetooth