uni-app 里面的蓝牙api 就是我打包成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
}
});
}
}
}