PhoneGap Bluetooth Plugin on Android device

17,614

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
Share:
17,614

Related videos on Youtube

benallansmith
Author by

benallansmith

Updated on June 04, 2022

Comments

  • benallansmith
    benallansmith almost 2 years

    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.

  • benallansmith
    benallansmith over 10 years
    This example is what I was using. This is stated at the start of my question.
  • benallansmith
    benallansmith over 10 years
    Thanks, this works for me. These are nicely detailed steps of how you built the apk, I followed very similar steps using the phonegap CLI and I'm not too sure why mine wasn't working. The only real difference I can see is the part where you edit the phonegap.js include to cordova.js
  • Stargazer
    Stargazer about 10 years
    Having some issues here also. I managed to do everything up to the point of building the damn thing. Once I put the apk file on the phone and install it, there is no boostrap layout and everything is disabled, can't click any button. Any thoughts?
  • Yorimitsu
    Yorimitsu about 10 years
    @Stargazer I think the reason everything is disabled is because the buttons are set to disabled by default. They are only enabled once everything is working. It seems something didn't work properly somewhere along the line and thus the buttons aren't getting enabled. You could try manually removing the disable from the buttons but I don't think it will help since the problem will still be there.
  • Stargazer
    Stargazer about 10 years
    @Yorimitsu, I did managed to put it to work. I was planing to come here and state that. In fact, it had to do with the plugin not being properly configured as a "plugman friendly" plugin on my app and also some other issues with CDN. Now I'm struggling to read binary data with it...But that's another story.
  • siva82kb
    siva82kb almost 10 years
    @Stargazer I am having the same problem as you (buttons are disabled). Could you tell me how you fixed your problem? I am new to mobile programming, and any link about this would be very useful.
  • user1126515
    user1126515 over 8 years
    i get processMessage failed: Stack: ReferenceError: isConnected is not defined" at main.js:122:14 DeviceListView = Backbone.View.extend({ el: "#list-devices", initialize: function() { return this.collection.on("reset add", this.render, this); }, render: function() { this.$el.html(""); return this.collection.each((function(_this) { return function(device) { return _this.$el.append(new DeviceView({ model: device }).render().el); <-------- line 122 }; })(this)); } }); Does anyone have any ideas?