react-native-ble-plx read stops working after setting state

30 Views Asked by At

I am connecting to a custom device where I am able to read only in the initial connection .then sequence and only if there is nothing else (other than stopDeviceScan) called. Here is the code:

  const connectToDevice = (deviceId: string) => {
    bleManager
      .connectToDevice(deviceId)
      .then(device => {
        bleManager.stopDeviceScan();
        console.log('Connected to device:', device.id);

        console.log('Calling device.discoverAllServicesAndCharacteristics()');
        return device.discoverAllServicesAndCharacteristics();
      })
      .then(async device => {
        const OwnerId = await bleGetOwnerId(device);
        console.log('OwnerId:', OwnerId);

        setConnectedDeviceId(device.id);

        const OwnerId2 = await bleGetOwnerId(device);
        console.log('OwnerId:', OwnerId);
      })
      .catch(error => {
        console.error('Connection error:', error);
      });
  };

It is able to read the OwnerId the first time: OwnerId: ABCDEFGHIJ but then I get: ERROR Connection error: [BleError: Device BC0DDECD-B0A9-4E14-3FBD-BF2A5EB30445 is not connected]

This is on an iOS device and has no issues reading/writing using LightBlue or nRF connect. What am I missing?

0

There are 0 best solutions below