Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
chipweinberger authored Nov 15, 2023
1 parent e7c198b commit 2235473
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ subscription.cancel();

```dart
// listen for disconnection
device.connectionState.listen((BluetoothConnectionState state) async {
var subscription = device.connectionState.listen((BluetoothConnectionState state) async {
if (state == BluetoothConnectionState.disconnected) {
// 1. typically, start a periodic timer that tries to
// reconnect, or just call connect() again right now
Expand All @@ -174,6 +174,9 @@ await device.connect();
// Disconnect from device
await device.disconnect();
// cancel to prevent duplicate listeners
subscription.cancel();
```

### MTU
Expand All @@ -183,13 +186,13 @@ On Android, we request an mtu of 512 by default during connection (see: `connect
On iOS & macOS, the mtu is negotiated automatically, typically 135 to 255.

```dart
final mtuSubscription = device.onMtu.listen((int mtu) {
final subscription = device.onMtu.listen((int mtu) {
// iOS: initial value is always 23, but iOS will quickly negotiate a higher value
print("mtu $mtu");
});
// cleanup: cancel subscription when disconnected
device.cancelWhenDisconnected(mtuSubscription);
device.cancelWhenDisconnected(subscription);
// You can also manually change the mtu yourself.
if (Platform.isAndroid) {
Expand Down Expand Up @@ -262,14 +265,14 @@ extension splitWrite on BluetoothCharacteristic {
// If `onValueReceived` is never called, see [Common Problems](#common-problems) in the README.

```dart
final chrSubscription = characteristic.onValueReceived.listen((value) {
final subscription = characteristic.onValueReceived.listen((value) {
// onValueReceived is updated:
// - anytime read() is called
// - anytime a notification arrives (if subscribed)
});
// cleanup: cancel subscription when disconnected
device.cancelWhenDisconnected(chrSubscription);
device.cancelWhenDisconnected(subscription);
// subscribe
// Note: If a characteristic supports both **notifications** and **indications**,
Expand All @@ -284,7 +287,7 @@ await characteristic.setNotifyValue(true);
It is very convenient for simple characteristics that support both WRITE and READ (and/or NOTIFY). **e.g.** a "light switch toggle" characteristic.

```dart
final chrSubscription = characteristic.lastValueStream.listen((value) {
final subscription = characteristic.lastValueStream.listen((value) {
// lastValueStream` is updated:
// - anytime read() is called
// - anytime write() is called
Expand All @@ -293,7 +296,7 @@ final chrSubscription = characteristic.lastValueStream.listen((value) {
});
// cleanup: cancel subscription when disconnected
device.cancelWhenDisconnected(chrSubscription);
device.cancelWhenDisconnected(subscription);
// enable notifications
await characteristic.setNotifyValue(true);
Expand Down

0 comments on commit 2235473

Please sign in to comment.