最后还是弃用了,因为监听接收消息的时候,发现好像页面卡死了,也不知道有啥好办法,
搜索不到 我这边的原因是因为没有申请到动态权限,代码如下
searchDevices() {
let BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
let BluetoothDevice = plus.android.importClass("android.bluetooth.BluetoothDevice");
let BluetoothSocket = plus.android.importClass("android.bluetooth.BluetoothSocket");
let BluetoothReceiver = plus.android.importClass("android.bluetooth.BluetoothReceiver");
let Intent = plus.android.importClass("android.content.Intent");
let IntentFilter = plus.android.importClass("android.content.IntentFilter");
this.BAdapter = BluetoothAdapter.getDefaultAdapter();
//检查设备是否支持蓝牙
if (this.BAdapter == null) {
uni.showToast({
title: "该设备不支持蓝牙",
duration: 2000,
icon: "none"
});
return false;
} else {
// 如果没有打开蓝牙
if (!this.BAdapter.isEnabled()) {
// 打开蓝牙操作
let startBt = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
console.log(startBt);
this.mainActivity.startActivityForResult(startBt, 1000);
// this.BAdapter.enable(); //打开蓝牙
// console.log(this.BAdapter.getAddress());
// console.log(this.BAdapter.getName());
} else {
// console.log(BAdapter.isDiscovering());
// 检测当前是否在搜索蓝牙
if (this.BAdapter.isDiscovering()) {
this.BAdapter.cancelDiscovery();
}
let IntentFilter = plus.android.importClass('android.content.IntentFilter');
let filter = new IntentFilter();
if (this.btFindReceiver != null) {
try {
this.mainActivity.unregisterReceiver(this.btFindReceiver);
} catch (e) {
console.error(e);
}
this.btFindReceiver = null;
// this.BAdapter.cancelDiscovery();
}
// SDK
this.Build = plus.android.importClass("android.os.Build");
let Manifest = plus.android.importClass("android.Manifest");
// console.log(Build.VERSION.SDK_INT);
// if (Build.VERSION.SDK_INT >= 6.0) {}
let ArrPermissions = [
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.BLUETOOTH,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
]
if (!this.PermissionChecks(ArrPermissions)) {
// 如果没有权限,请求权限
this.PermissionRequest(ArrPermissions);
} else {
// 开始showtoast
uni.showLoading({
title: '蓝牙设备搜索中',
mask: false
});
filter.addAction(BluetoothDevice.ACTION_FOUND); //发现蓝牙设备
filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); //蓝牙设备绑定状态改变
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); //蓝牙设备的状态改变
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); //搜索完成
//filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); //开始扫描
this.btFindReceiver = null;
let that1 = this;
this.devsList = [];
this.btFindReceiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
onReceive: function(context, intent) { //实现onReceiver回调函数
console.log('onReceive函数回调');
plus.android.importClass(context);
plus.android.importClass(intent);
let action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND == action) { // 找到设备
let device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
let newDevice = {
"object": device,
"name": plus.android.invoke(device, "getName") ? plus.android.invoke(device, "getName") : '',
"address": plus.android.invoke(device, "getAddress") ? plus.android.invoke(device, "getAddress") : ''
};
that1.devsList.push(newDevice)
// console.log(plus.android.invoke(device, "getName"));
// that1.DiscoveryNewDevice(newDevice);
}
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED == action) { // 搜索完成
console.log('搜索完成');
that1.BAdapter.cancelDiscovery();
uni.hideLoading();
uni.showToast({
title: "搜索结束",
duration: 2000,
icon: "none"
});
}
console.log('onReceive结束回调');
}
});
this.mainActivity.registerReceiver(this.btFindReceiver, filter);
this.BAdapter.startDiscovery(); //开启搜索
}
}
}