Kotlin + Compose 项目

// BLE 扫描(Android 4.3 ~ 14 兼容)
implementation “no.nordicsemi.android.support.v18:scanner:1.6.0”

// BLE 连接 / GATT / 读写 / Notify(核心)
implementation “no.nordicsemi.android:ble:2.10.2”

// Kotlin 协程支持(suspend / Flow)
implementation “no.nordicsemi.android:ble-ktx:2.10.2”

如果用到 下面的库可以选择依赖
// 只有老 LiveData 架构才要
implementation “no.nordicsemi.android:ble-livedata:2.10.2”

// 公共常量(通常会被 ble 自动传递)
implementation “no.nordicsemi.android:ble-common:2.10.2”

权限


扫描

val scanner = BluetoothLeScannerCompat.getScanner()
scanner.startScan(
null,
ScanSettings.Builder().build(),
scanCallback
)

连接 继承 BleManager)

class MyBleManager(
context: Context
) : BleManager(context) {

override fun getGattCallback(): BleManagerGattCallback {
    return object : BleManagerGattCallback() {
        override fun onServicesDiscovered(device: BluetoothDevice, services: List<BluetoothGattService>) {
            // 找到你的 Service / Characteristic
        }
    }
}

}

读 / 写 / notify

writeCharacteristic(characteristic, data)
readCharacteristic(characteristic)
enableNotifications(characteristic)

更多推荐