PhoneGap Bluetooth Plugin on Android device

独自空忆成欢 提交于 2019-12-03 07:44:28

问题


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.


回答1:


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



回答2:


Maybe this article can help? This is more bluetooth connection with others specific then your question, but maybe it can help.I've used it in the past and it worked great with PhoneGap 3.0, only downside was that BlackBerry wasn't compatible anymore.



来源:https://stackoverflow.com/questions/20259071/phonegap-bluetooth-plugin-on-android-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!