Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## Unreleased
* Peripheral mode: `updateCharacteristicValue` now completes when the notification is settled instead of when it was submitted, so awaiting it paces a bulk transfer and reports failures that previously only reached a log. Android completes on `onNotificationSent` (failing on a non-success GATT status), Apple when `CBPeripheralManager.updateValue` accepts the value — immediately or after `peripheralManagerIsReady(toUpdateSubscribers:)` — and Windows when `NotifyValueAsync` completes. A queued update that can never be sent fails instead of hanging: `clearPendingNotifications`, `removeService`, `clearServices`, central unsubscription, a Bluetooth reset and a device disconnect all settle their targets with an error. This is a behavioural change, not a signature break: the parameter list is unchanged, but code that awaited the future without minding the delay now waits for delivery, so bulk senders should keep awaiting it between chunks (#402).
* Apple peripheral: the notification FIFO is now an O(1) head-indexed queue instead of `Array.removeFirst()`, which shifted the whole tail per chunk and made a bulk transfer O(N²) on the main queue. Each accepted element is settled exactly once, and discarded updates fail their Dart futures with a descriptive error instead of waiting forever (#402).
* Apple peripheral: add `UniversalBlePeripheral.setDesiredConnectionLatency(deviceId, PeripheralConnectionLatency.low|medium|high)`, mapped to `CBPeripheralManager.setDesiredConnectionLatency(_:for:)`. Apple otherwise negotiates a high latency, which caps notification throughput. No-op on non-Apple platforms (#402).
* Windows: connecting to a device whose GATT database holds a service Windows reserves for itself — HID over GATT, `0x1812` — no longer fails. Windows answers `GetCharacteristicsAsync` with `AccessDenied` for it whether or not the device is paired, and since 2.2.0 that error ended the connection attempt during discovery. A denied service is now left out: its characteristics are not reported by `discoverServices`, the rest of the database connects normally, and a GATT service refresh skips it instead of keeping the previous map. A denied service that discovery already reported is the exception — the refresh keeps the previous services then, so reads and subscriptions on it do not silently disappear. A device whose every service was denied still connects, reports no services, and logs a warning. `Unreachable` and `ProtocolError` still fail the connection, so a discovery cut short by a link drop is still reported (#400).
* Android peripheral: an update no longer races the notification the stack is already sending. Below API 33 the payload travels in the shared `BluetoothGattCharacteristic.value`, and `updateCharacteristic` wrote it on the main thread while `onNotificationSent` drained the next queued payload on a binder thread: the assignment could land between that drain's value write and its `notifyCharacteristicChanged` call, so a same-length payload was silently transmitted with the wrong content. The in-flight check, the dequeue, the value assignment and the send now all happen under the pending-notification monitor, and the drain no longer reads the subscription map while holding it — the descriptor and cleanup paths take it in the opposite order. API 33+ uses the stateless four-argument overload and treats only `BluetoothStatusCodes.SUCCESS` as sent, and the value a read returns is written only when no target device is subscribed rather than on every update (#403).

Expand Down
52 changes: 44 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Looking for Bluetooth Classic? Check [universal_bluetooth_classic](https://pub.d
| getSubscribedClients | ✔️ | ✔️ | ✔️ | ✔️ | 🚧 | — |
| getMaximumNotifyLength | ✔️ | ✔️ | ✔️ | ✔️ | 🚧 | — |
| setAutoConnect | ✔️ | — | — | — | — | — |
| setDesiredConnectionLatency | — | ✔️ | ✔️ | — | — | — |
| event streams\*\*\* | ✔️ | ✔️ | ✔️ | ✔️ | 🚧 | — |

\* `getAvailabilityState` returns a snapshot. Listen to `advertisingStateStream`, `connectionStateStream`, and related streams for runtime updates.
Expand Down Expand Up @@ -1212,9 +1213,30 @@ await UniversalBlePeripheral.updateCharacteristicValue(
value: Uint8List.fromList([88]),
deviceId: deviceId,
);

// Pace a bulk transfer by awaiting each chunk. The future completes once the
// platform accepted (Apple) or sent (Android/Windows) the notification.
for (final chunk in chunks) {
await UniversalBlePeripheral.updateCharacteristicValue(
characteristicId: batteryLevelChar,
value: chunk,
);
}
```

On Windows, the returned future completes after submitting the notification. Awaiting it or selecting `QueueType.perDevice` or `QueueType.global` serializes submission only: notification operations can still overlap, and delivery order is not guaranteed. Transport failures and truncated values are logged when `UniversalBle.setLogLevel(BleLogLevel.error)` (or a more verbose level) is enabled. Repeated failures for a characteristic are suppressed until a successful update.
On Android, Apple and Windows the returned future is a backpressure signal: it
completes when the notification has been settled by the platform — Android on
`onNotificationSent` (failing on a non-success GATT status), Apple when
`CBPeripheralManager.updateValue` accepts the value (immediately or after
`peripheralManagerIsReady(toUpdateSubscribers:)`), Windows when
`NotifyValueAsync` completes. Failing an update that can never be sent — through
`clearPendingNotifications`, `removeService`, central unsubscription or a
Bluetooth reset — also completes it with an error. On Windows, transport
failures and truncated values are additionally logged when
`UniversalBle.setLogLevel(BleLogLevel.error)` (or a more verbose level) is
enabled; repeated failures for a characteristic are suppressed until a
successful update. Selecting `QueueType.perDevice` or `QueueType.global` still
serializes submission, but awaiting the future is what paces the radio.

### Subscribed Clients and Notify Length

Expand Down Expand Up @@ -1332,13 +1354,27 @@ before replacement. Install listeners before configuration, then query
`getSubscribedClients` to reconcile readiness. Explicit force-quit recovery and
background discoverability are governed by CoreBluetooth and are not guaranteed.

Apple notifications remain FIFO while CoreBluetooth applies backpressure. Use
`await UniversalBlePeripheral.clearPendingNotifications(deviceId)` at an explicit
session boundary or suspension to discard unsent updates, then publish neutral
state before new input. Native unsubscription, service removal, and adapter loss
also invalidate pending Apple updates. Packets already accepted by the OS cannot
be recalled. Completion of `updateCharacteristicValue` means accepted by the
plugin, not acknowledged by the remote device.
Apple notifications remain FIFO while CoreBluetooth applies backpressure.
Use `await UniversalBlePeripheral.clearPendingNotifications(deviceId)` at an
explicit session boundary or suspension to discard unsent updates, then publish
neutral state before new input. `updateCharacteristicValue` completes once
`CBPeripheralManager.updateValue` accepts the value — immediately, or later from
`peripheralManagerIsReady(toUpdateSubscribers:)` when the transmit queue was
full — so awaiting it between chunks paces a bulk transfer. It fails when a
queued update is discarded: `clearPendingNotifications`, service removal,
central unsubscription or adapter loss. Packets already accepted by the OS
cannot be recalled, and remote acknowledgement remains a link-layer concern.

Apple negotiates a high connection latency by default, which caps notification
throughput. Ask for the low-latency mode before a bulk transfer (iOS and macOS;
no-op elsewhere):

```dart
await UniversalBlePeripheral.setDesiredConnectionLatency(
deviceId,
PeripheralConnectionLatency.low,
);
```

Run `./darwin/test/run_peripheral_reliability_tests.sh` on macOS for native
characteristic, restored-identity, and notification-FIFO regression checks.
Expand Down
Loading
Loading