读取Ble设备电池值

时间:2017-08-18 14:21:32

标签: ios bluetooth-lowenergy core-bluetooth batterylevel

我试图使用CoreBluetooth和GATT从蓝牙设备读取电池电量。我成功连接到设备,我也得到了正确的服务列表,如下面的调试所示:

didDiscoverServices:(
"<CBService: 0x170273440, isPrimary = YES, UUID = Device Information>",
"<CBService: 0x170273700, isPrimary = YES, UUID = 1803>",
"<CBService: 0x170273740, isPrimary = YES, UUID = 1802>",
"<CBService: 0x170273780, isPrimary = YES, UUID = 1804>",
"<CBService: 0x1702737c0, isPrimary = YES, UUID = Battery>",
"<CBService: 0x170273800, isPrimary = YES, UUID = Heart Rate>",
"<CBService: 0x170273840, isPrimary = YES, UUID = FFF0>",
"<CBService: 0x170273880, isPrimary = YES, UUID = FFF3>")

显然我们在这里想要的是电池&#34;电池&#34; UUID。它只有一项服务:

<CBCharacteristic: 0x1700bf500, UUID = Battery Level, properties = 0x12, value = <64>, notifying = NO>

所以我将这个特征保存到变量中以便稍后阅读。然后我打电话给#34; readValueForCharacteristic&#34;并等待&#34; didUpdateValueForCharacteristic&#34;给我带来更新。一切正常,直到那一点。 价值似乎是错误的。它只是1位,等于&lt; 64&gt;无论我做什么,它永远不会改变。 在做的时候我总是得到100%:

Byte *rsp = (Byte*)[value bytes];
NSString percent = [NSString stringWithFormat:@"%hhu",rsp[0]];

我错过了什么吗?

0 个答案:

没有答案