详细问题描述
代码:
/**
- 初始化蓝牙设备
- 步骤1
*/
openBluetoothAdapter() {
uni.openBluetoothAdapter({
success: e => {
//console.log('初始化蓝牙成功:' + e.errMsg);
//console.log(JSON.stringify(e));
this.getBluetoothAdapterState();
},
fail: e => {
//console.log(e)
//console.log('初始化蓝牙失败,错误码:' + (e.errCode || e.errMsg));
if (e.errCode !== 0) {
this.initTypes(e.errCode, e.errMsg);
}
}
});
},
/** - 获取本机蓝牙适配器状态
-
步骤2
*/
getBluetoothAdapterState() {
//console.log('--->');
uni.getBluetoothAdapterState({
success: res => {
//console.log(JSON.stringify(res));
this.adapterState = res;
let e_this = this
// this.startBluetoothDevicesDiscovery();
setTimeout(function() {
e_this.startBluetoothDevicesDiscovery();
}, 500)}, fail: e => { //console.log('获取本机蓝牙适配器状态失败,错误码:' + e.errCode); if (e.errCode !== 0) { this.initTypes(e.errCode); } } }); }, /**
- 开始搜索蓝牙设备
-
步骤3
*/
startBluetoothDevicesDiscovery() {
uni.startBluetoothDevicesDiscovery({
services: ['FFF0'],
success: e => {
//console.log('开始搜索蓝牙设备:' + e.errMsg);
//this.searchLoad = true;
this.onBluetoothDeviceFound();
},
fail: e => {
//console.log('搜索蓝牙设备失败,错误码:' + e.errCode);
if (e.errCode !== 0) {
this.initTypes(e.errCode);
}
}
});
},/**
- 发现外围设备
- 步骤4
*/
onBluetoothDeviceFound() {
uni.onBluetoothDeviceFound(devices => {
//console.log('开始监听寻找到新设备的事件');
// this.$set(this.disabled, 3, false);
this.getBluetoothDevices();
});
},
/** - 获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。
-
步骤5
*/
getBluetoothDevices() {
uni.getBluetoothDevices({
success: res => {
this.newDeviceLoad = false;
console.log(JSON.stringify(res));
//console.log('获取蓝牙设备成功:' + res.errMsg);
// for (let i = 0; i < res.devices.length; i++) {
// console.log(res.devices[i])
// }
this.devicesList = res.devices; //蓝牙设备列表this.saveList = this.devicesList.slice() //用于搜索筛选 }, fail: e => { //console.log('获取蓝牙设备错误,错误码:' + e.errCode); if (e.errCode !== 0) { this.initTypes(e.errCode); } } }); },
蓝牙搜索结果:
{"devices":[{"deviceId":"6A:34:E9:F1:24:6F","name":"未知设备","RSSI":-71,"localName":"","advertisServiceUUIDs":[],"advertisData":{}},{"deviceId":"66:E7:24:35:C3:C6","name":"未知设备","RSSI":-86,"localName":"","advertisServiceUUIDs":[],"advertisData":{}},{"deviceId":"5B:67:08:35:E9:52","name":"未知设备","RSSI":-74,"localName":"","advertisServiceUUIDs":[],"advertisData":{}},{"deviceId":"4C:24:98:CE:FA:2E","name":"ABCDEFGHIJKLMNOP","RSSI":-85,"localName":"ABCDEFGHIJKLMNOP","advertisServiceUUIDs":["0000FFE0-0000-1000-8000-00805F9B34FB","0000FEE7-0000-1000-8000-00805F9B34FB"],"advertisData":{}},{"deviceId":"9C:1D:58:16:B2:79","name":"ABCDEFGHIJKLMNOP","RSSI":-89,"localName":"ABCDEFGHIJKLMNOP","advertisServiceUUIDs":["0000FFE0-0000-1000-8000-00805F9B34FB","0000FEE7-0000-1000-8000-00805F9B34FB"],"advertisData":{}},{"deviceId":"71:F5:34:31:28:2D","name":"未知设备","RSSI":-98,"localName":"","advertisServiceUUIDs":[],"advertisData":{}},{"deviceId":"C1:02:03:04:05:06","name":"191111PDF0000007","RSSI":-54,"localName":"191111PDF0000007","advertisServiceUUIDs":[],"advertisData":{}},{"deviceId":"71:9C:7C:B7:A6:CB","name":"未知设备","RSSI":-70,"localName":"","advertisServiceUUIDs":[],"advertisData":{}},{"deviceId":"EC:FB:F3:F2:E9:CB","name":"190603PDF0001079","RSSI":-92,"localName":"190603PDF0001079","advertisServiceUUIDs":[],"advertisData":{}}],"errMsg":"getBluetoothDevices:ok"}
IDE运行环境说明
[HBuilder 或 HBuilderX。如果你用其他工具开发uni-app,也需要在此说明]
HBuilderX
[IDE版本号]
2.3.7.20191024
[windows版本号]
win10
[mac版本号]
uni-app运行环境说明
[运行端是h5或app或某个小程序?]
app
[项目是cli创建的还是HBuilderX创建的?如果是cli创建的,请更新到最新版cli再试]
HBuilderX创建的
[编译模式是老模板模式还是新的自定义组件模式?]
uni-app模式
App运行环境说明
[Android版本号]
安卓9
[iOS版本号]
[手机型号]
MI 8 SE
[QQ] 206969186
2 个回复
1***@qq.com - 天天开心
遇到了同样问题,等待官方解决!
1***@qq.com
遇到了同样问题,希望官方尽早解决!