简体   繁体   English

Android设备上的PhoneGap蓝牙插件

[英]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. 我一直在尝试为PhoneGap工作获得一个蓝牙插件,但我似乎无法弄清楚我哪里出错了。 Firstly, my test device is a Galaxy S3 (GT-19305T) and the applications were built using the PhoneGap CLI . 首先,我的测试设备是Galaxy S3(GT-19305T),应用程序是使用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. 那么我就基本了,并尝​​试使用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: 使用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: 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. 我尝试过手动应用它并使用CLI。

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 . 然而,主要区别在于我使用了Cordova CLI

Note: You will need to have installed: Apache ANT, JAVA, Android SDK, GIT Command Line Tool. 注意:您需要安装:Apache ANT,JAVA,Android SDK,GIT命令行工具。 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) 下载Node.JS(然后运行命令提示符)
  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 隐蔽main.coffeemain.js使用coffee --compile main.coffee
  11. Download the library files (jQuery, bootstrap, underscore, backbone) and place them in the correct directories 下载库文件(jQuery,bootstrap,下划线,主干)并将它们放在正确的目录中
  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"> 将所有示例文档放在正确的目录中,并编辑索引以使<script src="cordova.js">而不是<script src="phonegap.js">
  13. cordova build android

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. 这是与他人特定的蓝牙连接然后你的问题,但也许它可以帮助。我过去使用它,它与PhoneGap 3.0一起工作很好,唯一的缺点是黑莓不再兼容了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM