|
1 | 1 | package com.viam.ble
|
2 | 2 |
|
| 3 | +import android.annotation.SuppressLint |
3 | 4 | import android.bluetooth.BluetoothDevice
|
4 | 5 | import android.bluetooth.BluetoothGatt
|
5 | 6 | import android.bluetooth.BluetoothGattCallback
|
6 | 7 | import android.bluetooth.BluetoothGattCharacteristic
|
| 8 | +import android.bluetooth.BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT |
7 | 9 | import android.bluetooth.BluetoothManager
|
8 | 10 | import android.bluetooth.BluetoothProfile
|
9 | 11 | import android.content.Context
|
@@ -103,6 +105,21 @@ class Peripheral(
|
103 | 105 | return char.value ?: throw Exception("characteristic $charId not yet read")
|
104 | 106 | }
|
105 | 107 |
|
| 108 | + @SuppressLint("MissingPermission") |
| 109 | + fun writeCharacteristic( |
| 110 | + serviceId: String, |
| 111 | + charId: String, |
| 112 | + data: ByteArray, |
| 113 | + ): Boolean { |
| 114 | + val svcUUID = UUID.fromString(serviceId) |
| 115 | + val charUUID = UUID.fromString(charId) |
| 116 | + val svc = gatt?.getService(svcUUID) ?: throw Exception("service $serviceId not found") |
| 117 | + val char = svc.getCharacteristic(charUUID) ?: throw Exception("characteristic $charId not found") |
| 118 | + char.setValue(data) |
| 119 | + char.writeType = WRITE_TYPE_DEFAULT |
| 120 | + return gatt?.writeCharacteristic(char) ?: throw Exception("gatt not available") |
| 121 | + } |
| 122 | + |
106 | 123 | @Throws(SecurityException::class)
|
107 | 124 | suspend fun close() {
|
108 | 125 | if (isClosed) {
|
@@ -230,27 +247,39 @@ class Peripheral(
|
230 | 247 | currentCharCont?.resume(Unit)
|
231 | 248 | currentCharCont = null
|
232 | 249 | discoveredSvcDoneCount++
|
| 250 | + val charUUID = characteristic.uuid.toString().lowercase() |
| 251 | + val charSvcUUID = |
| 252 | + characteristic.service.uuid |
| 253 | + .toString() |
| 254 | + .lowercase() |
233 | 255 | if (status != BluetoothGatt.GATT_SUCCESS) {
|
234 |
| - Log.d(TAG, "error getting characteristic ${characteristic.uuid}: $status") |
| 256 | + Log.d(TAG, "error getting characteristic $charUUID: $status") |
235 | 257 | } else {
|
236 | 258 | if (!discoveredCharacteristics.containsKey(characteristic.service.uuid.toString())) {
|
237 |
| - discoveredCharacteristics[characteristic.service.uuid.toString()] = mutableSetOf() |
| 259 | + discoveredCharacteristics[charSvcUUID] = mutableSetOf() |
238 | 260 | }
|
239 |
| - discoveredCharacteristics[characteristic.service.uuid.toString()]?.add(characteristic.uuid.toString()) |
| 261 | + discoveredCharacteristics[charSvcUUID]?.add(charUUID) |
240 | 262 | }
|
241 | 263 | if (discoveredSvcDoneCount == neededCharDoneCount) {
|
242 | 264 | _discoveredServices =
|
243 | 265 | gatt.services
|
244 |
| - .filter { discoveredCharacteristics.containsKey(it.uuid.toString()) } |
| 266 | + .filter { discoveredCharacteristics.containsKey(it.uuid.toString().lowercase()) } |
245 | 267 | .map { svc ->
|
246 | 268 | hashMapOf(
|
247 |
| - "id" to svc.uuid.toString(), |
| 269 | + "id" to svc.uuid.toString().lowercase(), |
248 | 270 | "characteristics" to
|
249 | 271 | svc.characteristics
|
250 |
| - .filter { discoveredCharacteristics[it.service.uuid.toString()]!!.contains(it.uuid.toString()) } |
251 |
| - .map { char -> |
| 272 | + .filter { |
| 273 | + discoveredCharacteristics[ |
| 274 | + it.service.uuid |
| 275 | + .toString() |
| 276 | + .lowercase(), |
| 277 | + ]!!.contains( |
| 278 | + it.uuid.toString().lowercase(), |
| 279 | + ) |
| 280 | + }.map { char -> |
252 | 281 | hashMapOf(
|
253 |
| - "id" to char.uuid.toString(), |
| 282 | + "id" to char.uuid.toString().lowercase(), |
254 | 283 | )
|
255 | 284 | },
|
256 | 285 | )
|
|
0 commit comments