-
Notifications
You must be signed in to change notification settings - Fork 362
Add read write to example app #237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5735423
add read and write operations to example app
remonh87 4abb440
fix bug in dematerialize when return type is void
remonh87 8bf9db6
add posibility to write multiple values
remonh87 89e485b
add subscribe to interaction dialog
remonh87 1a28a0d
improve layout
remonh87 8887c53
revert changing signature of dematerialize
remonh87 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
235 changes: 235 additions & 0 deletions
235
example/lib/src/ui/device_detail/characteristic_interaction_dialog.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,235 @@ | ||
| import 'dart:async'; | ||
|
|
||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter_reactive_ble/flutter_reactive_ble.dart'; | ||
| import 'package:flutter_reactive_ble_example/src/ble/ble_device_interactor.dart'; | ||
| import 'package:provider/provider.dart'; | ||
|
|
||
| class CharacteristicInteractionDialog extends StatelessWidget { | ||
| const CharacteristicInteractionDialog({ | ||
| required this.characteristic, | ||
| Key? key, | ||
| }) : super(key: key); | ||
| final QualifiedCharacteristic characteristic; | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return Consumer<BleDeviceInteractor>(builder: (context, interactor, _) { | ||
| return _CharacteristicInteractionDialog( | ||
| characteristic: characteristic, | ||
| readCharacteristic: interactor.readCharacteristic, | ||
| writeWithResponse: interactor.writeCharacterisiticWithResponse, | ||
| writeWithoutResponse: interactor.writeCharacterisiticWithoutResponse, | ||
| subscribeToCharacteristic: interactor.subScribeToCharacteristic, | ||
| ); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| class _CharacteristicInteractionDialog extends StatefulWidget { | ||
| const _CharacteristicInteractionDialog({ | ||
| required this.characteristic, | ||
| required this.readCharacteristic, | ||
| required this.writeWithResponse, | ||
| required this.writeWithoutResponse, | ||
| required this.subscribeToCharacteristic, | ||
| Key? key, | ||
| }) : super(key: key); | ||
|
|
||
| final QualifiedCharacteristic characteristic; | ||
| final Future<List<int>> Function(QualifiedCharacteristic characteristic) | ||
| readCharacteristic; | ||
| final Future<void> Function( | ||
| QualifiedCharacteristic characteristic, List<int> value) | ||
| writeWithResponse; | ||
|
|
||
| final Stream<List<int>?> Function(QualifiedCharacteristic characteristic) | ||
| subscribeToCharacteristic; | ||
|
|
||
| final Future<void> Function( | ||
| QualifiedCharacteristic characteristic, List<int> value) | ||
| writeWithoutResponse; | ||
|
|
||
| @override | ||
| _CharacteristicInteractionDialogState createState() => | ||
| _CharacteristicInteractionDialogState(); | ||
| } | ||
|
|
||
| class _CharacteristicInteractionDialogState | ||
| extends State<_CharacteristicInteractionDialog> { | ||
| late String readOutput; | ||
| late String writeOutput; | ||
| late String subscribeOutput; | ||
| late TextEditingController textEditingController; | ||
| late StreamSubscription<List<int>?> subscribeStream; | ||
|
|
||
| @override | ||
| void initState() { | ||
| readOutput = ''; | ||
| writeOutput = ''; | ||
| subscribeOutput = ''; | ||
| textEditingController = TextEditingController(); | ||
| super.initState(); | ||
| } | ||
|
|
||
| @override | ||
| void dispose() { | ||
| subscribeStream.cancel(); | ||
| super.dispose(); | ||
| } | ||
|
|
||
| Future<void> subscribeCharacteristic() async { | ||
| setState(() { | ||
| subscribeOutput = 'Notification set'; | ||
| }); | ||
| subscribeStream = | ||
| widget.subscribeToCharacteristic(widget.characteristic).listen((event) { | ||
| setState(() { | ||
| subscribeOutput = event.toString(); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| Future<void> readCharacteristic() async { | ||
| final result = await widget.readCharacteristic(widget.characteristic); | ||
| setState(() { | ||
| readOutput = result.toString(); | ||
| }); | ||
| } | ||
|
|
||
| List<int> _parseInput() { | ||
| return textEditingController.text | ||
| .split(',') | ||
| .map( | ||
| (value) => int.parse(value), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it clear in UI that this is a decimal number input? |
||
| ) | ||
| .toList(); | ||
| } | ||
|
|
||
| Future<void> writeCharacteristicWithResponse() async { | ||
| await widget.writeWithResponse(widget.characteristic, _parseInput()); | ||
| setState(() { | ||
| writeOutput = 'Ok'; | ||
| }); | ||
| } | ||
|
|
||
| Future<void> writeCharacteristicWithoutResponse() async { | ||
| await widget.writeWithoutResponse(widget.characteristic, _parseInput()); | ||
| setState(() { | ||
| writeOutput = 'Done'; | ||
| }); | ||
| } | ||
|
|
||
| Widget sectionHeader(String text) => Text( | ||
| text, | ||
| style: TextStyle(fontWeight: FontWeight.bold), | ||
| ); | ||
|
|
||
| List<Widget> get writeSection => [ | ||
| sectionHeader('Write characteristic'), | ||
| Padding( | ||
| padding: const EdgeInsets.symmetric(vertical: 8.0), | ||
| child: TextField( | ||
| controller: textEditingController, | ||
| decoration: InputDecoration( | ||
| border: OutlineInputBorder(), | ||
| labelText: 'Value', | ||
| ), | ||
| keyboardType: TextInputType.numberWithOptions( | ||
| decimal: true, | ||
| signed: false, | ||
| ), | ||
| ), | ||
| ), | ||
| Row( | ||
| mainAxisAlignment: MainAxisAlignment.spaceBetween, | ||
| children: [ | ||
| ElevatedButton( | ||
| onPressed: writeCharacteristicWithResponse, | ||
| child: Text('With response'), | ||
| ), | ||
| ElevatedButton( | ||
| onPressed: writeCharacteristicWithoutResponse, | ||
| child: Text('Without response'), | ||
| ), | ||
| ], | ||
| ), | ||
| Padding( | ||
| padding: const EdgeInsetsDirectional.only(top: 8.0), | ||
| child: Text('Output: $writeOutput'), | ||
| ), | ||
| ]; | ||
|
|
||
| List<Widget> get readSection => [ | ||
| sectionHeader('Read characteristic'), | ||
| Row( | ||
| mainAxisAlignment: MainAxisAlignment.spaceBetween, | ||
| children: [ | ||
| ElevatedButton( | ||
| onPressed: readCharacteristic, | ||
| child: Text('Read'), | ||
| ), | ||
| Text('Output: $readOutput'), | ||
| ], | ||
| ), | ||
| ]; | ||
|
|
||
| List<Widget> get subscribeSection => [ | ||
| sectionHeader('Subscribe / notify'), | ||
| Row( | ||
| mainAxisAlignment: MainAxisAlignment.spaceBetween, | ||
| children: [ | ||
| ElevatedButton( | ||
| onPressed: subscribeCharacteristic, | ||
| child: Text('Subscribe'), | ||
| ), | ||
| Text('Output: $subscribeOutput'), | ||
| ], | ||
| ), | ||
| ]; | ||
|
|
||
| Widget get divider => Padding( | ||
| padding: const EdgeInsets.symmetric(vertical: 12.0), | ||
| child: Divider(thickness: 2.0), | ||
| ); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return Dialog( | ||
| child: Padding( | ||
| padding: const EdgeInsets.all(20.0), | ||
| child: ListView( | ||
| shrinkWrap: true, | ||
| children: [ | ||
| Text( | ||
| 'Select an operation', | ||
| style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold), | ||
| ), | ||
| Padding( | ||
| padding: const EdgeInsets.symmetric(vertical: 8.0), | ||
| child: Text( | ||
| widget.characteristic.characteristicId.toString(), | ||
| ), | ||
| ), | ||
| divider, | ||
| ...readSection, | ||
| divider, | ||
| ...writeSection, | ||
| divider, | ||
| ...subscribeSection, | ||
| divider, | ||
| Align( | ||
| alignment: Alignment.bottomRight, | ||
| child: Padding( | ||
| padding: const EdgeInsets.only(top: 20.0), | ||
| child: ElevatedButton( | ||
| onPressed: () => Navigator.of(context).pop(), | ||
| child: Text('close')), | ||
| ), | ||
| ) | ||
| ], | ||
| ), | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious, I didn't know about this new keyword
late👍