我正在尝试从 BLE 设备读取数据,但不断收到权限错误。演示项目可以在这里找到:https ://github.com/sergiomtzlosa/CoreBluetooth-Demo (请记住 - 我的代码与这个有点不同)。
一般连接和读取值没有问题,但有一些特征(这是必不可少的)给出权限错误。
控制台日志:更新错误!!!特征:“未知 (< fff4 >)”,错误:“不允许读取”。
因此,当我从该特征订阅或读取数据时,它每次都会向我发送 NULL(可能的原因:没有读取权限)。
控制台日志:特征:“未知(< fff4 >)”-> 带有值:(null)
这是一个代码段:
//Action on discovering services
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
if (error)
{
NSLog(@"Discovered services for %@ with error: %@", peripheral.name, [error localizedDescription]);
return;
}
for (CBService *service in peripheral.services) {
NSLog(@"Discovereddddddddddd service %@", service.UUID);
[testPeripheral discoverCharacteristics:nil forService:service];
}
NSLog(@"didDiscoverServicesEnd");
}
//Action on discovered characteristics
- (void)peripheral:(CBPeripheral *)peripheral
didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
NSLog(@"didDiscoverCharacteristicsForService!");
for (CBCharacteristic *characteristic in service.characteristics) {
NSLog(@"Discovered characteristic %@", characteristic.UUID);
NSLog(@"---------------------------------------------------");
NSLog(@"Reading value for characteristic %@", characteristic.UUID);
[peripheral readValueForCharacteristic:characteristic];
NSLog(@"+++++++++++++++++++++++++++++++++++++++++++++++++++");
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}
//Action on reading value
- (void)peripheral:(CBPeripheral *)peripheral
didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
if (error){
NSLog(@"Update error!!! Characteristic: %@ with error: %@", characteristic.UUID, [error localizedDescription]);
return;
}else{
NSData *data = characteristic.value;
NSString *str = [NSString stringWithUTF8String:[data bytes]];
NSLog(@"Characteristic: %@ -> with value: %@", characteristic.UUID, str);
}
}
怎么了?有没有办法克服这个问题?