React-Native-BLE-plx : how to get all scanned device list of advertising ble devices using ble-plx or BLE-Manager

2.4k views Asked by At

im making a ble devices scanning app and im facing 2 different problems with these two libraries.

  1. using ble-plx: it is only returning a single device object after scanning not a list of device.

.

with ble-plx i tried this code.

bleManager.startDeviceScan(
      [],
      {allowDuplicates: false},
      (error, device) => {
        if (error) {
          console.log('ERRO: ', error);
          return;
        } else {
          deviceList.push(device);
          console.log(deviceList);
           settotalDevices(deviceList.map(item => item.id));
          console.log(deviceList.map(item => item.id));
          // if (device.localName || device.name === 'POCO M3') {
          //   device
          //     ?.connect()
          //     .then(() => {
          //       console.log('connected to', device?.name);

          //       return device?.discoverAllServicesAndCharacteristics;
          // })
          // .catch(err => console.log('Error: ', err));
        }

        bleManager.stopDeviceScan();

I trid to map the returned object in a list hopeing that it will store all the device objects during scan but it only recives 1 device in the object.Although if i give a device name and perform connect it gets that specific device and connects to it but i need a list of all devices.

with BLE-Manager I tried: to check whether stopScan is not called too early I Placed it in a setTimeout but the getDiscoveredPeripherals() consoles its output before the stop scan is called because of the setTimeout. and when I click on scan button consecutively it scans and returns number of discovered peripherals 0 or 1 or 2 or 3. here is the code.

await BleManager.start({showAlert: false, forceLegacy: true}).then(() => {
      // Success code
      console.log('Module initialized');
    });

    await BleManager.scan([], 10, true, {
      matchMode: 1,
      scanMode: 2,
    })
      .then(Results => {
        // Success code
        console.log('Scan started');
      })
      .catch(error => console.log(error));
    setTimeout(() => {
      BleManager.stopScan().then(() => {
        console.log('Scan stopped');
      });
    }, 11000);
    await BleManager.getDiscoveredPeripherals().then(peripheralsArray => {
      // Success code
      settotalDevices(peripheralsArray.length);
      console.log('Discovered peripherals: ' + peripheralsArray.length);
      console.log(peripheralsArray);
      deviceList = peripheralsArray.map(item => item.id);
      console.log(deviceList);
    });

i think the scan callback is being instantiated multiple times as the scan is not completed when device list is returned.

I would appriciate if anyone can solve my problem using any of the two libraries because it is very hard to find an implementation example for this in a react native functional commponent.

1

There are 1 answers

0
Khizer Hayat On

It was a logical error the getDiscoveredPeripherals() method was placed outside the stopScan() method. The documentation is not that well written for ble libs so it was by chance that i tried to put my discoveredperipherals method inside the stop method so it returned results after the execution of stopScan(). and i was tring to do it via async await but async await just executes a method whenever it finishes executing it. sometime the slightest mistake can cause chaotic results. hope someone in need finds this cause I didn't and it cost time.