识别正在通知的 BLE 设备

Posted

技术标签:

【中文标题】识别正在通知的 BLE 设备【英文标题】:Identify BLE device that is notifying 【发布时间】: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];


【问题讨论】:

【参考方案1】:

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

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

 CBPeripheral *p=characteristic.service.peripheral;

【讨论】:

以上是关于识别正在通知的 BLE 设备的主要内容,如果未能解决你的问题,请参考以下文章

Android BLE 通知

Android BLE GATT 外设模式通知

通过 BLE 通知接收数据包

核心蓝牙框架中的 ANCS 通知

BLE Android,不能启用超过 1 个关于读取特性的通知

ANCS 是不是支持向多个 BLE 设备发送通知?