wx.startBluetoothDevicesDiscovery({
            allowDuplicatesKey: false, // 是否允许重复上报同一设备
            success:(res)=>{
                wx.showLoading({
                    title: '正在搜索附近蓝牙'
                })
                getBluetooth.call(this);
            },
            fail: (res)=>{
                wx.showToast({
                    title: '蓝牙搜索错误',
                    icon: 'none',
                    duration: 1000
                })
            }
 }) 
function getBluetooth() {
            let ofSearchBt = false,time = 2000,timeout = 10000;
            let timer = setInterval(()=>{
                wx.getBluetoothDevices({
                    success: (res)=> {
                        for (let i = 0; i < res.devices.length; i++) {
                            if(res.devices[i].name==this.data.bluetoothName){ //bluetoothName为要连接的蓝牙名称
                                ofSearchBt = true;
                                clearInterval(timer);
                                wx.stopBluetoothDevicesDiscovery(); // 停止搜索
                                wx.hideLoading();
//重点 根据advertisData 取出mac进行拼接
                              let bf = res.devices[i].advertisData.slice(4, 10);
                              let mac = Array.prototype.map.call(new Uint8Array(bf), x => ('00' + x.toString(16)).slice(-2)).join(':');
                              mac = mac.toUpperCase()
                                this.setData({
                                    deviceId: res.devices[i].deviceId,
                                    mac: mac.replace(/:/g, "")
                                })
                                console.log(mac)
                                createConnectBt.call(this)
                                break;
                            }
                        }
                    }
                })
            },time);
            setTimeout(()=> {
                if(!ofSearchBt){
                    clearInterval(timer);
                    wx.stopBluetoothDevicesDiscovery();
                    wx.hideLoading();
                    wx.showModal({
                        title: '提示',
                        content: '搜索蓝牙超时',
                        confirmText: '重新搜索',
                        success: (res)=> {
                            if(res.confirm){
                                getBluetooth.call(this)
                            }
                        }
                    })
                }
            },timeout)
            function createConnectBt(){
                wx.showLoading({
                    title: '正在连接蓝牙'
                })
                wx.createBLEConnection({
                    deviceId: this.data.deviceId,
                    timeout: timeout,
                    success: (res)=> {
                        wx.hideLoading();
                        if(res.errCode == 0){
                            this.setData({
                                isConnectBluetooth: true
                            })
                            wx.showToast({
                              title: '连接成功'//提示文字
                            })
                          console.log("deviceId:" + this.data.deviceId)
                            // this.getMacinfo()//获取macinfo
                            // this.requestCreateOrder()
                        }else{
                            _showModal.call(this,'不能正常连接')
                        }
                    },
                    fail: (res) => {
                      wx.hideLoading();
                        if(res.errCode == 10012){
                            _showModal.call(this,'连接超时')
                        }else{
                            _showModal.call(this,'连接错误')
                        }
                    },
                    complete: res => {
                      // wx.hideLoading();
                    }
                })
            }
            function _showModal(content){
                wx.showModal({
                    title: '提示',
                    content: content,
                    confirmText: '重新连接',
                    success: (res)=> {
                        if(res.confirm){
                            createConnectBt.call(this)
                        }
                    }
                })
            }
        }

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐