uni app的蓝牙api搜索不到蓝牙设备,就是搜索的特别慢,
我在onLoad调用onBluetoothDeviceFound 来监听发现新的设备,但是我真机调试搜索很快,我打包发行后,发现搜索蓝牙设备特别慢,这是啥原因呢
是不是要发行的时候把APP的定位模块勾上
onLoad(options) {
this.type = options.type
let that = this
uni.onBluetoothAdapterStateChange((res) => {
that.searching = res.discovering
if (!res.available) {
that.searching = false
}
});
uni.onBluetoothDeviceFound((devices) => {
//剔除重复设备,兼容不同设备API的不同返回值
var isnotexist = true;
if (devices.deviceId) {
if (devices.advertisData) {
devices.advertisData = app.globalData.buf2hex(devices.advertisData);
} else {
devices.advertisData = '';
}
for (var i = 0; i < that.devicesList.length; i++) {
if (devices.deviceId == that.devicesList[i].deviceId) {
isnotexist = false;
}
}
if (isnotexist) {
that.devicesList.push(devices);
that.$forceUpdate()
}
} else if (devices.devices) {
if (devices.devices[0].advertisData) {
devices.devices[0].advertisData = app.globalData.buf2hex(devices.devices[0]
.advertisData);
} else {
devices.devices[0].advertisData = '';
}
console.log(devices.devices[0]);
for (var i = 0; i < that.devicesList.length; i++) {
if (devices.devices[0].deviceId == that.devicesList[i].deviceId) {
isnotexist = false;
}
}
if (isnotexist) {
that.devicesList.push(devices.devices[0]);
that.$forceUpdate()
}
} else if (devices[0]) {
if (devices[0].advertisData) {
devices[0].advertisData = app.globalData.buf2hex(devices[0].advertisData);
} else {
devices[0].advertisData = '';
}
console.log(devices[0]);
for (var i = 0; i < devices_list.length; i++) {
if (devices[0].deviceId == that.devicesList[i].deviceId) {
isnotexist = false;
}
}
if (isnotexist) {
that.devicesList.push(devices[0]);
that.$forceUpdate()
}
}
that.devicesList = that.devicesList
that.$forceUpdate()
});
},
onShow() {
this.Search()
},
methods: {
Search() {
var that = this;
if (!that.searching) {
// uni.closeBluetoothAdapter({
// complete(res) {
// console.log(res);
uni.openBluetoothAdapter({
success(res) {
console.log(res);
uni.getBluetoothAdapterState({
success(res) {
console.log(res);
}
});
uni.startBluetoothDevicesDiscovery({
allowDuplicatesKey: false,
success(res) {
console.log(res);
that.searching = true,
that.devicesList = []
}
});
},
fail(res) {
console.log(res);
uni.showModal({
title: '提示',
content: '请检查手机蓝牙是否打开',
showCancel: false,
success(res) {
that.searching = false
}
});
}
});
// }
// });
} else {
uni.stopBluetoothDevicesDiscovery({
success(res) {
console.log(res);
that.searching = false
}
});
}
}
}
0 个回复