我一直在尝试让 PhoneGap 工作的蓝牙插件,但我似乎不知道哪里出了问题。首先,我的测试设备是 Galaxy S3 (GT-19305T),应用程序是使用PhoneGap CLI http://docs.phonegap.com/en/3.0.0/guide_cli_index.md.html.
可以找到我尝试使用的插件here https://github.com/tanelih/phonegap-bluetooth-plugin举个例子here http://blogit.jamk.fi/metsaapuilta/2013/10/24/bluetooth-for-phonegap/.
我尝试了这个例子,但它似乎没有实际做任何事情。
然后我开始基础操作,并尝试使用 PhoneGap 给出的示例插件。我可以很容易地让所有这些工作起来。在我的示例中,我使用基本设备信息插件。
这是一些示例代码:
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>
所以基本上我试图让插件返回当前的蓝牙连接信息,或者在单击“启用蓝牙”按钮时启用蓝牙。
不幸的是,到目前为止没有任何效果,正如我之前所说,我不确定我哪里出错了。
我尝试过手动应用它并使用 CLI。