详细问题描述
(DCloud产品不会有明显的bug,所以你遇到的问题大都是在特定环境下才能重现的问题,请仔细描述你的环境和重现方式,否则DCloud很难排查解决你的问题)
[内容]
在安卓平台上先调用notifyBLECharacteristicValueChange然后再执行writeBLECharacteristicValue,但在onBLECharacteristicValueChange里监听不到返回的结果,在iOS里是没有问题的;执行notifyBLECharacteristicValueChange方法返回成功了,但是硬件端说notify没有开启所以无法收到回调信息
重现步骤
[步骤]
plus.bluetooth.openBluetoothAdapter
plus.bluetooth.getBluetoothAdapterState
plus.bluetooth.startBluetoothDevicesDiscovery
plus.bluetooth.getBluetoothDevices
plus.bluetooth.createBLEConnection
plus.bluetooth.getBLEDeviceServices
plus.bluetooth.getBLEDeviceCharacteristics
plus.bluetooth.notifyBLECharacteristicValueChange
plus.bluetooth.onBLECharacteristicValueChange
plus.bluetooth.writeBLECharacteristicValue
[结果]
15:23:55.780 打开蓝牙适配器成功: {"code":0,"message":"ok"} at html/bletest.html:51
15:23:55.802 状态成功: {"discovering":false,"available":true} at html/bletest.html:63
15:23:55.823 开始搜索成功: {"code":0,"message":"ok"} at html/bletest.html:75
15:23:55.846 获取成功: undefined at html/bletest.html:88
15:23:55.896 发现新设备: undefined at html/bletest.html:103
15:23:55.921 0: {"deviceId":"CB:6E:0A:DF:A1:55","name":"everyoo_ble","RSSI":-77,"localName":"everyoo_ble","advertisServiceUUIDs":["0000FEA0-0000-1000-8000-00805F9B34FB"],"advertisData":{}} at html/bletest.html:105
15:23:55.946 发现新设备: undefined at html/bletest.html:103
15:23:55.975 0: {"deviceId":"3E:25:4B:49:69:25","name":"","RSSI":-87,"localName":"","advertisServiceUUIDs":[],"advertisData":{}} at html/bletest.html:105
15:23:55.996 发现新设备: undefined at html/bletest.html:103
15:23:56.017 0: {"deviceId":"4B:6C:D7:56:E0:7F","name":"","RSSI":-71,"localName":"","advertisServiceUUIDs":[],"advertisData":{}} at html/bletest.html:105
15:23:56.038 发现新设备: undefined at html/bletest.html:103
15:23:56.059 0: {"deviceId":"44:82:9C:E5:1B:50","name":"","RSSI":-86,"localName":"","advertisServiceUUIDs":[],"advertisData":{}} at html/bletest.html:105
15:23:56.102 发现新设备: undefined at html/bletest.html:103
15:23:56.123 0: {"deviceId":"38:0A:C8:43:D3:AC","name":"","RSSI":-74,"localName":"","advertisServiceUUIDs":[],"advertisData":{}} at html/bletest.html:105
15:23:56.165 发现新设备: undefined at html/bletest.html:103
15:23:56.186 0: {"deviceId":"7F:A1:B8:51:3A:27","name":"","RSSI":-72,"localName":"","advertisServiceUUIDs":[],"advertisData":{}} at html/bletest.html:105
15:23:56.226 发现新设备: undefined at html/bletest.html:103
15:23:56.248 0: {"deviceId":"DA:CC:F9:F2:72:A4","name":"everyoo_ble2","RSSI":-79,"localName":"everyoo_ble2","advertisServiceUUIDs":["0000FEA0-0000-1000-8000-00805F9B34FB"],"advertisData":{}} at html/bletest.html:105
15:23:59.277 连接成功: {"code":0,"message":"ok"} at html/bletest.html:115
15:23:59.302 停止搜索成功: {"code":0,"message":"ok"} at html/bletest.html:128
15:24:00.746 xxxxxxxxxxxxxxxxxxx at html/bletest.html:193
15:24:00.768 [object ArrayBuffer] at html/bletest.html:221
15:24:00.788 DA:CC:F9:F2:72:A4 at html/bletest.html:222
15:24:00.808 get services success: 3 at html/bletest.html:142
15:24:00.829 0: {"uuid":"00001800-0000-1000-8000-00805F9B34FB","isPrimary":true} at html/bletest.html:144
15:24:00.852 1: {"uuid":"00001801-0000-1000-8000-00805F9B34FB","isPrimary":true} at html/bletest.html:144
15:24:00.873 2: {"uuid":"0000FEA0-0000-1000-8000-00805F9B34FB","isPrimary":true} at html/bletest.html:144
15:24:00.894 get characteristics success: 3 at html/bletest.html:157
15:24:00.920 0: {"uuid":"0000FEA1-0000-1000-8000-00805F9B34FB","properties":{"read":false,"write":true,"notify":true,"indicate":false}} at html/bletest.html:159
15:24:00.942 1: {"uuid":"0000FEA2-0000-1000-8000-00805F9B34FB","properties":{"read":false,"write":false,"notify":true,"indicate":false}} at html/bletest.html:159
15:24:00.963 2: {"uuid":"0000FFA0-0000-1000-8000-00805F9B34FB","properties":{"read":false,"write":true,"notify":true,"indicate":false}} at html/bletest.html:159
15:24:00.996 notify启用成功: at html/bletest.html:180
15:24:01.017 写入成功: {"code":0,"message":"ok"} at html/bletest.html:229
[期望]
正确的结果应该是在plus.bluetooth.writeBLECharacteristicValue之后,在onBLECharacteristicValueChange返回结果,在iOS平台里也是这样运行的没有问题,但在安卓里在调用 uni.writeBLECharacteristicValue后在uni.onBLECharacteristicValueChange没有任何结果返回,硬件端人员说notify没有开启,但是从日志看notify是启用成功的
[如果语言难以表述清晰,拍一个视频或截图,有图有真相]
IDE运行环境说明
[HBuilder 或 HBuilderX。如果你用其他工具开发uni-app,也需要在此说明]
HBuilderX
[IDE版本号]
2.2.2.20190816
[windows版本号]
[mac版本号]
Mac os mojave 10.14.4
uni-app运行环境说明
[运行端是h5或app或某个小程序?]
app
[运行端版本号]
[项目是cli创建的还是HBuilderX创建的?如果是cli创建的,请更新到最新版cli再试]
HBuilderX创建
[编译模式是老模板模式还是新的自定义组件模式?]
App运行环境说明
[Android版本号]
安卓6.0
[iOS版本号]
[手机型号]
华为畅享6s
[模拟器型号]
附件
[IDE问题请提供HBuilderX运行日志。菜单帮助-查看运行日志,点右键打开文件所在目录,将log文件压缩成zip包上传]
[App问题请提供可重现问题的代码片段,你补充的细一点,问题就解决的快一点]
[App安装包或H5地址]
[可重现代码片段]
document.addEventListener('plusready', function(){
console.log('111');
if (plus.os.name == "Android") {
devid = "DA:CC:F9:F2:72:A4";
}
openble();
blestate();
listenerDeviceFound();
bleDiscovery();
bleDevices();
setTimeout(function() {
bleConnect();
}, 2300);
setTimeout(function() {
bleService();
blenotify();
blevaluechange();
blewrite();
}, 5000);
});
//1打开蓝牙
function openble() {
plus.bluetooth.openBluetoothAdapter({
success:function(e){
console.log('打开蓝牙适配器成功: '+JSON.stringify(e));
},
fail:function(e){
console.log('打开蓝牙适配器失败: '+JSON.stringify(e));
}
});
}
//2获取蓝牙状态
function blestate() {
plus.bluetooth.getBluetoothAdapterState({
success:function(e){
console.log('状态成功: '+JSON.stringify(e));
},
fail:function(e){
console.log('状态失败: '+JSON.stringify(e));
}
});
}
//3搜索蓝牙设备
function bleDiscovery() {
plus.bluetooth.startBluetoothDevicesDiscovery({
success:function(e){
console.log('开始搜索成功: '+JSON.stringify(e));
},
fail:function(e){
console.log('开始搜索失败: '+JSON.stringify(e));
}
});
}
// 4获取已搜索到的蓝牙设备
function bleDevices(){
plus.bluetooth.getBluetoothDevices({
success:function(e){
var devices = e.devices;
console.log('获取成功: '+e.length);
for(var i in devices){
console.log(i+': '+JSON.stringify(devices[i]));
}
},
fail:function(e){
console.log('获取失败: '+JSON.stringify(e));
}
});
}
// 监听发现新设备
function listenerDeviceFound(){
plus.bluetooth.onBluetoothDeviceFound(function(e){
var devices = e.devices;
console.log('发现新设备: '+e.length);
for(var i in devices){
console.log(i+': '+JSON.stringify(devices[i]));
}
});
}
// 5连接蓝牙设备
function bleConnect(){
plus.bluetooth.createBLEConnection({
deviceId:devid,
success:function(e){
console.log('连接成功: '+JSON.stringify(e));
stopbleConnect();
},
fail:function(e){
console.log('连接失败: '+JSON.stringify(e));
}
});
}
// 6停止搜索蓝牙设备
function stopbleConnect(){
plus.bluetooth.stopBluetoothDevicesDiscovery({
success:function(e){
console.log('停止搜索成功: '+JSON.stringify(e));
},
fail:function(e){
console.log('停止搜索失败: '+JSON.stringify(e));
}
});
}
// 7获取服务和特征值
function bleService(){
plus.bluetooth.getBLEDeviceServices({
deviceId:devid,
success:function(e){
var services = e.services;
console.log('get services success: '+services.length);
for(var i in services){
console.log(i+': '+JSON.stringify(services[i]));
}
},
fail:function(e){
console.log('get services failed: '+JSON.stringify(e));
}
});
plus.bluetooth.getBLEDeviceCharacteristics({
deviceId:devid,
serviceId:"0000FEA0-0000-1000-8000-00805F9B34FB",
success:function(e){
var characteristics = e.characteristics;
console.log('get characteristics success: '+characteristics.length);
for(var i in characteristics){
console.log(i+': '+JSON.stringify(characteristics[i]));
}
},
fail:function(e){
console.log('get characteristics failed: '+JSON.stringify(e));
}
});
}
// 9启用notify
function blenotify(){
plus.bluetooth.notifyBLECharacteristicValueChange({
state: true,
deviceId:devid,
serviceId:"0000FEA0-0000-1000-8000-00805F9B34FB",
characteristicId:"0000FFA0-0000-1000-8000-00805F9B34FB",
// serviceId:"0000FFA0-0000-1000-8000-00805F9B34FB",
// characteristicId:"0000FEA0-0000-1000-8000-00805F9B34FB",
success:function(e){
var characteristics = e.characteristics;
console.log('notify启用成功: ');
for(var i in characteristics){
console.log(i+': '+JSON.stringify(characteristics[i]));
}
},
fail:function(e){
console.log('notify启用失败: '+JSON.stringify(e));
}
});
}
// 10监听返回
function blevaluechange(){
console.log('xxxxxxxxxxxxxxxxxxx');
// 监听低功耗蓝牙设备的特征值变化
plus.bluetooth.onBLECharacteristicValueChange(function(e){
console.log("监听到XXXXXXXXXXXXX");
console.log(e.value);
console.log('onBLECharacteristicValueChange: '+JSON.stringify(e));
console.log(JSON.stringify(e.value));
var value = tools.hex2buff(e.value).trim();
var value1 = tools.ab2str(e.value);
console.log('value(hex) = '+value);
console.log('value(hex1) = '+value1);
if(characteristicId == e.characteristicId){
// 更新到页面显示
alert('特征值变化: '+value);
}
});
}
// 11发送数据
function blewrite(){
// 要写入的数据
// var value = new ArrayBuffer(8);
// var iv = new Int8Array(value);
// iv[0] = 123;
// 要写入的数据
var value = new ArrayBuffer(4);
var iv = new Int32Array(value);
iv[0] = 8;
console.log(value);
console.log(devid);
plus.bluetooth.writeBLECharacteristicValue({
deviceId:devid,
serviceId:"0000FEA0-0000-1000-8000-00805F9B34FB",
characteristicId:"0000FFA0-0000-1000-8000-00805F9B34FB",
value:value,
success:function(e){
console.log('写入成功: '+JSON.stringify(e));
},
fail:function(e){
console.log('写入失败: '+JSON.stringify(e));
}
});
}
// 12读取低功耗蓝牙设备的特征值
function bleRead(){
plus.bluetooth.readBLECharacteristicValue({
deviceId:devid,
serviceId:"0000FEA0-0000-1000-8000-00805F9B34FB",
characteristicId:"0000FFA0-0000-1000-8000-00805F9B34FB",
success:function(e){
console.log('读取成功: '+JSON.stringify(e));
},
fail:function(e){
console.log('读取失败: '+JSON.stringify(e));
}
});
}
联系方式
[QQ]
1***@163.com
请问您解决了吗?遇到了同样的问题
2020-01-07 14:52