Sanjeev S
Sanjeev S

Reputation: 1163

Avoid Duplicate BLE devices using react-native-ble-plx

I am using react-native-ble-plx for implementing bluetooth in my app

The bluetooth scanning works fine but it contains duplicates in android whereas it works fine in iOS since allowDuplicates is default false in iOS.

In android the default is true. Please provide as solution to filter out the duplicates using the allowDuplicates property of react-native-ble-plx

Syntax for startDeviceScan from documentation:-

bleManager.startDeviceScan(
  UUIDs: ?Array<UUID>,
  options: ?ScanOptions,
  listener: (error: ?Error, scannedDevice: ?Device) => void
)

https://github.com/Polidea/react-native-ble-plx/wiki/Bluetooth-Scanning

My code:-

    this.manager.startDeviceScan(null, {allowDuplicates:false}, (error, device) => { 
//2nd parameter is scanOptions
        if (error) {
            // Handle error (scanning will be stopped automatically)
            return
        }
       this.state.count++
        if(this.state.count>10)
        {
          this.manager.stopDeviceScan();
        }
        console.log("id",device.id) 
}

please tell if any syntax error persists

Upvotes: 2

Views: 2134

Answers (2)

I use a list with the names of the founded devices and check for duplicates with the includes() method

  refreshScreen(device){
    if(!this.state.dataNames.includes(device.name)){
      let dataNow = this.state.data;
      dataNow.push(element);
      let names = this.state.dataNames;
      names.push(element.name);
      this.setState(
        {
          refreshing: false,
          data: dataNow,
          dataNames: names, 
        }
      );
    }
  }

this function adds devices that are not in the list dataNames

Upvotes: 0

Thomas Stubbe
Thomas Stubbe

Reputation: 2014

This setting is iOS only and does not prevent duplicates from being shown there either. You must use a set or equivalent to make sure only unique ones are displayed/used in your app

Upvotes: 1

Related Questions