微信小程序 uni-app蓝牙低功耗详细步骤

1.打开连接
onLoad: function (options) {
    wx.openBluetoothAdapter({
      success:(res)=>{
        console.log('蓝牙初始化',res)
        //搜索附近设备
        // wx.onBluetoothDeviceFound((result) => {
        //   console.log(result)
        // })
      },
      fail:(err)=>{
        console.log('初始化失败',err)
        wx.showToast({
          title: '蓝牙初始化失败',
          icon:'none'
        })
      }
    })
  },
2.搜索蓝牙设备
 //搜索蓝牙设备
    wx.startBluetoothDevicesDiscovery({
      allowDuplicatesKey: false,//是否允许重复上报同一设备
      success:(res)=>{
        console.log('开始搜索附近设备',res)
        this.getBluetoothDevices()
      },
      fail:(err)=>{
        console.log('没有找到设备')
      }
    })
3.获取蓝牙设备
getBluetoothDevices(){
   wx.getBluetoothDevices({
     success:(res)=>{
       console.log(res)
       if(res.devices.length==0){
         wx.showToast({
           title: '没有搜索到蓝牙设备',
           icon:'none'
         })
       }else{
         this.setData({devices:res.devices})
       }
     }
   })
 },
4.建立连接
 //建立连接
  connectDevice:function(e){
    console.log(e)
    let device=e.currentTarget.dataset.device
    wx.createBLEConnection({
      deviceId: device.deviceId,
      success:(res)=>{
        console.log('连接成功',res)
        wx.showToast({
          title: '连接成功',
          icon:'none'
        })
        this.getServiceId(device.deviceId)
      },
      fail:(err)=>{
        wx.showToast({
          title: '连接失败',
          icon:'none'
        })
      }
    })
  },
5.获取服务
getServiceId(deviceId){
    this.setData({deviceId:deviceId})
    wx.getBLEDeviceServices({
      deviceId: deviceId,
      success:(res)=>{
        console.log(res)
        console.log(deviceId)
        let item=res.services[1]
        this.setData({
          services:item.uuid
        })
        this.getCharacteId(deviceId,item.uuid)
      },
      fail:(err)=>{
        console.log(err)
      }
    })
  },
6.获取设备特征值
//获取设备特征值
  getCharacteId(deviceId,services){
    wx.getBLEDeviceCharacteristics({
      deviceId:deviceId,
      serviceId:services,
      success:(res)=>{
        console.log(res)
        this.setData({characteristicId:res.characteristics[0].uuid})
        this.startNotice(res.characteristics[0].uuid)
        for(let i=0;i<res.characteristics.length;i++){
          let item=res.characteristics[i]
          if(item.properties.indicate||item.properties.notify){
            this.setData({propertiesuuId:item.uuid})
            this.startNotice(this.data.propertiesuuId)
          }
          if(item.properties.write==true){
            this.setData({writeId:item.uuid})
          }
        }
      },
      fail:(err)=>{
        console.log(err)
      }
    })
  },
7.发送指令,读取蓝牙发送的消息
 //启用低功耗蓝牙设备特征值变化是的notify功能
  startNotice(uuid){
    wx.notifyBLECharacteristicValueChange({
      characteristicId: this.data.characteristicId,
      deviceId: this.data.deviceId,
      serviceId: this.data.serviceId,
      state: true,
      success:(res)=>{
        console.log(res)
        //开始监听发送值
        this.send()
      }
    })
  },
8.监听发送
send(){
   wx.onBLECharacteristicValueChange((res) => {
          console.log(res.value)
          let a=res.value
          let int8array=new Int8Array(a)
          console.log('监听到特征值更新',int8array[0])
          this.write(a)
        })
}
9.向特征值写入数据、读取数据
write(value){
    wx.writeBLECharacteristicValue({
      characteristicId: this.data.characteristicId,
      deviceId: this.data.deviceId,
      serviceId: this.data.serviceId,
      value: this.str2ab(value),
      success:(res)=>{
        console.log(res)
      },
      fail:(err)=>{
        console.log(err)
      }
    })
    wx.readBLECharacteristicValue({
      characteristicId: this.data.characteristicId,
      deviceId: this.data.deviceId,
      serviceId: this.data.serviceId,
      success:(res)=>{
        console.log(res)
      }
    })
  },
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值