Problems encountered

When using Bluetooth, we need to obtain the Mac address of bluetooth device. On Android devices, in the onBluetoothDeviceFound method, deviceId is the Mac address of the Bluetooth device. On iOS devices, deviceId is the UUID of a Bluetooth device. If we want to get a Mac address on an iOS device, we need to figure it out ourselves.

Solution

Through consulting some relevant materials, it is found that some Bluetooth devices have the service ID of 180A, and the characteristic value 2A23 in the service ID can obtain the Mac address of bluetooth devices. Refer to the following code for specific operations:

  function array2String(buffer) {
    let hexArr = Array.prototype.map.call(
      new Uint8Array(buffer),
      function (bit) {
        return ('00' + bit.toString(16)).slice(2 -)})return `${hexArr[7]}:${hexArr[6]}:${hexArr[5]}:${hexArr[2]}:${hexArr[1]}:${hexArr[0]}`
  }
// Connect to Bluetooth
wx.createBLEConnection({
  deviceId,
  success: res= > {
    // Get the service ID
    wx.getBLEDeviceServices({
      deviceId,
      success: res= > {
        let serviceId = ' '
        for (let i = 0, len = res.services.length; i < len; i++) {
          // Use the service ID containing 180A
          if (~res.services[i].uuid.indexOf('180A')) {
            serviceId = res.services[i].uuid
            // Get the corresponding eigenvalue
            wx.getBLEDeviceCharacteristics({
              deviceId,
              serviceId,
              success: res= > {
                let characteristicId = ' '
                for (let i = 0, len = res.characteristics.length; i < len; i++) {
                  // use eigenvalues that contain 2A23
                  if (~res.characteristics[i].uuid.indexOf('2A23')) {
                    characteristicId = res.characteristics[i].uuid
                  }
                }
                wx.readBLECharacteristicValue({
                  deviceId,
                  serviceId,
                  characteristicId,
                  success: res= > {
                    console.log('mac read ----------', res)
                  }
                })
                wx.notifyBLECharacteristicValueChange({
                  deviceId,
                  serviceId,
                  characteristicId,
                  state: true,
                })
                wx.onBLECharacteristicValueChange(function (characteristic) {
                  // When the eigenvalue is to query the Mac address
                  if (~characteristic.characteristicId.indexOf('2A23')) {
                    let macInfo = (array2String(characteristic.value)).toUpperCase()
                    console.log('mac info -----------', macInfo)
                  }
                })
              }
            })
          }
        }
      }
    })
  }
})
Copy the code

subsequent

After obtaining the Mac address of a Bluetooth device, we generally continue to use Bluetooth. In this case, the service ID of the Bluetooth device and the new eigenvalue need to be obtained again.

wx.getBLEDeviceCharacteristics({
  deviceId,
  // Get the new service ID from getBLEDeviceServices
  serviceId: newServiceId,
  success: res= > {
    wx.notifyBLECharacteristicValueChange({
      state: true,
      deviceId,
      serviceId: newServiceId,
      // New eigenvalues
      characteristicId: newCharacteristicId
    })
    // Listen for low power Bluetooth connection error events
    wx.onBLEConnectionStateChange(res= >{})}})Copy the code