我試圖從BLE設備讀取數據,但不斷收到權限錯誤。演示項目可以在這裏找到:https://github.com/sergiomtzlosa/CoreBluetooth-Demo(請記住 - 我的代碼有點不同於這個)。iOS從BLE設備讀取數據
連接和閱讀值沒有問題,但是有一些特性(這是必需的)會導致權限錯誤。
Console log: Update error!!! Characteristic: "Unknown (< fff4 >)" with error: "Reading is not permitted".
所以,當我訂閱或從特徵讀取數據時,它發出我每次NULL(可能的原因:沒有讀取權限)。
Console log: Characteristic: "Unknown (< fff4 >)" -> with value: (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);
}
}
有什麼不對?無論如何要克服這個問題?
你確定那些是可讀的特點呢?讓我們看看從外周側的特徵初始化代碼。 – allprog
該特徵似乎不允許閱讀。也許配對首先要做? – Larme
它已經配對。我沒有外圍設備的驅動代碼,但仍然試圖去獲取它們。 – snns