android - 如何格式化要发送到 BLE 设备上的特征的字节数组?

标签 android kotlin bluetooth-lowenergy android-ble rxandroidble

我连接到我的设备并尝试写入其特性:

scanSubscription = rxBleClient.scanBleDevices(
  ScanSettings.Builder()
  // .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) // change if needed
  // .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES) // change if needed
  .build()
  // add filters if needed
 )
 .filter {
  scanResult -> scanResult.bleDevice.name == "MyDevice"
 } // Filter for devices named MyDevice
 .subscribe({
  scanResult ->
  // Print list of available devices
  println("Scan Result: ${scanResult.bleDevice.bluetoothDevice.name}")
  val charUUID = UUID.fromString("49535343-1e4d-4bd9-ba61-23c647249616")
  println("UUID: $charUUID")

  // Connect to MyDevice
  val macAddress = scanResult.bleDevice.bluetoothDevice.address //34:81:F4:55:04:9A
  println("MAC Address: $macAddress")
  val rxBleDevice = rxBleClient.getBleDevice(macAddress)

  val charset = Charsets.UTF_8
  val bytesToWrite = "cmdl000".toByteArray(charset)
  println("Bytes: $bytesToWrite")

  rxBleDevice.establishConnection(false)
  .flatMapSingle {
   rxBleConnection -> rxBleConnection.writeCharacteristic(charUUID, bytesToWrite)
  }

 }, {
  throwable ->
  // Handle an error here.
  println("Scan Error: $throwable")
 })

输出:

I/System.out: Scan Result: MyDevice
    UUID: 49535343-1e4d-4bd9-ba61-23c647249616
    MAC Address: 34:81:F4:55:04:9A
I/System.out: Bytes: [B@4973078

我不确定发送到我的特征的字节数组的格式是否正确。当我打印它时,我得到以下信息并且我的设备没有按照我的预期进行响应。这是正确的吗?

最佳答案

从上面的代码来看,你遇到的问题很简单。所有 Observable 对象都需要订阅才能真正执行。您已正确订阅扫描流程但未连接。

  rxBleDevice.establishConnection(false)
    .flatMapSingle {
      rxBleConnection -> rxBleConnection.writeCharacteristic(charUUID, bytesToWrite)
    }
    .subscribe(
      { /* done */ },
      { /* encountered error */ }
    )

您可以使用单个 .subscribe() 连接整个流程(扫描、连接、写入特性)。它可能看起来像:

val charUUID = UUID.fromString("49535343-1e4d-4bd9-ba61-23c647249616")
val bytesToWrite = "cmdl000".toByteArray(charset)

subscription = rxBleClient.scanBleDevices(
    ScanSettings.Builder().build(),
    ScanFilter.Builder().setDeviceName("MyDevice").build() // Filter for devices named MyDevice
)
    .take(1) // stop the scan when a matching device will be scanned for the first time
    .flatMap {
        val device = it.bleDevice
        println("MAC Address: ${device.macAddress}")
        device.establishConnection(false)
            .flatMapSingle { rxBleConnection -> rxBleConnection.writeCharacteristic(charUUID, bytesToWrite) }
            .take(1) // disconnect after the write
    }
    .subscribe(
        { /* written */ },
        { throwable ->
            // Handle an error here.
            println("Scan Error: $throwable")
        }
    )

关于android - 如何格式化要发送到 BLE 设备上的特征的字节数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59011095/

相关文章:

android - Activity 已泄漏 IntentReceiver 错误

android - 如何在 Google Maps API V2 中锁定相机

android - 使用 Dagger2 将适配器注入(inject) fragment

Kotlin:Ktor 如何将文本响应为 html

android - 设备正在与 GATT 服务器断开连接

android - 你能在Android 5.0中唯一标识一个BLE MAC地址吗?

java - android.app.ServiceConnectionLeaked : Activity . ..MainActivity 泄露了原来绑定(bind)在这里的 ServiceConnection ...MainActivity$1@e794142

android - 错误 : Could not find org. jetbrains.kotlin :kotlin-stdlib-jre8:1. 3.10。在 Android Studio 3.3 RC 1

java - 在springboot上使用elasticsearch查询

ios - 将应用程序从后台(iOS 7)置于前台