javascript Android 设备上的 PhoneGap 蓝牙插件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20259071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:06:12  来源:igfitidea点击:

PhoneGap Bluetooth Plugin on Android device

javascriptcordovabluetoothphonegap-plugins

提问by benallansmith

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.

我一直在尝试让 PhoneGap 工作的蓝牙插件,但我似乎无法弄清楚我哪里出错了。首先,我的测试设备是 Galaxy S3 (GT-19305T),应用程序是使用PhoneGap CLI构建的。

The plugin I am attempting to use can be found herewith 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.

然后我开始了基本操作,并尝试使用带有 PhoneGap 示例的插件。我可以很容易地让所有这些工作。在我的示例中,我使用的是基本设备信息插件。

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:

网址:

  <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 应用它。

回答by Yorimitsu

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. The first three also need to be set up correctly in your Environment Path.

注意:您需要安装:Apache ANT、JAVA、Android SDK、GIT 命令行工具。前三个还需要在您的环境路径中正确设置。

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.coffeeto main.jsusing 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
  1. 下载 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. 下载这个
  10. 隐蔽main.coffeemain.js使用coffee --compile main.coffee
  11. 下载库文件(jQuery、bootstrap、下划线、主干)并将它们放在正确的目录中
  12. 将所有示例文档放在正确的目录中,并编辑索引以<script src="cordova.js">代替<script src="phonegap.js">
  13. cordova build android

回答by joostmakaay

Maybe thisarticle 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 配合得很好,唯一的缺点是黑莓不再兼容。