编辑
如果你想使用套接字连接和传输数据,你可以试试这个插件。
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
不过,您的手机必须启动连接,然后您才能使用 subscribe 方法来监听数据。然后将此数据传递给回调函数。
在从 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);