Search code examples
androidcordovabluetoothbluetooth-lowenergyphonegap

BLE access issue with `Cordova run android --device' command?


I'm building a simple BLE application using cordona/phonegap.

I use BLE lock as an example. Everything works fine with `cordova serve': cordova can scan and access available BLE devices with Phonegap Android application.

enter image description here enter image description here

However, with `cordova run android --device,' I could build a standalone Android app, download the app to my Android device; but no BLE devices are shown when I execute the downloaded app on the Android device.

enter image description here

What might make this difference? Do I need to do something more to make the standalone app access the Android BLE features?


Solution

  • You need to install the cordova bluetooth plugin into the Cordova app.

    cordova plugin add cordova-plugin-ble-central
    

    If you're using the phonegap cli, try

    phonegap plugin add cordova-plugin-ble-central
    

    The app works when running in the PhoneGap Developer App because the bluetooth plugin is pre-installed in that app. When you're running directly in Cordova, you need to install the plugin yourself.

    Before you install the plugin, you can see the error using Chrome. To get the stack trace from the application on your Android device: Open Chrome on your computer, enter chrome://inspect, choose your device. You should see a message like this in the console.

    stacktrace in chrome dev tools showing ble is not defined

    After you install the plugin and redeploy the project, scanning should work.

     cordova plugin add cordova-plugin-ble-central
     cordova run android --device
    

    screenshot of updated android app that found a bluetooth device