【发布时间】:2018-04-23 21:57:04
【问题描述】:
我尝试写入 ble 特征。但返回错误是“不允许写入”。
这是为了发现特征。 // 当您发现指定服务的特征时调用。
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
if ([service.UUID isEqual:[CBUUID UUIDWithString:DEVICE_SERVICE_UUID]]) {
for (CBCharacteristic *aChar in service.characteristics)
{
if ([aChar.UUID isEqual:dev2appcharacteristicsUUID]) {
[devicePeripheral setNotifyValue:YES forCharacteristic:aChar];
NSLog(@"Found R-Motion device with characteristics: %@\n", aChar);
dev2appCharacteristic = aChar;
}
if ([aChar.UUID isEqual:app2devcharacteristicsUUID]) {
//[devicePeripheral setNotifyValue:NO forCharacteristic:aChar];
NSLog(@"Found R-Motion device with characteristics: %@\n", aChar);
app2devCharacteristic = aChar;
}
}
}
}
我可以正确发现。并尝试写入 app2devCharacteristic。
它有_properties unsigned long long 4 和_isNotifying bool false。
我尝试将字节数组写为
- (IBAction)RequestResult:(id)sender {
unsigned char bytes[] = {0x00, 0xCC, 0xFF};//{0xFF, 0xCC, 0x00};//
NSData *transactData = [NSData dataWithBytes:bytes length:3];
[devicePeripheral writeValue:transactData forCharacteristic:app2devCharacteristic type:CBCharacteristicWriteWithResponse];
}
我已经返回didWriteValueForCharacteristic,但出现错误"Writing is not permitted"
如果我改成无响应为
- (IBAction)RequestResult:(id)sender {
unsigned char bytes[] = {0x00, 0xCC, 0xFF};//{0xFF, 0xCC, 0x00};//
NSData *transactData = [NSData dataWithBytes:bytes length:3];
[devicePeripheral writeValue:transactData forCharacteristic:app2devCharacteristic type:CBCharacteristicWriteWithoutResponse];
}
没有返回错误。但是我的外围设备没有收到任何东西。
【问题讨论】:
-
您说
_properties unsigned long long 4代表app2devCharacteristic。好吧,CBCharacteristicPropertyWriteWithoutResponse = 0x04,所以你显然不能在writeValue:forCharacteristic:type:的type中做CBCharacteristicWriteWithResponse。剩下的(没有写),你怎么知道的?transactData有效吗?你的设备真的会这样吗?
标签: ios iphone bluetooth-lowenergy core-bluetooth ios-bluetooth