我编写了一个应用程序,试图获取并更新我的 cB-OLP425 模块上的电池电量。
我使用了以下代码,但有时它给我的值是 70,有时它给我的值是 -104。我查看了很多帖子并尝试了很多东西,但似乎没有任何效果。
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
if([service.UUID isEqual:[CBUUID UUIDWithString:@"180F"]]) {
for (CBCharacteristic *characteristic in service.characteristics) {
NSLog(@"discovered service %@", service.UUID);
if([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A19"]]) {
NSLog(@"Found Notify Characteristic %@", characteristic.UUID);
self.mycharacteristic = characteristic;
[self.testPeripheral readValueForCharacteristic:mycharacteristic];
[self.testPeripheral setNotifyValue:YES forCharacteristic:mycharacteristic];
char batlevel;
[mycharacteristic.value getBytes:& batlevel length:0];
int n = (float)batlevel;
int value = n ;
self.batteryLevel = value;
NSLog(@"batterylevel1;%f",batteryLevel);
/* [[NSUserDefaults standardUserDefaults]setFloat: batteryLevel forKey:@"battery_level"];*/
}
} }
}
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
if([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A19"]]) {
NSLog(@"Found Battery Characteristic %@", characteristic.UUID);
[mycharacteristic.value getBytes:& batteryLevel length:0];
return;
}
[self.delegate peripheralDidReadChracteristic:mycharacteristic withPeripheral:testPeripheral withError:error];
}
有人可以帮我吗!