搞了很久,终于搞出来的,但是还不是很友好,目前,js不能开启多线程,只能 一次取完了数据,就关闭socket,下次再取数据,的时候,
在开启socket 获取完了,关闭;不能一直开着socket ,不然会阻塞当前主线程,什么也干不了,有没有大神指导这种情况如何处理,怎样才能,一直开着socket ,不停地解析传过来的包,不阻塞主线程呢,我是使用dclouder连接了蓝牙设备,蓝牙发信号,然后手机接收 数据,
先是搜索:
console.log("search::isOpen="+isOpen);
if(isOpen==true) return;
main = plus.android.runtimeMainActivity();
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
var BluetoothDevice = plus.android.importClass("android.bluetooth.BluetoothDevice");
console.log("开始搜索设备");
var filter = new IntentFilter();
// var bdevice = new BluetoothDevice();
var BleDevice = new BluetoothDevice();
disableBtn(true);
BAdapter.startDiscovery(); //开启搜索
var receiver;
receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
onReceive: function(context, intent) { //实现onReceiver回调函数
plus.android.importClass(intent); //通过intent实例引入intent类,方便以后的‘.’操作
if(intent.getAction() == "android.bluetooth.adapter.action.DISCOVERY_FINISHED") {
main.unregisterReceiver(receiver); //取消监听
disableBtn(false);
plus.nativeUI.toast("搜索结束");
console.log("搜索结束");
} else {
BleDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if(BleDevice==null){main.unregisterReceiver(receiver); //取消监听
disableBtn(false); return;}
var name=BleDevice.getAddress()+BleDevice.getName();
if(bleArray.indexOf(name)==-1){
bleArray.push(name);
addNode(table,BleDevice.getAddress(),BleDevice.getName());
}//开始判断
var lastBLE = localStorage.getItem('lastBLE');
console.log("lastBLE::"+lastBLE);
if(lastBLE!=null&&lastBLE==BleDevice.getAddress()){
main.unregisterReceiver(receiver); //取消监听
disableBtn(false);
ConnectEqu(lastBLE);
}
}
}
});
filter.addAction(BleDevice.ACTION_FOUND);
filter.addAction(BAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(BAdapter.ACTION_STATE_CHANGED);
main.registerReceiver(receiver, filter); //注册监听
搜索完了,开始连接蓝牙:
main = plus.android.runtimeMainActivity();
BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
UUID = plus.android.importClass("java.util.UUID");
uuid = UUID.fromString(FETUS_UUID);
BAdapter = BluetoothAdapter.getDefaultAdapter();
device = BAdapter.getRemoteDevice(mac_address);
plus.android.importClass(device);
bluetoothSocket=null;
bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
plus.android.importClass(bluetoothSocket);
if(!bluetoothSocket.isConnected()) {
bluetoothSocket.connect();
}
if(bluetoothSocket.isConnected()) {
isOpen=true;
console.log("search::isOpen="+isOpen);
console.log('设备已连接'); //连接成功做标记,下次直接连接
// localStorage.setItem('lastBLE',mac_address);
// var lastBLE =localStorage.getItem('lastBLE');
// console.log("lastBLE::"+lastBLE);
var inputStream = bluetoothSocket.getInputStream();
plus.android.importClass(inputStream);
var msg =null;
try{
var msg ;//= br.readLine();
while(isOpen){
var msg=inputStream.read();
console.log(msg);
parseData(msg);
}
// }
//地址:http://ask.dcloud.net.cn/question/60
}catch(err){
console.log("异常"+err);
}
}
附件是完整的代码: