微信小程序 ios 获取蓝牙设备MAC地址

 

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)
                        }
                    }
                })
            }
        }

 

    原文作者:唉呀我擦
    原文地址: https://blog.csdn.net/tang430524/article/details/103241873
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞