0

I'm trying to only print a list of bluetooth devices with their RSSI level.

But if I use some React setState() function inside the listener of the react-native-ble-plx startDeviceScan() I'm facing a warning Excessive number of pending callbacks 501 ...

const [devices, setDevices] = useState([]);

const startScan = () => {
    manager.startDeviceScan(null, null, (error, device) => {

      // Handling error
      if (error) {
        console.error('Error when scanning : ', error);
        if (error.errorCode === BleErrorCode.LocationServicesDisabled) {
          console.log('Please activate localisation');
        }
        if (error.errorCode === BleErrorCode.BluetoothUnauthorized) {
          console.log('Permission are missing... BLE and/or Localisation ?');
        }
        return;
      }

    const index = devices.findIndex((d) => d?.id === device?.id);
    if (index === -1) {
      // Adding device in the list
      setDevices((old) => [...old, device]);
    } else {
      // Update device informations
      const old = [...devices];
      old.splice(index, 1, device);
      setDevices(old);
    }
  });
};


return (
        <ScrollView persistentScrollbar>
          <View>
              {devices.map((d, i) => (
                <Button
                  key={i}
                  onPress={() => connectTo(d)}
                >
                  {d?.id} - {d?.localName || d?.name || '???'} - {d?.rssi}
                </Button>
              ))}
            </View>
        </ScrollView>
)

How am I supposed to keep a list up to date ? ("Reactly" correct)

Seba99
  • 1,197
  • 14
  • 38

0 Answers0