小程序中,ios获取设备id的方法(通过广播获取mac地址的方法获取)

4 篇文章 0 订阅
2 篇文章 0 订阅

小程序中,ios获取设备id的方法(通过广播获取mac地址的方法获取)

在做物联网小程序时,由于硬件将设备id设置为mac地址后5位,iOS不能直接读到Mac地址,所以通过蓝牙广播来获取mac。

获取广播的mac:

wx.onBluetoothDeviceFound((result) => {
          // console.log("监听蓝牙新设备: ", result )
          let t = 0;
          let searchDevice = [];
          // TODO 在这可以判断是否已经搜素到,指定蓝牙。如果搜素到,则停止蓝牙搜素
          // console.log("监听寻找新设备的事件 - ", result.devices[0]);
          for(let i=0; i<result.devices.length; i++){
            if( !result.devices[i].deviceId || !result.devices[i].name ){
              return
            }
            if(this.data.searchBlueBase.length >0){
              for(let j=0,len = this.data.searchBlueBase.length; j< len; j++ ){
                if(result.devices[i].name == this.data.searchBlueBase[j].name){
                  return
                }
              } 
            }
            searchDevice[t++] = result.devices[i];
            console.log('发现的新设备')
            console.log('设备id - ' + result.devices[i].deviceId)
            console.log('设备name - ' + result.devices[i].name)

            if (result.devices[i].advertisData){
              console.log('IOS - 将UUID转化为设备ID')
              let bf = result.devices[i].advertisData.slice(2, 8);
              console.log('IOS - 获取所需数据', bf)
              console.log(this.ab2hex(result.devices[i].advertisData))
              
              let mac = Array.prototype.map
                .call(new Uint8Array(bf), (x) => ("00" + x.toString(16)).slice(-2))
                .join(":");
              mac = mac.toUpperCase();
              for (let i = 0;i<result.devices.length;i++){
                result.devices[0].macId = mac
              }
  
              console.log('IOS - UUID转化的mac地址为', mac)
              console.log(result.devices);
            }
          }
          var device=result.devices[0];
          if(device.name.indexOf('#')>-1){
            this.data.searchBlueBase.push(device);
          }
          this.setData({
            searchBlueBase: this.data.searchBlueBase
          })
          console.log("发现的新设备列表",this.data.searchBlueBase)
         
        })

ArrayBuffer转16进度字符串示例

// ArrayBuffer转16进度字符串示例
 ab2hex(buffer) {
  var hexArr = Array.prototype.map.call(
    new Uint8Array(buffer),
    function(bit) {
      return ('00' + bit.toString(16)).slice(-2)
    }
  )
  return hexArr.join('');
},
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值