代码之家  ›  专栏  ›  技术社区  ›  Martin Erlic

如何使用响应式蓝牙订阅同步更改片段用户界面元素?

  •  0
  • Martin Erlic  · 技术社区  · 5 年前

    /* Buttons */
    val homeFragment = HomeFragment.newInstance()
    
    /* Buttons */
    val enableBluetooth = homeFragment.view ? .findViewById < TextView > (R.id.shotCountTextView)
    val deviceConnected = homeFragment.view ? .findViewById < Button > (R.id.deviceConnected)
    
    /* Monitor Connection State Changes */
    rxBleDevice.observeConnectionStateChanges()
     .subscribe({
      connectionState ->
      Log.d("Connection State: ", "$connectionState")
    
      if (connectionState == RxBleConnection.RxBleConnectionState.CONNECTED) { // fixed the check
    
       runOnUiThread {
    
        enableBluetooth ? .setBackgroundResource(R.drawable.bluetooth_on) // Change image
        deviceConnected ? .setText(R.string.connected_to_hooplight) // Changed text
    
       }
    
      } else {
    
       runOnUiThread {
    
        enableBluetooth ? .setBackgroundResource(R.drawable.bluetooth_off) // Change image
        deviceConnected ? .setText(R.string.connect_to_hooplight) // Changed text
    
       }
      }
     }, {
      throwable -> Log.d("Error: ", throwable.toString())
     })
    
    
    }, {
     throwable ->
     // Handle an error here.
     println("Scan Error: $throwable")
    }
    

    日志:

    D/BluetoothGatt: connect() - device: 34:81:F4:3C:2D:7B, auto: true
        registerApp()
        registerApp() - UUID=98485e50-4fb3-475c-beb1-22609fe118fb
    D/BluetoothGatt: onClientRegistered() - status=0 clientIf=9
    D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=9 device=34:81:F4:3C:2D:7B
    D/Connection State:: RxBleConnectionState{CONNECTED}
        RxBleConnectionState{CONNECTED}
    D/establishConnection:: Connection established
    
    0 回复  |  直到 5 年前