【问题标题】:Identify BLE device that is notifying识别正在通知的 BLE 设备
【发布时间】:2014-12-31 15:29:49
【问题描述】:

当电池状态发生变化时,我连接了多个 BLE 设备,每个“通知”。

在收到状态更改通知后,如何识别读取数据时通知的是哪个 BLE?

// Instance method to get the battery state
- (void) getBattery:(CBCharacteristic *)characteristic
{
    //----------------
    NSLog(@"getBattery");
    //----------------
    {
        // Get the Battery Data
        NSLog(@"Battery Level is : %@ (HEX)" , characteristic.value);
    }
}

这是收到通知时读取数据的代码...

感谢您的回答,但我似乎只接收来自上次服务设置的数据以进行通知。这是Notify的调用代码...

嗨,太好了。当数据发生变化时,我可以识别通知设备。

但是,我的代码似乎只是从最后识别的特征中通知和收集数据。

这里是调用代码:

- (void)peripheral:(CBPeripheral *)peripheral    didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{

// Retrieve the characteristic value for battery
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:SR1_DEVICE_BATTERY_LEVEL_SERVICE_UUID]])    {
[self getBattery:characteristic];
}
}

【问题讨论】:

    标签: objective-c cocoa bluetooth-lowenergy


    【解决方案1】:

    当外围设备发送值已更改的通知时,将调用 CBPeripheralDelegate 方法 didUpdateValueForCharacteristic。此方法包括对CBPeripheral 的引用。你没有展示你如何从这个方法到你的getBattery 方法,但是当你这样做时你需要传递外围设备。

    也可以引用CBCharacteristic的service属性的外围属性——

     CBPeripheral *p=characteristic.service.peripheral;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-10
      • 1970-01-01
      • 1970-01-01
      • 2018-11-24
      • 1970-01-01
      相关资源
      最近更新 更多