简体   繁体   English

iOS从BLE设备读取数据

[英]iOS reading data from BLE device

I'm trying to read data from a BLE device but keep getting permission error.我正在尝试从 BLE 设备读取数据,但不断收到权限错误。 Demo project can be found here: https://github.com/sergiomtzlosa/CoreBluetooth-Demo (keep in mind - my codes are a bit different than this one).演示项目可以在这里找到: https : //github.com/sergiomtzlosa/CoreBluetooth-Demo (请记住 - 我的代码与这个有点不同)。

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".特征:“未知 (< fff4 >)”,错误: “不允许读取”。

So, when I subscribe or read data from that characteristic, it sends me NULL everytime (probable reason: no read permission).因此,当我从该特征订阅或读取数据时,它每次都会向我发送 NULL(可能的原因:没有读取权限)。

Console log: Characteristic: "Unknown (< fff4 >)" -> with value: (null)控制台日志:特征:“未知(< fff4 >)”-> 值:(空)

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.外围设备是嵌入式设备还是另一个 iOS 设备。 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您必须检查您是否有权访问该值,该权限是在您初始化 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 https://developer.apple.com/library/ios/documentation/CoreBluetooth/Reference/CBMutableCharacteristic_Class/Reference/CBMutableCharacteristic.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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