React Native: 'android.bluetooth.bluetoothadapter.isEnabled()' on a null reference

952 Views Asked by At

I am working on a React-Native app that relies on Bluetooth for some of its functionality and as far as I understand, Android Studio's emulator does not support Bluetooth. The package I am using is react-native-eddystone which is used to work with Bluetooth low energy devices. When I start the app on my phone it prompts me to allow the usage of Bluetooth and works fine but when I use the emulator it crashes from the start. Here is the AndroidManifets.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application android:usesCleartextTraffic="true" tools:targetApi="28" tools:ignore="GoogleAppIndexingWarning" />
</manifest>

And here is a screenshot of the error:

enter image description here

Edit: The error occurs here in EddystoneModule.java:

  @ReactMethod
  public void startScanning() {
    ScanFilter serviceFilter = new ScanFilter.Builder().setServiceUuid(SERVICE_UUID).build();

    ScanFilter configurationFilter = new ScanFilter.Builder().setServiceUuid(CONFIGURATION_UUID).build();

    final List<ScanFilter> filters = new ArrayList<>();
    filters.add(serviceFilter);
    filters.add(configurationFilter);

    ScanSettings settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();

    if (!bluetoothAdapter.isEnabled()) { // <--- here bluethoothAdapter is null
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
      getCurrentActivity().startActivityForResult(enableBtIntent, 8123);
    }

    // start scanning
    scanner = bluetoothAdapter.getBluetoothLeScanner();
    scanner.startScan(filters, settings, scanCallback);
  }
0

There are 0 best solutions below