简体   繁体   中英

iOS reading data from BLE device

I'm trying to read data from a BLE device but keep getting permission error. Demo project can be found here: https://github.com/sergiomtzlosa/CoreBluetooth-Demo (keep in mind - my codes are a bit different than this one).

No problem with connection and reading value in general but there are some characteristics (which are essential) give permission error.

Console log: Update error!!! Characteristic: "Unknown (< fff4 >)" with error: "Reading is not permitted".

So, when I subscribe or read data from that characteristic, it sends me NULL everytime (probable reason: no read permission).

Console log: Characteristic: "Unknown (< fff4 >)" -> with value: (null)

Here is a code segment:

//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's wrong? Is there anyway to overcome this problem?

Is the peripheral an embedded device or is it another iOS device. I have seen the where the characteristic of the peripheral has to be explicitly configured to have read permission. Just because you have discovered a characteristic does not mean you can read it.

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

"Characteristic Value Permissions Values representing the read, write, and encryption permissions for a characteristic's value.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM