tauri-plugin-blew 0.1.0-alpha.1

Tauri plugin for the blew BLE library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
package org.jakebot.blew

import android.annotation.SuppressLint
import android.bluetooth.*
import android.bluetooth.le.ScanCallback
import android.bluetooth.le.ScanFilter
import android.bluetooth.le.ScanResult
import android.bluetooth.le.ScanSettings
import android.content.Context
import android.os.ParcelUuid
import android.util.Log
import java.util.UUID
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.Semaphore
import java.util.concurrent.TimeUnit

/**
 * Singleton managing the Android BLE central role (scanner + GATT client).
 *
 * Kotlin methods are called from Rust via JNI. Android BLE callbacks are
 * forwarded to Rust via [external fun] JNI hooks.
 *
 * ## GATT operation serialization
 *
 * Android's [BluetoothGatt] allows only one in-flight operation at a time
 * (read, write, descriptor write, discover services, request MTU). Issuing
 * a second operation while one is pending causes the second to silently fail.
 *
 * We use a single global [Semaphore] with one permit to serialize all GATT
 * operations across all devices. Android's Bluetooth controller has a single
 * HCI command queue — concurrent operations on different [BluetoothGatt]
 * objects can silently fail. The semaphore is acquired before issuing an
 * operation and released in the corresponding callback (or immediately for
 * fire-and-forget operations like write-without-response).
 */
@SuppressLint("MissingPermission")
object BleCentralManager {
    private const val TAG = "BleCentralManager"

    // Status codes returned to Rust via JNI.
    private const val STATUS_SUCCESS = 0
    private const val STATUS_NOT_CONNECTED = 1
    private const val STATUS_CHAR_NOT_FOUND = 2
    private const val STATUS_GATT_BUSY = 3
    private const val STATUS_GATT_FAILED = 4

    private var context: Context? = null
    private var bluetoothManager: BluetoothManager? = null
    private var adapter: BluetoothAdapter? = null

    // Active GATT connections keyed by device address.
    private val gattConnections = ConcurrentHashMap<String, BluetoothGatt>()

    // Per-device MTU (default 23 until negotiated).
    private val mtuMap = ConcurrentHashMap<String, Int>()

    // Global GATT operation semaphore (1 permit = 1 op at a time across ALL
    // devices). Android's Bluetooth controller has a single HCI command queue;
    // concurrent operations on different BluetoothGatt objects can silently fail.
    private val gattSemaphore = Semaphore(1)

    // Devices whose global semaphore hold is for MTU negotiation (acquired in
    // onConnectionStateChange, released in onMtuChanged). Tracked so we can
    // release the semaphore on disconnect if MTU never completed.
    private val mtuHoldDevices: MutableSet<String> = ConcurrentHashMap.newKeySet()

    // Devices with a pending write-with-response. Used by onCharacteristicWrite
    // to avoid double-releasing the semaphore for write-without-response.
    private val pendingWriteResponse: MutableSet<String> = ConcurrentHashMap.newKeySet()

    // ── L2CAP state ──
    private val l2cap =
        L2capSocketManager(
            tag = TAG,
            onData = { socketId, data -> nativeOnL2capChannelData(socketId, data) },
            onClosed = { socketId -> nativeOnL2capChannelClosed(socketId) },
        )

    // ── JNI hooks (Kotlin → Rust) ──

    @JvmStatic
    external fun nativeOnDeviceDiscovered(
        deviceAddr: String,
        deviceName: String?,
        rssi: Int,
        serviceUuids: String,
    )

    @JvmStatic
    external fun nativeOnConnectionStateChanged(
        deviceAddr: String,
        connected: Boolean,
    )

    @JvmStatic
    external fun nativeOnServicesDiscovered(
        deviceAddr: String,
        servicesJson: String,
    )

    @JvmStatic
    external fun nativeOnCharacteristicRead(
        deviceAddr: String,
        charUuid: String,
        value: ByteArray,
        status: Int,
    )

    @JvmStatic
    external fun nativeOnCharacteristicWrite(
        deviceAddr: String,
        charUuid: String,
        status: Int,
    )

    @JvmStatic
    external fun nativeOnCharacteristicChanged(
        deviceAddr: String,
        charUuid: String,
        value: ByteArray,
    )

    @JvmStatic
    external fun nativeOnMtuChanged(
        deviceAddr: String,
        mtu: Int,
    )

    // ── L2CAP JNI hooks ──

    @JvmStatic
    external fun nativeOnL2capChannelOpened(
        deviceAddr: String,
        socketId: Int,
        fromServer: Boolean,
    )

    @JvmStatic
    external fun nativeOnL2capChannelData(
        socketId: Int,
        data: ByteArray,
    )

    @JvmStatic
    external fun nativeOnL2capChannelClosed(socketId: Int)

    @JvmStatic
    external fun nativeOnL2capChannelError(
        deviceAddr: String,
        errorMessage: String,
    )

    fun init(ctx: Context) {
        context = ctx
        bluetoothManager = ctx.getSystemService(Context.BLUETOOTH_SERVICE) as? BluetoothManager
        adapter = bluetoothManager?.adapter
        Log.d(TAG, "initialized, adapter=${adapter != null}")
    }

    // ── GATT operation serialization helpers ──

    /**
     * Acquire the global GATT semaphore, blocking up to [timeoutMs].
     * Returns true if acquired, false on timeout (GATT busy).
     */
    private fun acquireGatt(timeoutMs: Long = 5000): Boolean = gattSemaphore.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS)

    /** Release the global GATT semaphore after an operation completes. */
    private fun releaseGatt() {
        gattSemaphore.release()
    }

    // ── Scanning ──

    private var scanCallback: ScanCallback? = null

    @JvmStatic
    fun startScan(
        serviceUuids: Array<String>,
        lowPower: Boolean = false,
    ) {
        val scanner =
            adapter?.bluetoothLeScanner ?: run {
                Log.e(TAG, "scanner not available")
                return
            }

        stopScan()

        val filters =
            if (serviceUuids.isNotEmpty()) {
                serviceUuids.map { uuid ->
                    ScanFilter
                        .Builder()
                        .setServiceUuid(ParcelUuid(UUID.fromString(uuid)))
                        .build()
                }
            } else {
                null
            }

        val scanMode =
            if (lowPower) {
                ScanSettings.SCAN_MODE_LOW_POWER
            } else {
                ScanSettings.SCAN_MODE_LOW_LATENCY
            }
        val settings =
            ScanSettings
                .Builder()
                .setScanMode(scanMode)
                .build()

        scanCallback =
            object : ScanCallback() {
                override fun onScanResult(
                    callbackType: Int,
                    result: ScanResult,
                ) {
                    val device = result.device
                    val addr = device.address
                    val name = device.name
                    val rssi = result.rssi

                    val uuids =
                        result.scanRecord
                            ?.serviceUuids
                            ?.joinToString(",") { it.uuid.toString() }
                            ?: ""

                    nativeOnDeviceDiscovered(addr, name, rssi, uuids)
                }

                override fun onScanFailed(errorCode: Int) {
                    Log.e(TAG, "scan failed: errorCode=$errorCode")
                }
            }

        scanner.startScan(filters, settings, scanCallback)
        Log.d(TAG, "scan started (filters=${serviceUuids.size} UUIDs)")
    }

    @JvmStatic
    fun stopScan() {
        scanCallback?.let { cb ->
            adapter?.bluetoothLeScanner?.stopScan(cb)
            scanCallback = null
        }
    }

    // ── GATT callback ──

    private val gattCallback =
        object : BluetoothGattCallback() {
            override fun onConnectionStateChange(
                gatt: BluetoothGatt,
                status: Int,
                newState: Int,
            ) {
                val addr = gatt.device.address

                if (newState == BluetoothProfile.STATE_CONNECTED) {
                    gattConnections[addr] = gatt
                    // Hold the global GATT semaphore during MTU negotiation so
                    // that no operations (from any device) proceed until the
                    // chipset finishes. Don't notify Rust yet — wait for onMtuChanged.
                    gattSemaphore.drainPermits()
                    mtuHoldDevices.add(addr)
                    if (!gatt.requestMtu(512)) {
                        // MTU request failed — release semaphore and notify Rust anyway.
                        Log.w(TAG, "requestMtu failed for $addr, proceeding without MTU negotiation")
                        mtuHoldDevices.remove(addr)
                        releaseGatt()
                        nativeOnConnectionStateChanged(addr, true)
                    }
                } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                    gattConnections.remove(addr)
                    mtuMap.remove(addr)
                    // If this device was holding the semaphore for MTU negotiation,
                    // release it so other devices aren't deadlocked.
                    if (mtuHoldDevices.remove(addr)) {
                        releaseGatt()
                    }
                    // For regular operations, gatt.close() triggers pending
                    // callbacks (with error status) which release the semaphore.
                    gatt.close()
                    nativeOnConnectionStateChanged(addr, false)
                }
            }

            override fun onMtuChanged(
                gatt: BluetoothGatt,
                mtu: Int,
                status: Int,
            ) {
                val addr = gatt.device.address
                if (status == BluetoothGatt.GATT_SUCCESS) {
                    mtuMap[addr] = mtu
                    nativeOnMtuChanged(addr, mtu)
                }
                // Release the global semaphore held since onConnectionStateChange.
                mtuHoldDevices.remove(addr)
                releaseGatt()
                // NOW notify Rust — MTU is negotiated, GATT is ready for operations.
                nativeOnConnectionStateChanged(addr, true)
            }

            override fun onServicesDiscovered(
                gatt: BluetoothGatt,
                status: Int,
            ) {
                val addr = gatt.device.address
                // Release the GATT semaphore (acquired in discoverServices).
                releaseGatt()
                if (status == BluetoothGatt.GATT_SUCCESS) {
                    val json = servicesToJson(gatt.services)
                    nativeOnServicesDiscovered(addr, json)
                } else {
                    nativeOnServicesDiscovered(addr, "[]")
                }
            }

            override fun onCharacteristicRead(
                gatt: BluetoothGatt,
                characteristic: BluetoothGattCharacteristic,
                value: ByteArray,
                status: Int,
            ) {
                // Release the GATT semaphore (acquired in readCharacteristic).
                releaseGatt()
                nativeOnCharacteristicRead(
                    gatt.device.address,
                    characteristic.uuid.toString(),
                    value,
                    status,
                )
            }

            override fun onCharacteristicWrite(
                gatt: BluetoothGatt,
                characteristic: BluetoothGattCharacteristic,
                status: Int,
            ) {
                // Only release the semaphore for write-with-response. For
                // write-without-response it was already released in writeCharacteristic().
                if (pendingWriteResponse.remove(gatt.device.address)) {
                    releaseGatt()
                }
                nativeOnCharacteristicWrite(
                    gatt.device.address,
                    characteristic.uuid.toString(),
                    status,
                )
            }

            override fun onDescriptorWrite(
                gatt: BluetoothGatt,
                descriptor: BluetoothGattDescriptor,
                status: Int,
            ) {
                // Release the GATT semaphore (acquired in subscribeCharacteristic).
                releaseGatt()
            }

            override fun onCharacteristicChanged(
                gatt: BluetoothGatt,
                characteristic: BluetoothGattCharacteristic,
                value: ByteArray,
            ) {
                // Notifications don't hold the GATT semaphore — they are passive.
                nativeOnCharacteristicChanged(
                    gatt.device.address,
                    characteristic.uuid.toString(),
                    value,
                )
            }
        }

    // ── Connection management ──

    @JvmStatic
    fun connect(deviceAddr: String) {
        val ctx =
            context ?: run {
                Log.e(TAG, "context not initialized")
                return
            }

        // Close any stale GATT connection to avoid leaking clientIf slots.
        // Android has a limit of ~7 concurrent GATT clients.
        gattConnections.remove(deviceAddr)?.let { oldGatt ->
            oldGatt.disconnect()
            oldGatt.close()
            Log.d(TAG, "closed stale GATT for $deviceAddr")
        }

        val device =
            adapter?.getRemoteDevice(deviceAddr) ?: run {
                Log.e(TAG, "could not get remote device $deviceAddr")
                return
            }
        // TRANSPORT_LE ensures we connect over BLE, not classic Bluetooth.
        val gatt = device.connectGatt(ctx, false, gattCallback, BluetoothDevice.TRANSPORT_LE)
        if (gatt == null) {
            Log.e(TAG, "connectGatt returned null for $deviceAddr")
            nativeOnConnectionStateChanged(deviceAddr, false)
            return
        }
        Log.d(TAG, "connecting to $deviceAddr")
    }

    @JvmStatic
    fun disconnect(deviceAddr: String) {
        gattConnections[deviceAddr]?.let { gatt ->
            gatt.disconnect()
            Log.d(TAG, "disconnecting from $deviceAddr")
        }
    }

    // ── GATT operations (serialized via global semaphore) ──

    @JvmStatic
    fun discoverServices(deviceAddr: String): Int {
        val gatt = gattConnections[deviceAddr] ?: return STATUS_NOT_CONNECTED
        if (!acquireGatt(10000)) return STATUS_GATT_BUSY
        if (!gatt.discoverServices()) {
            releaseGatt()
            return STATUS_GATT_FAILED
        }
        // onServicesDiscovered will release the semaphore.
        return STATUS_SUCCESS
    }

    @JvmStatic
    fun readCharacteristic(
        deviceAddr: String,
        charUuid: String,
    ): Int {
        val gatt = gattConnections[deviceAddr] ?: return STATUS_NOT_CONNECTED
        val char = findCharacteristic(gatt, charUuid) ?: return STATUS_CHAR_NOT_FOUND
        if (!acquireGatt()) return STATUS_GATT_BUSY
        if (!gatt.readCharacteristic(char)) {
            releaseGatt()
            return STATUS_GATT_FAILED
        }
        // onCharacteristicRead will release the semaphore.
        return STATUS_SUCCESS
    }

    @JvmStatic
    fun writeCharacteristic(
        deviceAddr: String,
        charUuid: String,
        value: ByteArray,
        writeType: Int,
    ): Int {
        val gatt = gattConnections[deviceAddr] ?: return STATUS_NOT_CONNECTED
        val char = findCharacteristic(gatt, charUuid) ?: return STATUS_CHAR_NOT_FOUND
        if (!acquireGatt()) return STATUS_GATT_BUSY
        val result = gatt.writeCharacteristic(char, value, writeType)
        if (result != BluetoothStatusCodes.SUCCESS) {
            releaseGatt()
            return STATUS_GATT_FAILED
        }
        if (writeType == BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE) {
            releaseGatt()
        } else {
            pendingWriteResponse.add(deviceAddr)
        }
        return STATUS_SUCCESS
    }

    @JvmStatic
    fun subscribeCharacteristic(
        deviceAddr: String,
        charUuid: String,
    ): Int {
        val gatt = gattConnections[deviceAddr] ?: return STATUS_NOT_CONNECTED
        val char = findCharacteristic(gatt, charUuid) ?: return STATUS_CHAR_NOT_FOUND

        if (!gatt.setCharacteristicNotification(char, true)) return STATUS_GATT_FAILED

        // Write to CCCD to enable notifications on the remote device.
        val cccdUuid = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")
        val descriptor = char.getDescriptor(cccdUuid) ?: return STATUS_CHAR_NOT_FOUND
        if (!acquireGatt()) return STATUS_GATT_BUSY
        val result =
            gatt.writeDescriptor(
                descriptor,
                BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE,
            )
        if (result != BluetoothStatusCodes.SUCCESS) {
            releaseGatt()
            return STATUS_GATT_FAILED
        }
        // onDescriptorWrite will release the semaphore.
        return STATUS_SUCCESS
    }

    @JvmStatic
    fun unsubscribeCharacteristic(
        deviceAddr: String,
        charUuid: String,
    ): Int {
        val gatt = gattConnections[deviceAddr] ?: return STATUS_NOT_CONNECTED
        val char = findCharacteristic(gatt, charUuid) ?: return STATUS_CHAR_NOT_FOUND

        // Always disable local notification state first, even if the CCCD
        // write below fails (GATT busy). The remote side will eventually
        // notice via timeout.
        gatt.setCharacteristicNotification(char, false)

        val cccdUuid = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")
        val descriptor = char.getDescriptor(cccdUuid)
        if (descriptor != null) {
            if (!acquireGatt()) return STATUS_GATT_BUSY
            val result =
                gatt.writeDescriptor(
                    descriptor,
                    BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE,
                )
            if (result != BluetoothStatusCodes.SUCCESS) {
                releaseGatt()
                return STATUS_GATT_FAILED
            }
            // onDescriptorWrite will release.
        }
        return STATUS_SUCCESS
    }

    @JvmStatic
    fun getMtu(deviceAddr: String): Int = mtuMap[deviceAddr] ?: 23

    // ── L2CAP ──

    @JvmStatic
    fun openL2capChannel(
        deviceAddr: String,
        psm: Int,
    ) {
        if (android.os.Build.VERSION.SDK_INT < 29) {
            nativeOnL2capChannelError(deviceAddr, "L2CAP requires API 29+")
            return
        }

        val device =
            adapter?.getRemoteDevice(deviceAddr) ?: run {
                nativeOnL2capChannelError(deviceAddr, "device not found")
                return
            }

        Thread {
            try {
                val socket = device.createInsecureL2capChannel(psm)
                socket.connect()
                val socketId = l2cap.register(socket)
                nativeOnL2capChannelOpened(deviceAddr, socketId, false)
                l2cap.startReadLoop(socketId, deviceAddr, socket)
            } catch (e: Exception) {
                Log.e(TAG, "L2CAP connect failed: ${e.message}")
                nativeOnL2capChannelError(deviceAddr, e.message ?: "connect failed")
            }
        }.start()
    }

    @JvmStatic
    fun writeL2cap(
        socketId: Int,
        data: ByteArray,
    ) = l2cap.write(socketId, data)

    @JvmStatic
    fun closeL2cap(socketId: Int) = l2cap.close(socketId)

    // ── Helpers ──

    private fun findCharacteristic(
        gatt: BluetoothGatt,
        charUuid: String,
    ): BluetoothGattCharacteristic? {
        val uuid = UUID.fromString(charUuid)
        for (service in gatt.services) {
            val char = service.getCharacteristic(uuid)
            if (char != null) return char
        }
        return null
    }

    /**
     * Serialize discovered services to a JSON array. Each service is:
     * {"uuid": "...", "characteristics": [{"uuid": "...", "properties": N}]}
     *
     * We build JSON manually to avoid pulling in a JSON library dependency.
     */
    private fun servicesToJson(services: List<BluetoothGattService>): String {
        val sb = StringBuilder("[")
        for ((i, svc) in services.withIndex()) {
            if (i > 0) sb.append(",")
            sb.append("{\"uuid\":\"").append(svc.uuid).append("\",\"characteristics\":[")
            for ((j, ch) in svc.characteristics.withIndex()) {
                if (j > 0) sb.append(",")
                sb
                    .append("{\"uuid\":\"")
                    .append(ch.uuid)
                    .append("\",\"properties\":")
                    .append(ch.properties)
                    .append("}")
            }
            sb.append("]}")
        }
        sb.append("]")
        return sb.toString()
    }
}