仅获取蓝牙低功耗设备的电量特征值(iOS)

3

我正在尝试使用CoreBluetooth框架从设备中读取所有可用服务及其特征值。

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral
        advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
        NSLog(@"Received peripheral : \n%@", peripheral);
        NSLog(@"Adv data : %@", advertisementData);

    self.activeperipheral=peripheral;

    [self.myCentralManager connectPeripheral:peripheral options:nil];

    if(peripheral.state==CBPeripheralStateConnected){
        peripheral.delegate=self;
        NSLog(@"Connected");

    }
    else
        NSLog(@"Not Connected");

    NSArray *serviceUUIDs=[advertisementData objectForKey:CBAdvertisementDataServiceUUIDsKey];
    for(CBUUID *foundserviceUUIDs in serviceUUIDs){

        if([serviceUUIDs containsObject:foundserviceUUIDs]){
            NSLog(@"%@",serviceUUIDs);
        }
    }

}

-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{

    for(CBService *service in peripheral.services){
    [peripheral discoverCharacteristics:nil forService:service];
        NSLog(@"Discover Service:%@",service);
    }
}

-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{

    for(CBCharacteristic *characteristic in service.characteristics){
        [peripheral setNotifyValue:YES forCharacteristic:characteristic];
        self.myCharacteristic=characteristic;
        NSLog(@"NotifyValue set on %@",characteristic);
        //[peripheral readValueForCharacteristic:myCharacteristic];

    }
}


-(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
if(characteristic.isNotifying)
    NSLog(@"Notification began on %@",characteristic);
}


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

    NSLog(@"%@",characteristic);
   // NSLog(@"Characteristic Value Updated");
}

- (int)scanForPeripherals {
    NSLog(@"Scanning");

    if(self.myCentralManager.state!=CBCentralManagerStatePoweredOn)
        NSLog(@"Turn on Bluetooth");
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey,
                             nil];
    NSLog(@"Scanning");
    [myCentralManager scanForPeripheralsWithServices:nil options:options];
    return 0;
}

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
    NSLog(@"Peripheral Connected");
    peripheral.delegate=self;
    //CBUUID *serviceUUID=[CBUUID UUIDWithString:@"1804"];
    [peripheral discoverServices:nil];
    //[myCentralManager stopScan];
}

- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
    NSLog(@"didDisconnectPeripheral");
}

- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
    NSLog(@"failed to connect");
}

- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error {
    NSLog(@"didReadRSSI");
}

@end

问题是我不能为除了BatteryLevel特征以外的其他特征设置setNotifyValue: Yes,并且我只收到相同特征的通知。

Output

这是我的输出结果。 你有什么想法,我在这里做错了什么吗? 谢谢


只有当特征确实允许时,您才能收到通知。使用 CBCharacteristicPropertyNotify 查看其属性。否则,您必须读取该值。换句话说:if (characteristic & CBCharacteristicPropertyNotify){//setNotifyValue}else {[peripheral readValueForCharacteristic:characteristic]} - Larme
@Larme 我已经编辑了代码并使用了readValueForCharacteristic函数,但是didUpdateValueForCharacteristic仅针对BatteryLevel特征被调用! - user2979190
2个回答

0

这是因为您在Gatt文件(蓝牙项目)中的特征设置没有正确设置。请查看其他特征设置的示例并更新您的Gatt文件。 另一种获取特征值的方法是使用定时器,在其中读取您的特征:

   -(void)TimerFunction
    {
      [talking_peripher readValueForCharacteristic:_talk_charack];
      NSlog("VALUE: %@" _talk_charack);
    }

就是这样。但实际上最好的做法是修改您在Gatt文件(蓝牙项目)中的特征设置。


0

除了您只能为支持“通知”属性的所有可用特征属性(请参阅https://developer.apple.com/documentation/corebluetooth/cbcharacteristicproperties)设置通知标志之外,没有错误。

当遇到时,您可以检查特定特征支持的属性。

optional func peripheral(
    _ peripheral: CBPeripheral,
    didDiscoverCharacteristicsFor service: CBService,
    error: Error?
)

然后通过迭代返回的service.characteristics属性(参见https://developer.apple.com/documentation/corebluetooth/cbservice)检查CBCharacteristic.properties值。
在您的代码中,当您循环进入characteristics时,应首先检查当前characteristic是否支持CBCharactirsticProperties.notify,然后再调用函数"setNotifyValue:YES"。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接