【发布时间】:2017-08-09 13:55:08
【问题描述】:
我正在尝试使用代码 sn-p 写入数据。
- (void)peripheral:(CBPeripheral *)peripheral1 didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
// Again, we loop through the array, just in case.
for (CBCharacteristic *characteristic in service.characteristics) {
// And check if it's the right one
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]]) {
NSString *payloadMessage = @"3N";
NSData *payload = [payloadMessage dataUsingEncoding:NSUTF8StringEncoding];
[_discoveredPeripheral discoverDescriptorsForCharacteristic:characteristic];
[_discoveredPeripheral writeValue:payload forCharacteristic:characteristic
type:CBCharacteristicWriteWithResponse];
[_discoveredPeripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}
}
但出现错误
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error
{
}
作为:
Error Domain=CBATTErrorDomain Code=3 "Writing is not permitted." UserInfo={NSLocalizedDescription=Writing is not permitted.}
虽然同样适用于 android。
【问题讨论】:
-
那个
characteristic的properties是什么? -
-
0x16,也就是 0x10+0x2+0x04,不是吗?然后是
CBCharacteristicPropertyRead = 0x02, CBCharacteristicPropertyWriteWithoutResponse = 0x04, CBCharacteristicPropertyNotify = 0x10,所以你不能做CBCharacteristicWriteWithResponse(不包括CBCharacteristicPropertyWrite = 0x08,),你需要用CBCharacteristicWriteWithoutResponse来做。
标签: android ios xcode bluetooth bluetooth-lowenergy