How to read information from core bluetooth device

2020-02-28 03:29发布

问题:

I am working on an iOS core Bluetooth application, I am able to connect with the BLE device using iPad3. I am able to reach to the block didDiscoverServices, but unable to proceed from here.

My questions are ;

  1. How can I read characteristic from Bluetooth device?
  2. How can I read other information of Bluetooth device?

Help me on this or provide any suggestion.

Thanks Wilhelmsen for reply.

I got the following from the mentioned block :

[0] - Service : <CBConcreteService: 0x1769a0> UUID: Generic Attribute Profile
[1] - Service : <CBConcreteService: 0x174470> UUID: Generic Access Profile
[2] - Service : <CBConcreteService: 0x1744e0> UUID: Unknown (<00005301 00000041 4c505749 53450000>)

Characteristic

[0] - Characteristic : <CBConcreteCharacteristic: 0x15d410> UUID: Service Changed

[0] - Characteristic : <CBConcreteCharacteristic: 0x1805b0> UUID: Device Name
[1] - Characteristic : <CBConcreteCharacteristic: 0x1806a0> UUID: Appearence

[0] - Characteristic : <CBConcreteCharacteristic: 0x183810> UUID: Unknown (<00004301 00000041 4c505749 53450000>)
[1] - Characteristic : <CBConcreteCharacteristic: 0x1838a0> UUID: Unknown (<00004302 00000041 4c505749 53450000>)

Now how to get the exact values from this Characteristic in didUpdateValueForCharacteristic block?

回答1:

Take a nice good read through the framework. if you have come this far you shouldn't have any problem finding 'discoverCharacteristics' and the peripheral delegate callback 'didDiscoverCharacteristic'. You need to know the UUID of the services and characteristics you want to discover and apply it to those methods.

Then you can read with 'readValueForCharacteristic' and the delegate callback 'didUpdateValueForCharacteristic'.

This is sent from my phone, so I will maybe edit a bit when I get to a computer. Hope it helps

New question:

[connectedPeripheral readValueForCharacteristic:wantedCharacteristic] 

and at peripheral delegate

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

NSLog(@"Characteristic value : %@ with ID %@", characteristic.value, characteristic.UUID);
[delegate characteristicValueRead:characteristic.value];
}

works for me



回答2:

You can get value of a characteristic

 NSString *value = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];

NSLog(@"Value %@",value);


回答3:

Just use https://github.com/LGBluetooth/LGBluetooth

    [LGUtils readDataFromCharactUUID:@"f045"
                          seriveUUID:@"5ec0"
                          peripheral:peripheral
                          completion:^(NSData *data, NSError *error) {
                              NSLog(@"Data : %s Error : %@", (char *)[data bytes], error);
                          }];