Я использую библиотеку RxAndroidBle https://github.com/Polidea/RxAndroidBle для работы с любым устройством.Но у меня проблема с нахождением характеристикUuid .
Я делаю все из README, но не могу найти характеристикуUuid.В read () нужно выполнить .flatMapSingle { rxBleConnection -> rxBleConnection.readCharacteristic(characteristicUuid) }
, но нет характеристикаUuid .Где я могу взять это?
private lateinit var rxBleClient: RxBleClient
private lateinit var bleDevice: RxBleDevice
private lateinit var characteristicUuid: UUID
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
getLocationPermission()
rxBleClient = RxBleClient.create(this)
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
button.setOnClickListener { discover() }
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_OK) {
}
}
private lateinit var scanSubscription: Disposable
private fun discover() {
Log.i("BLE", "start scan")
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
)
.subscribe(
{ scanResult ->
bleDevice = scanResult.bleDevice
Log.i("BLE", "SCAN ${bleDevice.name} ${bleDevice.macAddress}")
connect()
scanSubscription.dispose()
// Process scan result here.
},
{ throwable ->
Log.i("BLE", "ERROR SCAN ${throwable.localizedMessage}")
// Handle an error here.
}
)
}
private lateinit var connectDisposable: Disposable
private fun connect() {
val device = bleDevice
connectDisposable = device.establishConnection(false) // <-- autoConnect flag
.subscribe(
{ rxBleConnection ->
Log.i("BLE", "CONNECT ${rxBleConnection.mtu}")
read(device)
connectDisposable.dispose()
// All GATT operations are done through the rxBleConnection.
},
{ throwable ->
Log.i("BLE", "ERROR CONNECT ${throwable.localizedMessage}")
// Handle an error here.
}
)
}
private fun read(device: RxBleDevice) {
val dis = device.establishConnection(false)
.flatMapSingle { rxBleConnection -> rxBleConnection.readCharacteristic(characteristicUuid) }
.subscribe(
{ characteristicValue ->
Log.i("BLE", "READ $characteristicValue")
// Read characteristic value.
},
{ throwable ->
Log.i("BLE", "ERROR READ ${throwable.localizedMessage}")
// Handle an error here.
}
)
}