【问题标题】:iOS reading data from BLE deviceiOS 从 BLE 设备读取数据
【发布时间】:2013-08-21 09:08:50
【问题描述】:

我正在尝试从 BLE 设备读取数据,但不断收到权限错误。演示项目可以在这里找到:https://github.com/sergiomtzlosa/CoreBluetooth-Demo(请记住 - 我的代码与这个有点不同)。

连接和读取值一般没有问题,但有一些特征(这是必不可少的)给予权限错误。

控制台日志:更新错误!!!特征:“未知()” 出现错误:“不允许读取”。

因此,当我从该特征订阅或读取数据时,它每次都会向我发送 NULL(可能的原因:没有读取权限)。

控制台日志:特征:“未知 ()”-> 具有值: (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);
}
}

怎么了?有没有办法克服这个问题?

【问题讨论】:

  • 你确定这些是可读的特征吗?从外设端向我们展示特征初始化代码。
  • 该特性似乎不允许阅读。也许要先配对?
  • 已经配对。我没有外设端的驱动代码,还想去拿。

标签: ios bluetooth core-bluetooth


【解决方案1】:

外围设备是嵌入式设备还是另一个 iOS 设备。 我已经看到必须将外围设备的特征显式配置为具有读取权限的地方。仅仅因为你发现了一个特征并不意味着你可以阅读它。

【讨论】:

    【解决方案2】:

    您必须检查您是否有访问该值的权限,该权限是在您初始化 CBCharacteristic 时设置的

    "特征值权限 表示特征值的读取、写入和加密权限的值。

    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

    【讨论】:

      猜你喜欢
      • 2013-11-24
      • 1970-01-01
      • 2021-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多