ios - BLE 只获取 Battery Level 特征值 IOS

标签 ios objective-c swift

我正在尝试使用 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 特性之外的其他特性是的,我也只收到相同的通知。

Output

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

最佳答案

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

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

仅此而已。但实际上最好的办法是修改 gatt 文件(蓝牙项目)中的特性设置。

关于ios - BLE 只获取 Battery Level 特征值 IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32472087/

相关文章:

iphone - 如何在 iphone 的 UITableView 中设置单元格的背景颜色?

Swift 使用 NotificationCenter 在 View Controller 之间传递数据

objective-c - 如何从 UIColor 获取反色?

ios - 将子图层的路径更改为另一路径时创建动画

ios - 同步打开 UIDocument

iphone - 将 ViewController 放在 TabBarController 之上

ios - 以编程方式在 UIScrollView 中居中 subview

Swift - 叠加层未显示在 AVCaptureVideoPreviewLayer 上

ios - UITableView reloadData之前的reloadSections

ios - 我在 Xcode7 上遇到字符串错误