dclouder 连接蓝牙

搞了很久,终于搞出来的,但是还不是很友好,目前,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); //注册监听
\n

搜索完了,开始连接蓝牙:

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('设备已连接'); //连接成功做标记,下次直接连接
\n

// 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);
}
}

附件是完整的代码:


7 分享 关注
DCloud_heavensoft DCloud_客服_Trust 黑槿 499636526@qq.com 18753062322@163.com 411753814@qq.com 306626546@qq.com
1392293229@qq.com

1392293229@qq.com

现在不行了连接报错Uncaught java.io.IOException: read failed, socket might closed or timeout, read ret: -1;at android.bluetooth.BluetoothSocket.connect at index.html:1,
0 赞 4 天前
睿小狼

睿小狼 回复 赵梦欢

最好提供一个插件中心,让有能力的人分享一下,这样不懂原生开发的人能方便很多。。。
0 赞 2016-12-12 01:12
yinfuqing666@126.com

yinfuqing666@126.com 回复 赵梦欢

能否提供思路,我这里总是阻塞线程
0 赞 2016-09-22 11:49
yinfuqing666@126.com

yinfuqing666@126.com

什么样的的
1 赞 2016-09-22 11:49
赵梦欢

赵梦欢

最好使用5+ 插件的形式集成,这样可以自己可以写一个service。
1 赞 2016-09-08 13:26

要回复文章请先登录注册