Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhoneGap Bluetooth Plugin on Android device

I've been trying to get a bluetooth plugin for PhoneGap working but I can't seem to figure out where I'm going wrong. Firstly, my test device is a Galaxy S3 (GT-19305T) and the applications were built using the PhoneGap CLI.

The plugin I am attempting to use can be found here with an example here.

I tried the example which didn't seem to actually do anything.

So then I went basic, and tried using the plugins with examples given by PhoneGap. I could quite easily get all of these working. In my example, I am using the basic device information plugin.

Here is some example code:

Javascript:

<script type="text/javascript" charset="utf-8">
    // Wait for device API libraries to load
    document.addEventListener("deviceready", onDeviceReady, false);
    // device APIs are available
    function onDeviceReady() {
        var element = document.getElementById('deviceProperties');
        element.innerHTML = 'Device Model: '    + device.model    + '<br />' +
                            'Device Cordova: '  + device.cordova  + '<br />' +
                            'Device Platform: ' + device.platform + '<br />' +
                            'Device UUID: '     + device.uuid     + '<br />' +
                            'Device Version: '  + device.version  + '<br />';
        var btstatus = document.getElementById('status');
        btstatus.innerHTML = "Getting bluetooth information";

        window.bluetooth.isEnabled(isEnabledSuccess, isEnabledError);
    }

    function checkBluetoothStatus() {
        var btstatus = document.getElementById('status');
        btstatus.innerHTML = "Checking bluetooth information";
        window.bluetooth.isEnabled(isEnabledSuccess, isEnabledError);
    }

    function isEnabledSuccess(isEnabled){
       var btstatus = document.getElementById('status');
       if(isEnabled){
         btstatus.innerHTML = "Enabled";
       }else{
         btstatus.innerHTML = "Disabled";
       }
    }

    function isEnabledError(error){
       var btstatus = document.getElementById('status');
       btstatus.innerHTML = "Cannot determine Bluetooth status: " + error.message;
    }

    function enableBluetooth(){
        var btstatus = document.getElementById('status');
        btstatus.innerHTML = "Attempting to turn bluetooth on";
        window.bluetooth.enable(bluetoothTestSucces, bluetoothTestFail);
    }
</script>

Html:

  <body>
    <p id="deviceProperties">Loading device properties...</p>
    <br />
    <button onclick="enableBluetooth();">Enable Bluetooth</button>
    <br />
    <button onclick="checkBluetoothStatus();">Check Bluetooth Status</button>
    <br />
    <p id="status">Loading bluetooth information...</p>
  </body>

So basically I am trying to either get the plugin to return the current bluetooth connectivity information, or enable the bluetooth upon clicking the "enable bluetooth" button.

Unfortunately nothing has worked so far and as I stated earlier I am not sure where I am going wrong.

I have tried applying it manually and by using the CLI.

like image 413
benallansmith Avatar asked Nov 28 '13 06:11

benallansmith


1 Answers

I have recently experimented with the same example and was able to get it working. The main difference however is that I used Cordova CLI instead.

Note: You will need to have installed: Apache ANT, JAVA, Android SDK, GIT Command Line Tool. The first three also need to be set up correctly in your Environment Path.

These are the steps I performed:

  1. Download Node.JS (and then run the command prompt)
  2. npm install -g cordova
  3. npm install -g coffee-script
  4. cd C:\
  5. cordova create bluetooth com.example.bluetooth bluetooth
  6. cd bluetooth
  7. cordova platform add android
  8. cordova plugin add https://github.com/tanelih/phonegap-bluetooth-plugin.git
  9. Download this
  10. Covert main.coffee to main.js using coffee --compile main.coffee
  11. Download the library files (jQuery, bootstrap, underscore, backbone) and place them in the correct directories
  12. Place all of the example documents in the correct directories, and edit index to have <script src="cordova.js"> instead of <script src="phonegap.js">
  13. cordova build android
like image 119
Yorimitsu Avatar answered Oct 28 '22 13:10

Yorimitsu