Javascript 如何实现与Phonegap的蓝牙通信?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11261547/
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
How to implement Bluetooth communication with Phonegap?
提问by Fran
I'm intended to program an interface with HTML/JAVASCRIPT/jQuery to control an electronic device. The communication must be via Bluetooth (there's already an instruction set to control the device in existence, and the device is already Bluetooth-capable), and this interface should be portable to an android app via Phonegap...
我打算用 HTML/JAVASCRIPT/jQuery 编写一个界面来控制电子设备。通信必须通过蓝牙(已经存在控制设备的指令集,并且该设备已经具有蓝牙功能),并且该接口应该可以通过Phonegap移植到Android应用程序......
so I would love to get some advice in how to establish a Bluetooth communication with HTML or JAVASCRIPT, and to port it via Phonegap... tutorials, books, or whatever kind of hint will be warmly wellcomed!
所以我很想得到一些关于如何与 HTML 或 JAVASCRIPT 建立蓝牙通信的建议,并通过 Phonegap 移植它......教程、书籍或任何类型的提示都会受到热烈欢迎!
回答by Roman K
Quick answer: You have to write your own cordova/phonagap plugin.
快速回答:您必须编写自己的cordova/phonagap 插件。
Longer answer:
更长的答案:
The only way to establish communication between HTML and native phone API within cordova/phonegap is to use Phonegap Plugin API. The plugin API works as a bridge between the phone and the browser JS.
在cordova/phonegap 中建立HTML 和本机电话API 之间的通信的唯一方法是使用Phonegap 插件API。插件 API 充当手机和浏览器 JS 之间的桥梁。
You have to write native code for you target platforms and JS.
您必须为目标平台和 JS 编写本机代码。
The most tricky part is to unify the handling of different native APIs from the different mobile platforms on the JS site. For a single targeted platform the complexity is quite manageable.
最棘手的部分是在 JS 站点上统一处理来自不同移动平台的不同原生 API。对于单个目标平台,复杂性是相当可控的。
Phonegap has an github page with almost all available plugins open sourced. You could use them as examples. There are also some tutorials available.
Phonegap 有一个 github 页面,几乎所有可用的插件都是开源的。你可以用它们作为例子。还有一些可用的教程。