详细问题描述
安卓在调用uni.startBluetoothDevicesDiscovery后,在uni.onBluetoothDeviceFound监听设备,我的蓝牙设备设备的是每0.5秒广播一次信号,在IOS和微信小程序里测试都是正常的,很快就能发现设备,但在安卓里不正常,发现设备的时间从1秒到20秒不等。以下是主要测试代码:
startBluetoothDiscovery() {
var p = this;
p.listenerDeviceFound();
uni.startBluetoothDevicesDiscovery({
success: function(e) {
p.log('start discovery success: ' + JSON.stringify(e));
p.startTime = new Date().getTime();
},
fail: function(e) {
p.log('start discovery failed: ' + JSON.stringify(e));
}
});
},
listenerDeviceFound() {
var p = this;
uni.onBluetoothDeviceFound(function(e) {
var devices = e.devices;
p.log('device found: ' + e.length);
for (var i in devices) {
p.log(i + ': ' + JSON.stringify(devices[i]));
}
p.endTime = new Date().getTime();
console.log('deviceFind time:' + Math.round((p.endTime-p.startTime)/1000));
setTimeout(()=>{
p.stopBluetoothDiscovery();
}, 500);
setTimeout(()=>{
p.startBluetoothDiscovery();
}, 1500);
});
},
stopBluetoothDiscovery() {
var p = this;
this.closeConnection();
uni.stopBluetoothDevicesDiscovery({
success: function(e) {
p.log('stop discovery success: ' + JSON.stringify(e));
},
fail: function(e) {
p.log('stop discovery failed: ' + JSON.stringify(e));
}
});
}
以下为测试结果:
16:21:09.683 0: {"deviceId":"C0:BD:13:F2:AB:4E","name":"000-101","RSSI":-87,"localName":"000-101","advertisServiceUUIDs":["6E400001-B5A3-F393-E0A9-C0BD13F2AB4E"]} at pages/bletest/bletest.vue:277
16:21:09.703 deviceFind time:3 at pages/bletest/bletest.vue:120
16:21:19.265 0: {"deviceId":"C0:BD:13:F2:AB:4E","name":"000-101","RSSI":-87,"localName":"000-101","advertisServiceUUIDs":["6E400001-B5A3-F393-E0A9-C0BD13F2AB4E"]} at pages/bletest/bletest.vue:277
16:21:19.285 deviceFind time:8 at pages/bletest/bletest.vue:120
16:21:28.899 0: {"deviceId":"C0:BD:13:F2:AB:4E","name":"000-101","RSSI":-77,"localName":"000-101","advertisServiceUUIDs":["6E400001-B5A3-F393-E0A9-C0BD13F2AB4E"]} at pages/bletest/bletest.vue:277
16:21:28.918 deviceFind time:8 at pages/bletest/bletest.vue:120
16:21:34.941 0: {"deviceId":"C0:BD:13:F2:AB:4E","name":"000-101","RSSI":-83,"localName":"000-101","advertisServiceUUIDs":["6E400001-B5A3-F393-E0A9-C0BD13F2AB4E"]} at pages/bletest/bletest.vue:277
16:21:34.961 deviceFind time:4 at pages/bletest/bletest.vue:120
16:21:44.501 0: {"deviceId":"C0:BD:13:F2:AB:4E","name":"000-101","RSSI":-90,"localName":"000-101","advertisServiceUUIDs":["6E400001-B5A3-F393-E0A9-C0BD13F2AB4E"]} at pages/bletest/bletest.vue:277
16:21:44.521 deviceFind time:8 at pages/bletest/bletest.vue:120
16:22:03.641 0: {"deviceId":"C0:BD:13:F2:AB:4E","name":"000-101","RSSI":-88,"localName":"000-101","advertisServiceUUIDs":["6E400001-B5A3-F393-E0A9-C0BD13F2AB4E"]} at pages/bletest/bletest.vue:277
16:22:03.661 deviceFind time:18 at pages/bletest/bletest.vue:120
16:22:19.286 0: {"deviceId":"C0:BD:13:F2:AB:4E","name":"000-101","RSSI":-74,"localName":"000-101","advertisServiceUUIDs":["6E400001-B5A3-F393-E0A9-C0BD13F2AB4E"]} at pages/bletest/bletest.vue:277
16:22:19.306 deviceFind time:14 at pages/bletest/bletest.vue:120
16:22:28.877 0: {"deviceId":"C0:BD:13:F2:AB:4E","name":"000-101","RSSI":-82,"localName":"000-101","advertisServiceUUIDs":["6E400001-B5A3-F393-E0A9-C0BD13F2AB4E"]} at pages/bletest/bletest.vue:277
16:22:28.897 deviceFind time:8 at pages/bletest/bletest.vue:120
16:22:38.483 0: {"deviceId":"C0:BD:13:F2:AB:4E","name":"000-101","RSSI":-89,"localName":"000-101","advertisServiceUUIDs":["6E400001-B5A3-F393-E0A9-C0BD13F2AB4E"]} at pages/bletest/bletest.vue:277
16:22:38.503 deviceFind time:8 at pages/bletest/bletest.vue:120
16:22:48.071 0: {"deviceId":"C0:BD:13:F2:AB:4E","name":"000-101","RSSI":-73,"localName":"000-101","advertisServiceUUIDs":["6E400001-B5A3-F393-E0A9-C0BD13F2AB4E"]} at pages/bletest/bletest.vue:277
16:22:48.091 deviceFind time:8 at pages/bletest/bletest.vue:120
16:22:57.637 0: {"deviceId":"C0:BD:13:F2:AB:4E","name":"000-101","RSSI":-92,"localName":"000-101","advertisServiceUUIDs":["6E400001-B5A3-F393-E0A9-C0BD13F2AB4E"]} at pages/bletest/bletest.vue:277
16:22:57.657 deviceFind time:8 at pages/bletest/bletest.vue:120
16:23:20.450 0: {"deviceId":"4D:6B:B8:39:D5:00","name":"","RSSI":-95,"localName":"","advertisServiceUUIDs":[],"advertisData":{}} at pages/bletest/bletest.vue:277
16:23:20.470 deviceFind time:21 at pages/bletest/bletest.vue:120
16:23:23.890 0: {"deviceId":"C0:BD:13:F2:AB:4E","name":"000-101","RSSI":-87,"localName":"000-101","advertisServiceUUIDs":["6E400001-B5A3-F393-E0A9-C0BD13F2AB4E"]} at pages/bletest/bletest.vue:277
16:23:23.910 deviceFind time:2 at pages/bletest/bletest.vue:120
其中“ deviceFind time:8”后面的数字即为发现设备的时间,单位为秒,基本在8秒左右,有时候会2、3秒,有时候也会10多20秒。
IDE运行环境说明
[HBuilderX]
[1.9.4]
[windows7]
[mac版本号]
uni-app运行环境说明
[运行端是app]
[运行端版本号]
[项目是cli创建的还是HBuilderX创建的]
[编译模式是老模板模式还是新的自定义组件模式?]
App运行环境说明
[Android8]
[iOS9]
[荣耀8,小米6,红米2,荣耀8X,荣耀mate10 pro, iphone5,iphone5s,iphone6等,多种手机]
[模拟器型号]
附件
[IDE问题请提供HBuilderX运行日志。菜单帮助-查看运行日志,点右键打开文件所在目录,将log文件压缩成zip包上传]
[App问题请提供可重现问题的代码片段,你补充的细一点,问题就解决的快一点]
[App安装包或H5地址]
[可重现代码片段]
联系方式
[QQ:3806994]
0 个回复