編輯
如果你想使用套接字連接和傳輸數據,你可以試試這個插件。
https://www.npmjs.com/package/cordova-plugin-networking-bluetooth
安裝:
cordova plugin add cordova-plugin
實施例連接:
var uuid = '94f39d29-7d6d-437d-973b-fba39e49d4ee';
networking.bluetooth.connect(device.address, uuid, function (socketId) {
// Profile implementation here.
}, function (errorMessage) {
console.log('Connection failed: ' + errorMessage);
});
實施例從插座接收數據:
networking.bluetooth.onReceive.addListener(function (receiveInfo) {
if (receiveInfo.socketId !== socketId) {
return;
}
// receiveInfo.data is an ArrayBuffer.
});
原始
我以前使用這個插件:
https://github.com/don/BluetoothSerial
安裝:
cordova plugin add cordova-plugin-bluetooth-serial
您的手機必須雖然發起連接,那麼你可以使用訂閱方法來監聽數據。這些數據然後傳遞給回調函數。
在從python腳本發送的數據中,您將需要包含一個分隔符(如新行),以便插件知道何時完成讀取數據。用法如下:
function connectSuccess(){
//The first argument is the delimiter to stop reading data at
bluetoothSerial.subscribe('\n', function (data) {
console.log(data);
}, failure);
}
function failure(e){
console.log('Subscribe failure: ' + e);
}
function connectFailure(e){
console.log('Connect failure: ' + e);
}
bluetoothSerial.connect(macAddress_or_uuid, connectSuccess, connectFailure);
感謝您的回覆。我正在嘗試你的方式,請保持聯繫。 – user2709785
讓我知道,如果我可以進一步幫助。 –
是的,我嘗試過,但連接API後它給錯誤無法連接。 – user2709785