其他分享
首页 > 其他分享> > web连接蓝牙电子秤navigator.bluetooth

web连接蓝牙电子秤navigator.bluetooth

作者:互联网

web连接蓝牙获取电子秤的重量

data(){
    return {
      bluetoothStatus:false,
      device:null,
      weight :null //获取到的重量
    }
}
async connectBluetooth() { // 连接蓝牙
      if (this.bluetoothStatus) {
        return alert('你已经连接了蓝牙')
      }
      const _this = this
      if ('bluetooth' in navigator) {
        console.log('浏览器支持bluetooth')
        // 打开蓝牙选择框
        _this.device = await navigator.bluetooth.requestDevice(_this.options)
        // 连接
        const server = await _this.device.gatt.connect()
        _this.blueGetweight(server)
      } else {
        alert('不支持蓝牙')
        _this.bluetoothStatus = false
      }
    },
    // 蓝牙获取重量
    async blueGetweight(server) {
      let characteristicId = ''
      // 获取uuid
      const servicesInfo = await server.getPrimaryServices()
      console.log(servicesInfo)
      const _this = this
      // 传入uuid
      const service = await server.getPrimaryService(servicesInfo[0].uuid)
      console.log(server)
      // 获取特征值列表
      const characteristicList = await service.getCharacteristics()
      console.log(characteristicList)
      characteristicList.forEach(item => {
        if (item.properties.notify) {
          characteristicId = item.uuid
        }
      })
      console.log(characteristicId)
      // 传入特征值
      const characteristic = await service.getCharacteristic(characteristicId)
      // 获取重量
      if (characteristic.properties.notify) {
        characteristic.addEventListener(
          'characteristicvaluechanged', e => {
            _this.bluetoothStatus = true
            var buffer = e.target.value.buffer
            var val = _this.ab2hex(buffer)
            var resValue = _this.hexCharCodeToStr(val)
            console.log(resValue)
            _this.weight = resValue
          }
        )
        await characteristic.startNotifications()
      } else {
        _this.bluetoothStatus = false
      }
    },
    // 关闭蓝牙
    blueDisconnect() {
      this.device.gatt.disconnect()
    },
    ab2hex(buffer) {
      const hexArr = Array.prototype.map.call(
        new Uint8Array(buffer),
        function(bit) {
          return ('00' + bit.toString(16)).slice(-2)
        }
      )
      return hexArr.join('')
    },
    // 十六进制转字符串,调整高低位
    hexCharCodeToStr(hexCharCodeStr) {
      const trimedStr = hexCharCodeStr.trim()
      const rawStr =
        trimedStr.substr(0, 2).toLowerCase() === '0x' ? trimedStr.substr(2) : trimedStr
      const len = rawStr.length
      if (len % 2 !== 0) {
        alert('Illegal Format ASCII Code!')
        return ''
      }
      let curCharCode
      const resultStr = []
      for (let i = 0; i < len; i = i + 2) {
        curCharCode = parseInt(rawStr.substr(i, 2), 16) // ASCII Code Value
        resultStr.unshift(String.fromCharCode(curCharCode))
      }
      return Math.round(parseFloat(resultStr.join('')) * 100) / 100
    },

标签:web,const,电子秤,await,蓝牙,bluetooth,return,console,server
来源: https://blog.csdn.net/MrHao_/article/details/121467343