Reading iOS data from a BLE device

I try to read data from a BLE device, but keep getting a permission error. The demo project can be found here: https://github.com/sergiomtzlosa/CoreBluetooth-Demo (keep in mind - my codes are slightly different from this).

There are no problems with the connection and the meaning of reading in general, but there are some characteristics (which are significant) that give a permission error.

Console log: update error !!! Feature: "Unknown (<fff4>)" with an error: "Reading is not allowed."

So, when I subscribe or read data from this sign, it sends me NULL every time (likely reason: lack of read permission).

Console Log: Feature: "Unknown (<fff4>)" β†’ with value: (Zero)

Here is the code snippet:

//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); } } 

What happened? Is there any way to overcome this problem?

+4
source share
2 answers

Is the peripheral device a built-in device or is it another iOS device. I saw where the characteristics of the peripheral device must be explicitly configured for read permission. Just because you have found a characteristic does not mean that you can read it.

0
source

You should check if you have permission to access the value, permission is set when CBCharacteristic starts

"Characteristic Permissions Values ​​representing read, write, and encryption permissions for characteristic values.

 typedef enum { CBAttributePermissionsReadable = 0x01, CBAttributePermissionsWriteable = 0x02, CBAttributePermissionsReadEncryptionRequired = 0x04, CBAttributePermissionsWriteEncryptionRequired = 0x08, } CBAttributePermissions;" 

https://developer.apple.com/library/ios/documentation/CoreBluetooth/Reference/CBMutableCharacteristic_Class/Reference/CBMutableCharacteristic.html

0
source

Source: https://habr.com/ru/post/1497810/


All Articles