项目 / JuulLabs/kable

JuulLabs/kable

行业推荐
语言 Kotlin标签 bluetooth-low-energykotlin-multiplatformkotlin-coroutinesapplecore-bluetoothjavascriptweb-bluetoothandroid数据更新 2026-11-20 06:49:16 · 仓库 2026-11-19

一段话介绍

一套用 Kotlin 协程编写的低功耗蓝牙 API,用于与 BLE 设备交互。

本站整理,非 GitHub 官方 description。

官方 description(GitHub API 原样)
Kotlin Asynchronous Bluetooth Low-Energy
1,204累计星
115Fork
—峰值日增
0上榜次数
总榜
暂未入榜
日榜
暂未入榜
周榜
暂未入榜
月榜
暂未入榜

官方快照(gh_history.db,2026-11-25):总榜 未进 Top50 · 日榜 未进 Top50 · 周榜未满 7 天基线(官方不生成) · 月榜暂无官方口径;下方四卡为站内趋势聚合(周卡按上榜次数,非官方 7 日增量窗)。

行业与场景

按行业选型 →

能耗监测与能效优化

适配系统:能源管理系统、能耗监测平台 · 得分 1.4

面向能耗采集与负荷分析建模,可用于园区/楼宇能效诊断与用能优化建议。

更多信息

以下三段按该项目自己的 GitHub README 章节摘录(可合理截断并标明);README 没有对应章节的留空,不编造。

我的用法
在 Kotlin Multiplatform 项目中,按[仓库 README](https://github.com/JuulLabs/kable)的 Gradle 配置引入 `kable-core`,并按目标平台补充权限等设置。具体调用方式可参考仓库中的 SensorTag 示例应用。
特点或设计要点
支持 Android、iOS、JavaScript、macOS 和 JVM 等目标;提供扫描、连接、读写和观察特征值等能力,并可用蓝牙标准短 UUID 或 Web Bluetooth 名称来构造 UUID。
星标走势还差一天的数据,明天就能看到曲线
每天 10:30 记一次星标,连续两天后自动生成曲线
首次发现 — · 最近在榜 2026-11-23 · 语言 Kotlin · 贡献者 —
kotlin

GitHub 项目主页介绍

来自 GitHub 仓库 README / 主页介绍正文,正文较长,已截断:保留前 20000 字符(原始 33,342 字节),完整内容见 GitHub 仓库。

![badge][badge-android]
![badge][badge-ios]
![badge][badge-js]
![badge][badge-mac]
![badge][badge-jvm]
[![Slack](https://img.shields.io/badge/Slack-%23juul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MCAwIDM0LjA0OCA1NG0wLTE0LjM2NWgxNC4zMzZhNS4zODEgNS4zODEgMCAwIDAgNS4zNzYtNS4zODYgNS4zODEgNS4zODEgMCAwIDAtNS4zNzYtNS4zODdIMzQuMDQ4YTUuMzgxIDUuMzgxIDAgMCAwLTUuMzc2IDUuMzg3IDUuMzgxIDUuMzgxIDAgMCAwIDUuMzc2IDUuMzg2IiBmaWxsPSIjRUNCMjJFIi8+PHBhdGggZD0iTTAgMzQuMjQ5YTUuMzgxIDUuMzgxIDAgMCAwIDUuMzc2IDUuMzg2IDUuMzgxIDUuMzgxIDAgMCAwIDUuMzc2LTUuMzg2di01LjM4N0g1LjM3NkE1LjM4MSA1LjM4MSAwIDAgMCAwIDM0LjI1bTE0LjMzNi0uMDAxdjE0LjM2NEE1LjM4MSA1LjM4MSAwIDAgMCAxOS43MTIgNTRhNS4zODEgNS4zODEgMCAwIDAgNS4zNzYtNS4zODdWMzQuMjVhNS4zODEgNS4zODEgMCAwIDAtNS4zNzYtNS4zODcgNS4zODEgNS4zODEgMCAwIDAtNS4zNzYgNS4zODciIGZpbGw9IiNFMDFFNUEiLz48L2c+PC9zdmc+&labelColor=611f69)](https://kotlinlang.slack.com/messages/juul-libraries/)

# Kable

**K**otlin **A**synchronous **B**luetooth **L**ow **E**nergy provides a simple Coroutines-powered API for interacting
with Bluetooth Low Energy devices.

Usage is demonstrated with the [SensorTag sample app].

## UUIDs

UUIDs (Universally Unique Identifiers) are used to uniquely identify various components of a
Bluetooth Low Energy device. The Bluetooth Base UUID (`00000000-0000-1000-8000-00805F9B34FB`) allows
for short form (16-bit or 32-bit) UUIDs which are reserved for standard, predefined components
(e.g. 0x180D for "Heart Rate Service", or 0x2A37 for "Heart Rate Measurement").
128-bit UUIDs outside of the Bluetooth Base UUID are typically used for custom applications.

The `Bluetooth.BaseUuid` is provided to simplify defining 16-bit or 32-bit UUIDs. Simply add (`+`)
a 16-bit or 32-bit UUID (in [`Int`] or [`Long`] form) to the Bluetooth Base UUID to get a "full"
[`Uuid`] representation; for example:

```kotlin
val uuid16bit = 0x180D
val heartRateServiceUuid = Bluetooth.BaseUuid + uuid16bit
println(heartRateServiceUuid) // Output: 0000180d-0000-1000-8000-00805f9b34fb
```

Web Bluetooth named UUIDs may also be used to acquire [`Uuid`]s via the following [`Uuid`] extension
functions:

- `Uuid.service(name: String)`
- `Uuid.characteristic(name: String)`
- `Uuid.descriptor(name: String)`

For example:

```kotlin
val heartRateServiceUuid = Uuid.service("heart_rate")
println(heartRateServiceUuid) // Output: 0000180d-0000-1000-8000-00805f9b34fb
```

> [!NOTE]
> List of known UUID names can be found in [`Uuid.kt`](https://github.com/JuulLabs/kable/blob/main/kable-core/src/commonMain/kotlin/Uuid.kt).

Additional example shorthand notations:

| Shorthand                         | Canonical UUID                         |
|-----------------------------------|----------------------------------------|
| `Bluetooth.BaseUuid + 0x180D`     | `0000180D-0000-1000-8000-00805F9B34FB` |
| `Bluetooth.BaseUuid + 0x12345678` | `12345678-0000-1000-8000-00805F9B34FB` |
| `Uuid.service("blood_pressure")`  | `00001810-0000-1000-8000-00805F9B34FB` |
| `Uuid.characteristic("altitude")` | `00002AB3-0000-1000-8000-00805F9B34FB` |
| `Uuid.descriptor("valid_range")`  | `00002906-0000-1000-8000-00805F9B34FB` |

## Scanning

To scan for nearby peripherals, the [`Scanner`] provides an [`advertisements`] [`Flow`] which is a stream of
[`Advertisement`] objects representing advertisements seen from nearby peripherals. [`Advertisement`] objects contain
information such as the peripheral's name and RSSI (signal strength).

The [`Scanner`] may be configured via the following DSL (shown are defaults, when not specified):

```kotlin
val scanner = Scanner {
    filters {
        match {
            name = Filter.Name.Exact("My device")
        }
    }
    logging {
        engine = SystemLogEngine
        level = Warnings
        format = Multiline
    }
}
```

Scan results can be filtered by providing a list of [`Filter`]s via the `filters` DSL.
The following filters are supported:

| Filter             |    Android    |     Apple     | JavaScript |      JVM      |
|--------------------|:-------------:|:-------------:|:----------:|:-------------:|
| `Service`          |       ✓       | ✓<sup>2</sup> |     ✓      | ✓<sup>1</sup> |
| `Name`             |       ✓       | ✓<sup>1</sup> |     ✓      | ✓<sup>1</sup> |
| `NamePrefix`       | ✓<sup>1</sup> | ✓<sup>1</sup> |     ✓      | ✓<sup>1</sup> |
| `Address`          |       ✓       |               |            |               |
| `ManufacturerData` |       ✓       | ✓<sup>1</sup> |     ✓      | ✓<sup>1</sup> |

✓&nbsp; Supported natively  
✓<sup>1</sup> Support provided by Kable via flow filter  
✓<sup>2</sup> Supported natively if the only filter type used, otherwise falls back to flow filter  

> [!TIP]
> When a filter is supported natively, the system will often be able to perform scan optimizations. If feasible, it is
> recommended to provide only `Filter.Service` filters (and at least one) — as it is natively supported on all platforms.

When filters are specified, only [`Advertisement`]s that match at least one [`Filter`] will be emitted. For example, if
you had the following peripherals nearby when performing a scan:

| ID | Name        | Services                                                                           |
|:--:|-------------|------------------------------------------------------------------------------------|
| D1 | "SensorTag" | `0000aa80-0000-1000-8000-00805f9b34fb`                                             |
| D2 |             | `f484e2db-2efa-4b58-96be-f89372a3ef82`                                             |
| D3 | "Example"   | `8d7798c7-15bd-493f-a935-785305946870`,<br/>`67bebb9e-6372-4de6-a7bf-e0384583929e` |

To have peripherals D1 and D3 emitted during a scan, you could use the following `filters`:

```kotlin
val scanner = Scanner {
    filters {
        match {
            services = listOf(Bluetooth.BaseUuid + 0xaa80) // SensorTag
        }
        match {
            name = Filter.Name.Prefix("Ex")
        }
    }
}
```

Scanning begins when the [`advertisements`] [`Flow`] is collected and stops when the [`Flow`] collection is terminated.
A [`Flow`] terminal operator (such as [`first`]) may be used to scan until (for example) the first advertisement is
found matching the specified filters: 

```kotlin
val advertisement = Scanner {
    filters {
        match {
            name = Filter.Name.Exact("Example")
        }
    }
}.advertisements.first()
```

### Android

Android offers additional settings to customize scanning. They are available via the `scanSettings` property in the
[`Scanner`] builder DSL. Simply set `scanSettings` property to an Android [`ScanSettings`] object, for example:

```kotlin
val scanner = Scanner {
    scanSettings = ScanSettings.Builder()
        .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
        .build()
}
```

> [!NOTE]
> _The `scanSettings` property is only available on Android and is considered a Kable obsolete API, meaning it will be
> removed when a DSL specific API becomes available._

### JavaScript

_Scanning for nearby peripherals is supported, but only available on Chrome 79+ with "Experimental Web Platform
features" enabled via:_ `chrome://flags/#enable-experimental-web-platform-features`

## Peripheral

Once an [`Advertisement`] is obtained, it can be converted to a [`Peripheral`] via the `Peripheral` builder function:

```kotlin
val peripheral = Peripheral(advertisement) {
    // Configure peripheral.
}
```

[`Peripheral`] objects represent actions that can be performed against a remote peripheral, such as connection
handling and I/O operations. [`Peripheral`] objects provide a [`CoroutineScope`] `scope` property, and coroutines
can be `launch`ed from it:

```kotlin
peripheral.scope.launch {
    // Long running task that will be cancelled when peripheral is disposed
    // (i.e. `peripheral.close()` is called).
}
```

> [!IMPORTANT]
> When a [`Peripheral`] is no longer needed, it should be disposed via `close`:
>
> ```kotlin
> peripheral.close()
> ```
>
> Once a [`Peripheral`] is disposed (via `close`) it can no longer be used (e.g. calling `connect` will throw
> `IllegalStateException`).

> [!TIP]
> `launch`ed coroutines from a `Peripheral` object's `scope` are permitted to run until `Peripheral.dispose()`
> is called (i.e. can span across reconnects); for tasks that should only run for the duration of a single connection
> (i.e. shutdown on disconnect), `launch` via the `CoroutineScope` returned from `Peripheral.connect` (or
> [`Connected`] state `scope` property) instead.

### Configuration

#### Logging

By default, Kable only logs a small number of warnings when unexpected failures occur. To aid in debugging, additional
logging may be enabled and configured via the `logging` DSL, for example:

```kotlin
val peripheral = Peripheral(advertisement) {
    logging {
        level = Events // or Data
    }
}
```

The available log levels are:

- `Warnings`: Logs warnings when unexpected failures occur _(default)_
- `Events`: Same as `Warnings` plus logs all events (e.g. writing to a characteristic)
- `Data`: Same as `Events` plus string representation of I/O data

Available logging settings are as follows (all settings are optional; shown are defaults, when not specified):

```kotlin
val peripheral = Peripheral(advertisement) {
    logging {
        engine = SystemLogEngine
        level = Warnings
        format = Multiline
        data = Hex
    }
}
```

The format of the logs can be either `Compact` (on a single line per log) or `Multiline` (spanning multiple lines for
details):

| `Compact` | `Multiline` _(default)_ |
|-----------|-------------------------|
| <pre>example message(detail1=value1, detail2=value2, ...)</pre> | <pre>example message<br/>  detail1: value1<br/>  detail2: value2<br/>  ...</pre> |

Display format of I/O data may be customized, either by configuring the `Hex` representation, or by providing a
`DataProcessor`, for example:

```kotlin
val peripheral = Peripheral(advertisement) {
    logging {
        data = Hex {
            separator = " "
            lowerCase = false
        }

        // or...

        data = DataProcessor { bytes, _, _, _, _ ->
            // todo: Convert `bytes` to desired String representation, for example:
            bytes.joinToString { byte -> byte.toString() } // Show data as integer representation of bytes.
        }
    }
}
```

_I/O data is only shown in logs when logging `level` is set to `Data`._

When logging, the identity of the peripheral is prefixed on log messages to differentiate messages when multiple
peripherals are logging. The identifier (for the purposes of logging) can be set via the `identifier` property:

```kotlin
val peripheral = Peripheral(advertisement) {
    logging {
        identifier = "Example"
    }
}
```

The default (when not specified, or set to `null`) is to use the platform specific peripheral identifier:

- Android: Hardware (MAC) address (e.g. "00:11:22:AA:BB:CC")
- Apple: The UUID associated with the peer
- JavaScript: A `DOMString` that uniquely identifies a device
- JVM: A platform specific string. This is a UUID on Mac, a MAC address on Windows, and a path on Linux.

#### Service Discovery

All platforms support an `onServicesDiscovered` action (that is executed after service discovery but before observations
are wired up):

```kotlin
val peripheral = Peripheral(advertisement) {
    onServicesDiscovered {
        // Perform any desired I/O operations.
    }
}
```

_Exceptions thrown in `onServicesDiscovered` are propagated to the `Peripheral`'s [`connect`] call._

### Android

On Android targets, additional configuration options are available (all configuration directives are optional):

```kotlin
val peripheral = Peripheral(advertisement) {
    autoConnectIf { false } // default
    transport = Transport.Le // default
    phy = Phy.Le1M // default
    mtu = 517 // default
}
```

#### `autoConnect`

Per the [`connectGatt`] documentation, `autoConnect` determines:

> Whether to directly connect to the remote device (`false`) or to automatically connect as soon as the remote device
> becomes available (`true`).

With respect to [`connect`]ing:

| `autoConnect` value | [`connect`] timeout |
|:-------------------:|:-------------------:|
|       `false`       |     ~30 seconds     |
|       `true`        |        Never        |

Per [answer](https://stackoverflow.com/a/50273724) to "What exactly does Android's Bluetooth `autoConnect` parameter do?":

> Direct connect has a different scan interval and scan window at a higher duty than auto connect, meaning it will
> dedicate more radio time to listen for connectable advertisements for the remote device, i.e. the connection will be
> established faster.

One possible strategy for a fast initial connection attempt that falls back to lower battery usage connection attempts is:

```kotlin
val autoConnect = MutableStateFlow(false)

val peripheral = Peripheral {
    autoConnectIf { autoConnect.value }
}

while (peripheral.state.value !is Connected) {
    autoConnect.value = try {
        peripheral.connect()
        false
    } catch (e: Exception) {
        if (e is CancellationException) throw e
        true
    }
}
```

### JavaScript

On JavaScript, rather than processing a stream of advertisements, a specific peripheral can be requested using the
[`requestPeripheral`] function. Criteria ([`Options`]) such as expected service UUIDs on the peripheral and/or the
peripheral's name may be specified. When [`requestPeripheral`] is called with the specified options, the browser shows
the user a list of peripherals matching the criteria. The peripheral chosen by the user is then returned (as a
[`Peripheral`] object). If user cancels the dialog, then [`requestPeripheral`] returns `null`.

```kotlin
val options = Options {
    filters {
        match {
            name = Filter.Name.Prefix("Example")
        }
    }
    optionalServices = listOf(
        Uuid.parse("f000aa80-0451-4000-b000-000000000000"),
        Uuid.parse("f000aa81-0451-4000-b000-000000000000"),
    )
}
val peripheral = requestPeripheral(options)
```

> After the user selects a device to pair with this origin, the origin is allowed to access any service whose UUID was
> listed in the services list in any element of `options.filters` or in `options.optionalServices`.
> 
> This implies that if developers filter just by name, they must use `optionalServices` to get access to any services.

— [Web Bluetooth: 4. Device Discovery](https://webbluetoothcg.github.io/web-bluetooth/#device-discovery)

## Connectivity

Once a [`Peripheral`] object is acquired, a connection can be established via the [`connect`] function. The [`connect`]
method suspends until a connection is established and ready (or a failure occurs). A connection is considered ready when
connected, services have been discovered, and observations (if any) have been re-wired. _Service discovery occurs
automatically upon connection._

> [!TIP]
> Multiple concurrent calls to [`connect`] will all suspend until connection is ready.

```kotlin
peripheral.connect()
```

> [!TIP]
> The [`connect`] function returns a [`CoroutineScope`] that can be used to `launch` tasks that
> should run until peripheral disconnects. When [`disconnect`] is called, any coroutines
> `launch`ed from the [`CoroutineScope`] returned by [`connect`] will be cancelled prior to
> performing the underlying disconnect process.

> [!TIP]
> The connection [`CoroutineScope`] is also available as the `scope` property on the [`Connected`]
> [`State`][connection-state].

To disconnect, the [`disconnect`] function will disconnect an active connection, or cancel an in-flight connection
attempt. The [`disconnect`] function suspends until the peripheral has settled on a disconnected state.

```kotlin
peripheral.disconnect()
```

#### State

The connection state of a [`Peripheral`] can be monitored via its [`state`] [`Flow`].

```kotlin
peripheral.state.collect { state ->
    // Display and/or process the connection state.
}
```

The [`state`] will typically transition through the following [`State`][connection-state]s:

![Connection states](artwork/connection-states.png)

> [!NOTE]
> [`Disconnecting`] state is skipped on Apple and JavaScript when connection closure is initiated by peripheral (or
> peripheral goes out-of-range).

### I/O

Bluetooth Low Energy devices are organized into a tree-like structure of services, characteristics and descriptors;
whereas characteristics and descriptors have the capability of being read from, or written to.

For example, a peripheral might have the following structure:

- Service S1 (`0x1815` or `00001815-0000-1000-8000-00805f9b34fb`)
    - Characteristic C1
        - Descriptor D1
        - Descriptor D2
    - Characteristic C2 (`0x2a56` or `00002a56-0000-1000-8000-00805f9b34fb`)
        - Descriptor D3 (`gatt.client_characteristic_configuration` or `00002902-0000-1000-8000-00805f9b34fb`)
- Service S2
    - Characteristic C3

To access a characteristic or descriptor, use the [`characteristicOf`] or [`descriptorOf`] functions, respectively.
These functions lazily search for the first match (based on UUID) in the GATT profile when performing I/O.

_When performing I/O operations on a characteristic ([`read`], [`write`], [`observe`]), the properties of the
characteristic are taken into account when finding the first match. For example, when performing a [`write`] with a
[`WriteType`] of [`WithoutResponse`], the first characteristic matching the expected UUID **and** having the
[`writeWithoutResponse`] property will be used._

In the above example, to lazily access "Descriptor D3":

```kotlin
val descriptor = descriptorOf(
    service = Bluetooth.BaseUuid + 0x1815,
    characteristic = Bluetooth.BaseUuid + 0x2A56,
    descriptor = Uuid.descriptor("gatt.client_characteristic_configuration"),
)
```

Alternatively, a characteristic or descriptor may be obtained by traversing the [`Peripheral.services`]. This is useful
when multiple characteristics or descriptors have the same UUID. Objects obtained from the [`Peripheral.services`] hold
strong references to the underlying platform types, so special care must be taken to properly remove references to
objects retrieved from [`Peripheral.services`] when no longer needed.

To access "Descriptor D3" using a discovered descriptor:

```kotlin
val services = peripheral.services.value ?: error("Services have not been discovered")
val descriptor = services
    .first { it.serviceUuid == Uuid.parse("00001815-0000-1000-8000-00805f9b34fb") }
    .characteristics
    .first { it.characteristicUuid == Uuid.parse("00002a56-0000-1000-8000-00805f9b34fb") }
    .descriptors
    .first { it.descriptorUuid == Uuid.parse("00002902-0000-1000-8000-00805f9b34fb") }
```

> [!TIP]
> Shorthand notations are available for UUIDs. The accessing "Descriptor D3" example could be written as:
>
> ```kotlin
> val services = peripheral.services.value ?: error("Services have not been d
↑