【问题标题】:BLE only getting Battery Level characteristic Value IOSBLE 仅获取 Battery Level 特征值 IOS
【发布时间】:2015-09-09 06:12:22
【问题描述】:

我正在尝试使用设备中的 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

问题是我不能 setNotifyValue :是的,除了 BatteryLevel 特性之外的其他特性,我也只收到相同的通知。

这是我的输出。 知道我在这里做错了什么吗? 谢谢

【问题讨论】:

  • 只有在特性确实允许的情况下,您才能获得特性通知。使用CBCharacteristicPropertyNotify 查看他们的属性。否则,您必须读取该值。换句话说:if (characteristic & CBCharacteristicPropertyNotify){//setNotifyValue}else {[peripheral readValueForCharacteristic:characteristic]}
  • @Larme 我确实编辑了代码并使用了 readValueForCharacteristic 函数,但 didUpdateValueForCharacteristic 仅针对 BatteryLevel 特性被调用!

标签: ios objective-c swift


【解决方案1】:

那是因为您在 gatt 文件(您的蓝牙项目)中的特征设置未正确设置。例如,查看另一个特征设置并更新您的 gatt 文件。 另一种获取特征值的方法是使用计时器来读取您的特征:

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

仅此而已。但实际上最好做 - 修改 gatt 文件(蓝牙项目)中的特征设置。

【讨论】:

    猜你喜欢
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 2016-07-26
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多