Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
AgainPsychoX committed Aug 3, 2019
2 parents 3442bc9 + e99d851 commit b69d01f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void connect(String address) throws IOException {

/// Disconnects current session (ignore if not connected)
public void disconnect() {
if (!isConnected()) {
if (isConnected()) {
connectionThread.cancel();
connectionThread = null;
}
Expand Down
39 changes: 20 additions & 19 deletions example/lib/ChatPage.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';
Expand All @@ -22,8 +23,7 @@ class _Message {
class _ChatPage extends State<ChatPage> {
static final clientID = 0;
static final maxMessageLength = 4096 - 3;

StreamSubscription<Uint8List> _streamSubscription;
BluetoothConnection connection;

List<_Message> messages = List<_Message>();
String _messageBuffer = '';
Expand All @@ -32,33 +32,34 @@ class _ChatPage extends State<ChatPage> {
final ScrollController listScrollController = new ScrollController();

bool isConnecting = true;
bool get isConnected => _streamSubscription != null;
bool get isConnected => connection != null && connection.isConnected;

@override
void initState() {
super.initState();

FlutterBluetoothSerial.instance.connect(widget.server).then((_) { // @TODO ? shouldn't be done via `.listen()`?
isConnecting = false;

// Subscribe for incoming data after connecting
_streamSubscription = FlutterBluetoothSerial.instance.onRead().listen(_onDataReceived);
setState(() {/* Update for `isConnecting`, since depends on `_streamSubscription` */});

// Subscribe for remote disconnection
_streamSubscription.onDone(() {
print('we got disconnected by remote!');
_streamSubscription = null;
setState(() {/* Update for `isConnected`, since is depends on `_streamSubscription` */});
try {
BluetoothConnection.toAddress(widget.server.address).then((_connection) {
print('Connected to the device');
connection = _connection;
setState(() {
isConnecting = false;
});

connection.input.listen(_onDataReceived).onDone(() {
print('Disconnected by remote request');
});
});
});
} catch (exception) {
print('Cannot connect, exception occured');
}
}

@override
void dispose() {
// Avoid memory leak (`setState` after dispose) and disconnect
if (isConnected) {
_streamSubscription.cancel();
connection.dispose();
connection = null;
print('we are disconnecting locally!');
}

Expand Down Expand Up @@ -193,7 +194,7 @@ class _ChatPage extends State<ChatPage> {
if (text.length > 0) {
textEditingController.clear();

FlutterBluetoothSerial.instance.write(text + "\r\n");
connection.output.add(utf8.encode(text + "\r\n"));

setState(() {
messages.add(_Message(clientID, text));
Expand Down

0 comments on commit b69d01f

Please sign in to comment.