在uniapp 中调用蓝牙的API 其他的手机都可以 包括 荣耀手机(安卓12),但是 荣耀手机如果是安卓13的版本就不行。在
onBluetoothDeviceFound 中直接没有进入。搜索不到任何的蓝牙设备。
当前 targetSdkVersion 版本为 32 IDE 版本为 3.8.7
代码为
var BluetoothSortingScan = {};
var deviceArray = [];
BluetoothSortingScan.distance = ((rssi, A, n) => {
let iRssi = Math.abs(rssi);
let power = (iRssi - A) / (10 * n);
return Math.pow(10, power);
}),
//蓝牙开始 更改后
BluetoothSortingScan.start = function(that, callback) {
uni.closeBluetoothAdapter({
complete(res) {
deviceArray = [];
uni.openBluetoothAdapter({//首先初始化蓝牙
success(res) {
console.log(res);
uni.getBluetoothAdapterState({
success: function(res) {}
});
uni.onBluetoothAdapterStateChange(function(res) {
if (!res.available) {}
});
uni.startBluetoothDevicesDiscovery({
success(res3) {
console.log(JSON.stringify(res3));
}
});
uni.onBluetoothDeviceFound(res1=>{
console.log(JSON.stringify(res1));
var isnotexist = true
if (res1) {
var deviceName = res1.devices[0].localName;
var rssi = res1.devices[0].RSSI;
let device = {
"deviceName": deviceName,
"rssi": rssi,
"deviceId": res1.devices[0].deviceId
};
deviceArray.push(device);
}
});
uni.startBluetoothDevicesDiscovery({
services: ['6E400001-B5A3-F393-E0A9-E50E24DCCA9E'],
allowDuplicatesKey: false,
success:(res) =>{
console.log(JSON.stringify(res) );
BluetoothSortingScan.stop(callback); //延时停止扫描
}
});
},
fail:err=>{
uni.showModal({
title: '提示',
content: '请检查手机蓝牙是否打开'
});
}
});
}
});
}
BluetoothSortingScan.stop = function(callback) {
setTimeout(function() {
uni.stopBluetoothDevicesDiscovery({
success: function(res) {
console.log("停止扫描");
deviceArray = deviceArray.sort(function(a, b) {
return b.rssi - a.rssi //如果是降序,则为b - a
})
callback(deviceArray);
}
})
}, 500);
}
module.exports = BluetoothSortingScan;
请问 是应为版本的问题吗?
0 个回复