微信小程序 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)
                        }
                    }
                })
            }
        }

 

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值