0

I'm developing an application which scans for BLE devices(Peripherals), then connect to them, read their services and characteristics. For now, I was able to connect to an iPhone which acting as a Peripheral device with certain characteristics and also was able to connect to Fitbit Versa Watch to read the heart sensor data. Although I was able to discover their services, I wasn't able to extract more information from the services like ServiceUUId and their characteristics.

Below is my code is written in react native.

scanAndConnect() {
console.log("Scanning Started");
this.manager.startDeviceScan(null, null, (error, device) => {
  if (error) {
    // Handle error (scanning will be stopped automatically)
    console.log("Error in scanning devices:", error);
    return
  }
  // Check if it is a device you are looking for based on advertisement data
  // or other criteria.
  console.log("Detected Device Details:", device.id, device.name);
  // ||device.localName === 'BLEPeripheralApp') 
  if (device.name === 'Versa Lite'){ //
    // Stop scanning as it's not necessary if you are scanning for one device.
    console.log("Device Found, Stopping the Scan.");
    console.log("Connecting to:",device.name)
    this.manager.stopDeviceScan();
    device.connect()
      .then((device) => {
        // this.info("Discovering services and characteristics")
        console.log("Connected...Discovering services and characteristics");
        return device.discoverAllServicesAndCharacteristics()
      })
      .then((device) => {
        console.log('Services and characteristics discovered');
        //return this.testChar(device)
        const services = device.services()
        console.log(services);
        return device.readCharacteristicForService(services)
        // device.readCharacteristicForService("abbaff00-e56a-484c-b832-8b17cf6cbfe8")
        // this.info("Setting notifications")
        //return this.setupNotifications(device)
      })
      .then(() => {
        const characteristicsData =  device.readCharacteristicForService();
        console.log(characteristicsData);
        //this.info("Listening...")
      }, (error) => {
        console.warn(error.message);
        // this.error(error.message)
      })
    }
  });
 }

How do I extract the serviceUUId from the Service method and also read the characteristics of that service? For my iPhone peripheral, I have two mutable characteristics that I should be able to read and write. How do I read them from service to characteristics to the real value?

Any help/suggestions are much appreciated.

Thanks.

Madhu
  • 869
  • 1
  • 17
  • 37

1 Answers1

2

You're missing the method characteristicsForService.

Loop over all the services, get their characteristics by

await device.discoverAllServicesAndCharacteristics();
const services = await device.services();
services.forEach(async service => {
   const characteristics = await device.characteristicsForService(service.uuid);
   characteristics.forEach(console.log);
});
Adam Jenkins
  • 51,445
  • 11
  • 72
  • 100