可以监测蓝牙开启和关闭,正式包之后,搜不到其他蓝牙,但是标准基座调试没有任何问题,配置权限蓝牙都均配置好。 plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {onReceive: function(context, intent) { }这个回调都进不去具体代码如下
:
ble.search = function(address, pipeiFunc, noPipeiFunc, finishFunc) {
if (isSearch) return;
isSearch = true;
//var main = plus.android.runtimeMainActivity();
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
//var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
var BluetoothDevice = plus.android.importClass("android.bluetooth.BluetoothDevice");
//var BAdapter = BluetoothAdapter.getDefaultAdapter();
var filter = new IntentFilter();
var bdevice = new BluetoothDevice();
var on = null;
var un = null;
BAdapter.startDiscovery(); //开启搜索
console.log("搜索已开启")
alert("搜索已开启");
var receiver;
receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
onReceive: function(context, intent) { //实现onReceiver回调函数
//alert(intent);
plus.android.importClass(intent); //通过intent实例引入intent类,方便以后的‘.’操作
if (intent.getAction() == "android.bluetooth.adapter.action.DISCOVERY_FINISHED") {
alert("取消监听");
main.unregisterReceiver(receiver); //取消监听
finishFunc();
} else {
console.log("jinlai1");
var BleDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
try {
plus.android.importClass(BleDevice);
} catch (e) {
console.log(e);
}
// console.log(BleDevice, "bdevice.BOND_NONE")
console.log(BleDevice.getBondState() + "w");
//判断是否配对
if (BleDevice.getBondState() == bdevice.BOND_NONE) {
console.log("jinlai2");
//参数如果跟取得的mac地址一样就配对
if (address == BleDevice.getAddress()) {
if (BleDevice.createBond()) {
//id name
pipeiFunc(BleDevice.getAddress(), BleDevice.getName());
}
} else {
if (BleDevice.getName() != on) {
//判断防止重复添加
noPipeiFunc(BleDevice.getAddress(), BleDevice.getName());
}
}
} else {
if (BleDevice.getName() != un) {
//判断防止重复添加
pipeiFunc(BleDevice.getAddress(), BleDevice.getName());
}
}
}
}
});
filter.addAction(bdevice.ACTION_FOUND);
filter.addAction(BAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(BAdapter.ACTION_STATE_CHANGED);
main.registerReceiver(receiver, filter); //注册监听
}
1 个回复
刘瑞强 (作者) - Dcloud
这个是打包正常使用的,就是一周前突然不能用了,搜索不到周围蓝牙,但基座调试是可以的,希望有了解的回复一下