Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Android: Fixed parsing of CHARACTERISTIC_DATABASE_HASH (#1232)

Michael Rademakere57de0059907
diff --git a/console/android/ORLib/src/main/java/io/openremote/orlib/service/BleProvider.kt b/console/android/ORLib/src/main/java/io/openremote/orlib/service/BleProvider.kt
index a072b964a..0a044c874 100644
--- a/console/android/ORLib/src/main/java/io/openremote/orlib/service/BleProvider.kt
+++ b/console/android/ORLib/src/main/java/io/openremote/orlib/service/BleProvider.kt
@@ -24,17 +24,22 @@ class BleProvider(val context: Context) {
private const val bleDisabledKey = "bleDisabled"
private const val version = "ble"
+ // BLE UUIDs according to
+ // https://www.bluetooth.com/wp-content/uploads/Files/Specification/HTML/Assigned_Numbers/out/en/Assigned_Numbers.pdf?v=1706541022964
+
private val SERVICE_GENERIC_ACCESS = UUID.fromString("00001800-0000-1000-8000-00805f9b34fb")
private val SERVICE_GENERIC_ATTRIBUTE =
UUID.fromString("00001801-0000-1000-8000-00805f9b34fb")
private val CHARACTERISTIC_SERVICE_CHANGED =
- UUID.fromString("00002a05-0000-1000-8000-00805f9b34fb");
+ UUID.fromString("00002a05-0000-1000-8000-00805f9b34fb")
private val CHARACTERISTIC_DEVICE_NAME =
- UUID.fromString("00002a00-0000-1000-8000-00805f9b34fb");
+ UUID.fromString("00002a00-0000-1000-8000-00805f9b34fb")
private val CHARACTERISTIC_APPEARANCE =
- UUID.fromString("00002a01-0000-1000-8000-00805f9b34fb");
+ UUID.fromString("00002a01-0000-1000-8000-00805f9b34fb")
private val CHARACTERISTIC_CENTRAL_ADDRESS_RESOLUTION =
- UUID.fromString("00002aa6-0000-1000-8000-00805f9b34fb");
+ UUID.fromString("00002aa6-0000-1000-8000-00805f9b34fb")
+ private val CHARACTERISTIC_DATABASE_HASH =
+ UUID.fromString("00002b2a-0000-1000-8000-00805f9b34fb")
// This is the max MTU size supported by Android
private const val GATT_MAX_MTU_SIZE = 517
@@ -264,10 +269,9 @@ class BleProvider(val context: Context) {
value: ByteArray? = null
) {
when (status) {
- BluetoothGatt.GATT_SUCCESS -> handleSuccess(
- gatt,
- readCharacteristic,
- value ?: readCharacteristic.value
+ BluetoothGatt.GATT_SUCCESS -> Log.d(
+ "BluetoothGattCallback",
+ "Characteristic read success for ${readCharacteristic.uuid}"
)
BluetoothGatt.GATT_READ_NOT_PERMITTED -> Log.e(
@@ -280,24 +284,23 @@ class BleProvider(val context: Context) {
"Characteristic read failed for ${readCharacteristic.uuid}, error: $status"
)
}
- }
-
- private fun handleSuccess(
- gatt: BluetoothGatt?,
- readCharacteristic: BluetoothGattCharacteristic,
- value: ByteArray
- ) {
val deviceCharacteristic =
deviceCharacteristics.find { it.characteristic.uuid == readCharacteristic.uuid }
- deviceCharacteristic?.also {
- it.value = String(value).substringBefore('\u0000')
+ deviceCharacteristic?.also { attribute ->
+ if (deviceCharacteristic.characteristic.uuid == CHARACTERISTIC_DATABASE_HASH) {
+ attribute.value = value?.toHexString()
+ } else {
+ (value ?: readCharacteristic.value)?.let {
+ attribute.value = String(it).substringBefore('\u0000')
+ }
+ }
}
Log.d(
"BluetoothGattCallback",
"Read characteristic ${readCharacteristic.uuid}:\n${
- String(
- value
- )
+ value?.let {
+ String(it)
+ } ?: "null"
}"
)
handleDeviceIndex(gatt)
@@ -312,11 +315,11 @@ class BleProvider(val context: Context) {
"action" to "CONNECT_TO_DEVICE",
"provider" to "ble",
"success" to true,
-// "data" to hashMapOf("attributes" to deviceCharacteristics.map {
-// createAttributeMap(
-// it
-// )
-// })
+ "data" to hashMapOf("attributes" to deviceCharacteristics.map {
+ createAttributeMap(
+ it
+ )
+ })
)
)
}
@@ -330,7 +333,6 @@ class BleProvider(val context: Context) {
"value" to deviceCharacteristic.value
)
-
override fun onCharacteristicWrite(
gatt: BluetoothGatt?,
characteristic: BluetoothGattCharacteristic?,
@@ -530,3 +532,7 @@ fun BluetoothGattCharacteristic.isWritableWithoutResponse(): Boolean =
fun BluetoothGattCharacteristic.containsProperty(property: Int): Boolean {
return properties and property != 0
}
+
+fun ByteArray.toHexString(): String {
+ return joinToString("") { "%02x".format(it) }
+}