[iOS] Scan - BleError: BluetoothLE is in unknown state

See original GitHub issue

Prerequisites

  • I am running the latest version
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed

Expected Behavior

When the BleState is PoweredOn, I should be able to startScanDevices() without having BleError: BluetoothLE is in unknown state. I should be able to scan devices with and without “Remote Debug” enabled.

Current Behavior

When I start scanning my devices, I receive the BleError: BluetoothLE is in unknown state even if I waited that the BleManager State was in ‘PoweredOn’. This behavior is only present when I enable the “Remote Debug” on iOS. It’s working fine if I’m disable the “Remote Debug”. It’s really annoying because everybody is developing with the Remote Debug active I guess… Otherwise it’s a bit complicated.

I don’t see anything about this issue in the documentation. It’s just written that we have to wait that the status is PoweredOnon iOS. And that’s done… I found that it’s not working with the “Remote Debug” active in this issue. I don’t understand why there is no related issue or documentation about that…

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. Run your app
  2. Activate Remote Debug
  3. Wait until State is PoweredOn
  4. Call startDevicesScan()
  5. See error…

Context

  • Library version: 2.0.1
  • Platform: iOS.
  • JS logs:
DeviceScanner.js:17 LIB : Wait until bluetooth ready...
DeviceScanner.js:28 Base state PoweredOn
DeviceScanner.js:57 LIB : Start scan...
DeviceScanner.js:70 Scan error :  {"message":"BluetoothLE is in unknown state","errorCode":103,"attErrorCode":null,"iosErrorCode":null,"androidErrorCode":null,"reason":null,"name":"BleError"}
DeviceScanner.js:85 LIB : Scan stopped
DeviceScanner.js:22 State change :  PoweredOn
  • Formatted code sample or link to a repository:

App.js


 startScan() {
    _deviceScanner.startDevicesScan(
      error => {
        console.error(error);
        _deviceScanner.stopDevicesScan();
      },
      device => {
        console.log(device);
      }
    );
  };

DeviceScanner.js

  /**
   * Wait until Bluetooth is ready. On iOS the BLE stack is not immediately ready,
   * we need to wait for the "PoweredOn" status.
   */
  async waitUntilBluetoothReady() {
    let {PoweredOn, PoweredOff, Unauthorized, Unsupported} = State;
    console.log('LIB : Wait until bluetooth ready...');
    return new Promise(async (resolve, reject) => {
      this._bleManager.onStateChange(state => {
        console.log('State changed : ', state);
        if (state === PoweredOn) {
          resolve(true);
        }
      });

     // Verify if the state is already poweredOn before the listener was created
      let crtState = await this._bleManager.state();
      console.log('Base state', crtState);
      if (crtState === PoweredOn) {
        resolve(true);
      } 
      else if (
        crtState === PoweredOff ||
        crtState === Unauthorized ||
        crtState === Unsupported
      ) {
        reject(new Error('Bluetooth is not available :', crtState));
      }
    });
  }

  /** 
   * Start device scanning only if the BleManager State is ready 
   */
  async startDevicesScan(
    onError: (error: Error) => void,
    onDeviceFound: (device: ConnectHubDevice) => void,
  ) {
    this.waitUntilBluetoothReady()
      .then(() => {
        console.log('LIB : Start scan...');
        const scanOptions = {
          allowDuplicates: false,
          scanMode: ScanMode.LowLatency,
        };
        this._bleManager.startDeviceScan(
          [SERVICE_UUID],
          scanOptions,
          (error, device) => {
            if (error) {
              onError(error);
            }
            if (device) {
              onDeviceFound(device);
            }
          },
        );
      })
      .catch(error => console.log(JSON.stringify(error));
  }

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:15

github_iconTop GitHub Comments

6reactions
purrsong-kmcommented, Oct 28, 2020

Is there any progress??

4reactions
dariuszseweryncommented, Dec 29, 2020

It is important to keep only a single instance of BleManager. Readme was recently updated to stress this out.

Read more comments on GitHub >

github_iconTop Results From Across the Web

IOS :-scanning Error - BluetoothLE is in unknown state · Issue ...
Here is my code : startDeviceScan(scanTimeout) { var me = this; // first destroy BLE manager object and re-initialize it
Read more >
react native ble plx - Bluetooth is not supported in ios
When I start device scan by calling the method startDevcieScan(), most of the time "bluetooth is not supported" alert comes.
Read more >
react-native-ble-plx 2.0.2 | Documentation
When iOS application launches BLE stack is not immediately available and we need to check its status ... Once device is scanned it...
Read more >
RxBLELibraries/react-native-ble - Gitter
”BluetoothLE is in unknown state”? ... fail because there is no connected device. restarting the app re-establishes the connection via the startDeviceScan.
Read more >
[iOS] Scan - BleError: BluetoothLE is in unknown state
When the BleState is PoweredOn , I should be able to startScanDevices() without having BleError: BluetoothLE is in unknown state .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found