繁体   English   中英

在写入特征时反应本机 BLE 管理器(Android)状态代码 14

[英]React Native BLE Manager (Android) status code 14 on write to characteristic

我正在使用 react native ble manager package 来构建一个 react native 应用程序,该应用程序通过 BLE 与 python 客户端通信。

写入 Android 上的特性时(此错误似乎未出现在 IOS 上)写入成功但不久后我收到此错误:

ERROR Error writing eeee2a38-0000-1000-8000-00805f9b34fb status=14

这是在 Android 端处理连接、通知和写入的简化代码:

import { NativeModules, NativeEventEmitter, Platform } from 'react-native'
import BleManager, { Peripheral } from 'react-native-ble-manager'
import { END } from 'redux-saga'
import { bytesToString } from 'convert-string'

const UPDATE_SERVICE_UUID = '0000180d-aaaa-1000-8000-00805f9b34fb'

export const Characteristic =
   {
        WIFI_STATUS_UUID: 'bbbb2a38-0000-1000-8000-00805f9b34fb',
        WIFI_CREDS_UUID: 'aaaa2a38-0000-1000-8000-00805f9b34fb',
        VERSION_UUID: 'cccc2a38-0000-1000-8000-00805f9b34fb',
        UPDATE_STATUS_UUID: 'dddd2a38-0000-1000-8000-00805f9b34fb',
        DO_UPDATE_UUID: 'eeee2a38-0000-1000-8000-00805f9b34fb',
        ERROR_UUID: 'ffff2a38-0000-1000-8000-00805f9b34fb',
      }

class BLEManager {
  bleManagerModule: any
  bleManagerEmitter: any
  scanning: boolean
  dispatch: any
  stopScanListener: any
  peripheralDiscoverListener: any
  characteristicUpdateListener: any
  onDisconnectListener: any
  connectTimeout: any

  constructor() {
    BleManager.start({ showAlert: false })

    this.bleManagerModule = NativeModules.BleManager
    this.bleManagerEmitter = new NativeEventEmitter(this.bleManagerModule)
    this.scanning = false
  }

  startScan = (onPeripheralFound: (peripheral: Peripheral | null) => void) => {
    if (!this.scanning) {
      BleManager.scan([], 3, true)
        .then(() => {
          console.log('Scanning...')
          this.scanning = true
          this.peripheralDiscoverListener = this.bleManagerEmitter.addListener(
            'BleManagerDiscoverPeripheral',
            onPeripheralFound,
          )

          this.stopScanListener = this.bleManagerEmitter.addListener(
            'BleManagerStopScan',
            () => {
              onPeripheralFound(END)
            },
          )
          return
        })
        .catch(err => {
          console.error(err)
        })
    } else {
      console.log('already scanning')
    }
    return () => {
      console.log('stopped scanning')
      this.peripheralDiscoverListener.remove()
      this.stopScanListener.remove()
    }
  }

  getBondedDevices = (onGetBondedPeripherals: any) => {
    BleManager.getBondedPeripherals().then(bondedPeripheralsArray => {
      onGetBondedPeripherals(bondedPeripheralsArray)
      // TODO: is the END message here necessary?
      onGetBondedPeripherals(END)
      return
    })
    return () => {}
  }

  connectToPeripheral = async (peripheralID: string) => {
    try {
      await new Promise(async (resolve, reject) => {
        this.connectTimeout = setTimeout(reject, 3000)

        console.log('connecting to ' + peripheralID)

        try {
          await BleManager.connect(peripheralID)
          await BleManager.retrieveServices(peripheralID)
        } catch (error) {
          reject()
        }

        if (this.connectTimeout) {
          clearTimeout(this.connectTimeout)

          this.connectTimeout = null

          this.onDisconnectListener = this.bleManagerEmitter.addListener(
            'BleManagerDisconnectPeripheral',
            this.onDisconnectPeripheral,
          )

          resolve()
        }
      })
    } catch (err) {
      clearTimeout(this.connectTimeout)

      this.connectTimeout = null

      console.error('Could not connect to device.')

      throw new Error(err)
    }

    return
  }

  watchForCharacteristicsUpdates = async (
    updateCharValue: (arg0: { payload: any }) => void,
    peripheralID: string,
  ) => {
    try {
      await BleManager.startNotification(
        peripheralID,
        UPDATE_SERVICE_UUID,
        Characteristic.ERROR_UUID,
      )
      await BleManager.startNotification(
        peripheralID,
        UPDATE_SERVICE_UUID,
        Characteristic.VERSION_UUID,
      )
      await BleManager.startNotification(
        peripheralID,
        UPDATE_SERVICE_UUID,
        Characteristic.UPDATE_STATUS_UUID,
      )
    } catch (e) {
      updateCharValue(new Error(e))
      console.error(e)
    }

    console.log('watch for notifications')

    this.characteristicUpdateListener = this.bleManagerEmitter.addListener(
      'BleManagerDidUpdateValueForCharacteristic',
      ({ value, characteristic }) => {
        // Convert bytes array to string
        const data = bytesToString(value)

        console.log(
          `Received ${data} (${value}) for characteristic ${characteristic}`,
        )

        updateCharValue({
          payload: {
            characteristic: characteristic,
            data: data,
          },
        })
      },
    )
  }

  disconnectFromPeripheral = async (peripheralID: string) => {
    await BleManager.disconnect(peripheralID)
    this.characteristicUpdateListener.remove()
  }

  onDisconnectPeripheral = (peripheralID: string) => {
    console.log(peripheralID + ' disconnected')
    this.onDisconnectListener.remove()
  }

  checkIfConnected = async (peripheralID: string) => {
    return await BleManager.isPeripheralConnected(peripheralID, [])
  }

  triggerUpdateCheck = async (peripheralID: string) => {
    return await BleManager.write(
      peripheralID,
      UPDATE_SERVICE_UUID,
      Characteristic.WIFI_STATUS_UUID,
      [1],
    )
  }

  runUpdate = async (peripheralID: string) => {
    return await BleManager.write(
      peripheralID,
      UPDATE_SERVICE_UUID,
      Characteristic.DO_UPDATE_UUID,
      [1],
    )
  }
}

const bleManager = new BLEManager()

export default bleManager

我对此进行了一些研究,似乎有些人遇到了问题,但我找不到解释或解决方案。

我什至不确定从哪里开始调试。 欢迎任何建议。

细节:

  • 设备:[像素 6]
  • 操作系统:[Android 12]
  • react-native-ble-manager 版本:^8.4.1
  • react-native 版本:0.67.4

注意:我也在 Github 上问过这个问题:https://github.com/innoveit/react-native-ble-manager/issues/887

问题(如 Martijn 所述)是 Bluez 中的错误,该错误已在 5.65 中修复。 只需升级和清除蓝牙缓存即可修复它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM