Skip to content

Commit

Permalink
Added support for notification/indications on characteristics.
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldemarco committed Sep 4, 2017
1 parent d9bd574 commit 197caba
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
20 changes: 20 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class _FlutterBlueAppState extends State<FlutterBlueApp> {
BluetoothDevice device;
bool get isConnected => (device != null);
List<BluetoothService> services = new List();
StreamSubscription<List<int>> valueChangedSubscription;

@override
void initState() {
Expand Down Expand Up @@ -128,6 +129,20 @@ class _FlutterBlueAppState extends State<FlutterBlueApp> {
setState(() {});
}

_setNotification(BluetoothCharacteristic c) async {
if(c.isNotifying) {
await device.setNotifyValue(c, false);
} else {
await device.setNotifyValue(c, true);
valueChangedSubscription = device.onValueChanged(c).listen((d) {
setState(() {
print('onValueChanged $d');
});
});
}
setState(() {});
}

_buildScanningButton(BuildContext context) {
if (isConnected || state != BluetoothState.on) {
return null;
Expand Down Expand Up @@ -211,6 +226,11 @@ class _FlutterBlueAppState extends State<FlutterBlueApp> {
icon: new Icon(Icons.file_upload,
color: Theme.of(context).iconTheme.color.withOpacity(0.5)),
onPressed: () => _writeCharacteristic(c),
),
new IconButton(
icon: new Icon(c.isNotifying ? Icons.sync_disabled : Icons.sync,
color: Theme.of(context).iconTheme.color.withOpacity(0.5)),
onPressed: () => _setNotification(c),
)
],
);
Expand Down
3 changes: 0 additions & 3 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ dependencies:
intl: '^0.15.1'
intl_translation: '^0.15.0'
convert: "^2.0.1"
guid:
git:
url: git://github.com/pauldemarco/guid.git
protobuf: '^0.5.5'

# For information on the generic Dart part of this file, see the
Expand Down

0 comments on commit 197caba

Please sign in to comment.