如下代码,我测试好几遍不知道问题出在哪里,蓝牙是可以打开的,但就是搜索不到设备(我同时开了另外手机的蓝牙,并设置可发现),求大神解答!!!已经搞了很多天了!
document.addEventListener('plusready', function() {
document.getElementById("DingWei").addEventListener('tap', function() {
main = plus.android.runtimeMainActivity();
BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
BluetoothDevice = plus.android.importClass("android.bluetooth.BluetoothDevice");
// BluetoothSocket = plus.android.importClass("android.bluetooth.BluetoothSocket");
BAdapter = BluetoothAdapter.getDefaultAdapter();
var receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
onReceive: function(context, intent) { //实现onReceiver回调函数
plus.android.importClass(intent);
var action = intent.getAction();
console.log(action);
//如果发现蓝牙热点
if (BluetoothDevice.ACTION_FOUND == action) {
alert(99);
}
main.unregisterReceiver(receiver);
}
});
IntentFilter = plus.android.importClass('android.content.IntentFilter');
var filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BAdapter.ACTION_STATE_CHANGED); //监听蓝牙开关
filter.addAction(BAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(BAdapter.ACTION_SCAN_MODE_CHANGED);
main.registerReceiver(receiver, filter); //注册监听
if (!BAdapter.isEnabled()) {
BAdapter.enable(); //启动蓝牙
BAdapter.startDiscovery();
} else {
BAdapter.disable();
}
})
});
1 个回复
蛋蛋 (作者) - 穷怎么了?穷就应该被嘲笑吗?抬头!挺胸!让大家看看,你不仅穷,而且还丑。
官方的文档我都看了,也没有找到解决方案