XTiger
XTiger
  • 发布:2019-02-19 10:58
  • 更新:2019-04-22 17:24
  • 阅读:2729

【报Bug】蓝牙的几个bug

分类:uni-app

[内容]
一、IOS蓝牙notifyBLECharacteristicValueChange失效
之前在这里提交过bug,http://ask.dcloud.net.cn/question/63483蓝牙uni.notifyBLECharacteristicValueChange失败,换成plus.bluetooth.notifyBLECharacteristicValueChange,发现可以用,但是现在又发现只能在android平台正常运行,在IOS上不正常,在IOS上有几个问题:
1.如果返回两条结果,在plus.bluetooth.onBLECharacteristicValueChange中只能监听到最后一条,在安卓上是可以监听到两条的,也就是在安卓里plus.bluetooth.onBLECharacteristicValueChange会执行两次,而在IOS里plus.bluetooth.onBLECharacteristicValueChange只会执行一次,监听到的结果也是最后一条的结果;

  1. 在安卓里先建立监听plus.bluetooth.notifyBLECharacteristicValueChange,然后设立监听处理程序plus.bluetooth.onBLECharacteristicValueChange,再执行特征值写入uni.writeBLECharacteristicValue,这样是没有问题的,在plus.bluetooth.onBLECharacteristicValueChange可以监听到正确的结果,但是在IOS里这样监听不到结果,如果先执行uni.writeBLECharacteristicValue,然后再自执行plus.bluetooth.notifyBLECharacteristicValueChange,plus.bluetooth.onBLECharacteristicValueChange,则可以监听到结果,但监听到的是上一次执行uni.writeBLECharacteristicValue的结果,如果是第一次执行则也是监听不到结果的

二、plus.bluetooth.createBLEConnection中timeout属性失效
三、plus.bluetooth.getBluetoothDevices苹果手机蓝牙搜索到的设备返回数据不全,见:http://ask.dcloud.net.cn/question/64242
四、安卓里在初始蓝牙后直接执行plus.bluetooth.createBLEConnection,可以连接到蓝牙设备,但在IOS里不行,在IOS里要选执行uni.startBluetoothDevicesDiscovery然后再createBLEConnection才可用
五、createBLEConnection的success里马上执行uni.writeBLECharacteristicValue,一直失败,一定要用setTimeout延时200毫秒后才行,在很多API的success里马上执行其他蓝牙api都会有这样的问题,如:notifyBLECharacteristicValueChange

重现步骤
[步骤]
[结果]
[期望]
[如果语言难以表述清晰,可以拍一个视频或截图,有图有真相]

IDE运行环境说明
[HBuilderX]
[1.5.4]
[windows7]
[mac版本号]

App运行环境说明
[Android8]
[iOS9]
[iphone5]
[模拟器型号]

代码

function openBluetoothAdapter() {  
    writeSuccess = false;  
    devices = [];  
    platform = uni.getSystemInfoSync().platform.toLowerCase();  
    console.log("platform:" + platform);  
    var suuid = serviceUuid;  
    if (dotype == 'backdevice') {  
        suuid = '';  
    }  
    //console.log("suuid:" + suuid);  
    uni.openBluetoothAdapter({  
        success: function(e) {  
            if(dotype == 'backdevice' || platform == 'ios'){  
                var discoveryObj = {  
                    success: function(e) {  
                        listenerDeviceFound();  
                        intFindDevice = setTimeout(function(){  
                            if(devices.length == 0){  
                                fail({  
                                    code: 3,  
                                    msg: '未发现门锁信息',  
                                });  
                            }  
                        }, 10000);  
                    },  
                    fail: function(e) {  
                        fail({  
                            code: 2,  
                            msg: '蓝牙初始失败'  
                        });  
                    }  
                };  
                if(suuid != ''){  
                    discoveryObj.services = [suuid];  
                }  
                uni.startBluetoothDevicesDiscovery(discoveryObj);  
            }else{  
                createConnection();  
            }  

        },  
        fail: function(e) {  
            //console.log('open failed: ' + JSON.stringify(e));  
            fail({  
                code: 8,  
                msg: '蓝牙未开启,请打开蓝牙'  
            });  
        }  
    });  
}  

function listenerDeviceFound() {  
    console.log('device found');  
    var p = this;  
    uni.onBluetoothDeviceFound(function(e) {  
        devices = e.devices;  
        console.log('device found: ' + JSON.stringify(devices));  
        if (dotype == 'backdevice') {  
            if(onDeviceFind)  
                onDeviceFind(devices);  
            return;  
        }  
        for (var i in devices) {  
            uni.stopBluetoothDevicesDiscovery({  
                success(res) {  
                    //console.log("stop discovery:" + JSON.stringify(res));  
                }  
            });  
            lockNo = devices[i].deviceId;  
            if (dotype == 'open' || dotype == 'regdevice' || dotype == 'getsn') { //开门操作或门锁注册  
                createConnection();  
            } else if (dotype == 'record') { //打卡操作  
                success('record');  
            }  
        }  
    });  
}  

function createConnection() {  
    console.log("deviceId:" + lockNo);  
    plus.bluetooth.createBLEConnection({  
        deviceId: lockNo.toUpperCase(),  
        timeout: 3000,  
        success: function(e) {  
            console.log('create connection success: ' + JSON.stringify(e));  
            if(e.code != '0'){  
                fail({code: e.code, msg: e.message});  
                return;  
            }  
            setTimeout(function(){  
                if(platform == 'ios'){  
                    writeCharacteristics(bleCmd);  
                    setTimeout(function(){  
                        writeCharacteristics(bleCmd);  
                    }, 200);  
                }else{  
                    startCharacteristicsNotify();  
                }  
                //startCharacteristicsNotify();  
            }, 800);  
        },  
        fail: function(e) {  
            console.log('create connection failed: ' + JSON.stringify(e));  
            fail({  
                code: 5,  
                msg: '连接蓝牙失败'  
            });  
        },  
        complete(e) {  
            console.log('connection complete:' + JSON.stringify(e));  
        }  
    });  
}  

function reConnection(){  
    uni.closeBLEConnection({  
        deviceId: lockNo,  
        complete: function(e) {  
            createConnection();  
        }  
    });  
}  

function startCharacteristicsNotify() {  
    //监听特征值变化  
    var setNotify = false;  
    plus.bluetooth.notifyBLECharacteristicValueChange({  
        state: true,  
        deviceId: lockNo.toUpperCase(),  
        serviceId: serviceUuid.toUpperCase(),  
        characteristicId: characterUuid1.toUpperCase(),  
        success(e) {  
            console.log('set notify success: ' + JSON.stringify(e));  
            if(platform == 'android'){  
                setTimeout(function(){  
                    writeCharacteristics(bleCmd);  
                }, 200);  
            }  
        },  
        fail(e) {  
            console.log('set notify failed: ' + JSON.stringify(e));  
            fail({  
                code: 5,  
                msg: '建立特征值变化通知失败'  
            });  
        },  
        complete(e){  
            setNotify = true;  
            console.log('set notify complete: ' + JSON.stringify(e));  
        }  
    });  

    characteristicValueChange();  

    setTimeout(function(){  
        if(!setNotify){  
            reConnection();  
        }  
    }, 500);  
}  

function characteristicValueChange() {  
    plus.bluetooth.onBLECharacteristicValueChange(function(e) {  
        console.log('value:' + buf2hex(e.value).trim());  
        var result = buf2hex(e.value).trim();  
        success(result);  
    });  
}  

function writeCharacteristics(cmds) {  
    var writeResult = false;  
    for (var i = 0; i < cmds.length; i++) {  
        console.log(buf2hex(cmds[i]));  
        uni.writeBLECharacteristicValue({  
            deviceId: lockNo.toUpperCase(),  
            serviceId: serviceUuid.toUpperCase(),  
            characteristicId: characterUuid.toUpperCase(),  
            value: cmds[i],  
            success: function(e) {  
                console.log('write characteristics success: ' + JSON.stringify(e));  

                if(platform == 'ios'){  
                    startCharacteristicsNotify();  
                }  
            },  
            fail: function(e) {  
                console.log('write characteristics failed: ' + JSON.stringify(e));  
                //reConnection();  
                if(!writeSuccess){  
                    fail({  
                        code: 7,  
                        msg: '写入蓝牙特征值失败'  
                    });  
                }  
            },  
            complete() {  
                writeResult = true;  
            }  
        });  
    }  
    setTimeout(function(){  
        if(!writeResult){  
            reConnection();  
        }  
    }, 500);  
}  

function stopBluetoothDiscovery() {  
    //console.log('stop');  
    uni.stopBluetoothDevicesDiscovery({  
        success: function(e) {  

        },  
        fail(e) {  
            //complete(e);  
            //console.log('stop Discovery fail: ' + JSON.stringify(e));  
        }  
    });  
    uni.closeBluetoothAdapter({  
        success: function(e) {  
            //console.log('stop Discovery success: ' + JSON.stringify(e));  
            //complete(e);  
        },  
        fail(e) {  
            //complete(e);  
            //console.log('closeBluetoothAdapter fail: ' + JSON.stringify(e));  
        }  
    });  
    setTimeout(function() {  
        complete();  
    }, 200);  
}  

function closeConnection() {  
    uni.closeBLEConnection({  
        deviceId: lockNo,  
        success: function(e) {  
            //console.log('close connection success: ' + JSON.stringify(e));  
        },  
        fail: function(e) {  
            //console.log('close failed: ' + JSON.stringify(e));  
        }  
    });  
}  

function close() {  
    closeConnection();  
    setTimeout(function() {  
        stopBluetoothDiscovery();  
    }, 200);  
}

联系方式
[3806994]

2019-02-19 10:58 负责人:无 分享
已邀请:
CLP

CLP

四、安卓里在初始蓝牙后直接执行plus.bluetooth.createBLEConnection,可以连接到蓝牙设备,但在IOS里不行,在IOS里要选执行uni.startBluetoothDevicesDiscovery然后再createBLEConnection才可用 ---------iOS原生在第一次搜索到设备后,不调用关闭蓝牙模块的API的情况下,是可以直接连接蓝牙设备的,第一次搜索之后原生层有缓存了,由于平台特性,安卓可以直接连接设备

  • XTiger (作者)

    好的,谢谢,其他的几个问题麻烦也帮忙看看

    2019-03-12 12:50

  • XTiger (作者)

    http://ask.dcloud.net.cn/question/65820

    这个帖子里的bug也麻烦看看,这个比较严重,我的项目碰到这个问题现在做不下去了

    2019-03-12 13:26

CLP

CLP

三、plus.bluetooth.getBluetoothDevices苹果手机蓝牙搜索到的设备返回数据不全,见:http://ask.dcloud.net.cn/question/64242 ------下一个版本修复了

  • XTiger (作者)

    刚更新了1.6.8版本的IDE试了下,http://ask.dcloud.net.cn/question/65820这个问题还是没有解决

    2019-03-13 14:25

  • x***@163.com

    二.plus.bluetooth.createBLEConnection中timeout属性失效 这个问题有修复吗?

    帮忙看下这个问题: http://ask.dcloud.net.cn/question/71095

    2019-05-17 10:17

g***@163.com

g***@163.com

请问有解决方案了么?我这边也是蓝牙接收不到设备回复的数据

该问题目前已经被锁定, 无法添加新回复