prosseek
prosseek

Reputation: 190859

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?

Upvotes: 0

Views: 528

Answers (2)

doncoleman
doncoleman

Reputation: 2283

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

Upvotes: 1

prosseek
prosseek

Reputation: 190859

Don's answer is (almost) perfect to me, but I think I need to add some more things just in case.

plugin installation

The command I actually used was phonegap plugin add cordova-plugin-ble-central from this site.

After that, I have a plugin directory created with BLE libraries.

modification of config.xml (?)

I don't think this step is not necessary, but the site says that these two lines should be added in config.xml, but I could build the app without this step.

<gap:plugin name="cordova-plugin-ble-central" source="npm" /> <preference name="phonegap-version" value="cli-6.1.0" />

execution

I could use this command to build and install the app.

cordova platform add android cordova run android --device

The cause of the problem

When I tried some BLE plugins, I guess I installed other plugins. As a result I have com.megster.rordova.ble and cordova-plugin-ble-central in my plugins directory. I also guess config.xml and package.json might have some wrong information not to include the correct BLE library.

So, when I clean installed the BLE lock program, and installed the BLE plugin and build the app, everything seems fine.

Upvotes: 0

Related Questions