Skip to content

Commit

Permalink
Version 0.0.2.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardo Folly committed Sep 27, 2018
1 parent 4f0dd41 commit 50688ef
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 19 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [0.0.1] - 2018-??-??
## [0.0.2] - 2018-09-27

* isConnected Implementation (thanks @Riscue)


## [0.0.1] - 2018-08-20

* Only Android support.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public static void registerWith(Registrar registrar) {
this.readChannel = new EventChannel(registrar.messenger(), NAMESPACE + "/read");
this.mBluetoothManager = (BluetoothManager) registrar.activity()
.getSystemService(Context.BLUETOOTH_SERVICE);
assert mBluetoothManager != null;
this.mBluetoothAdapter = mBluetoothManager.getAdapter();
channel.setMethodCallHandler(this);
stateChannel.setStreamHandler(stateStreamHandler);
Expand All @@ -84,7 +85,12 @@ public void onMethodCall(MethodCall call, Result result) {
break;

case "isOn":
result.success(mBluetoothAdapter.isEnabled());
try {
assert mBluetoothAdapter != null;
result.success(mBluetoothAdapter.isEnabled());
} catch (Exception ex) {
result.error("Error", ex.getMessage(), ex);
}
break;

case "isConnected":
Expand All @@ -108,7 +114,7 @@ public void onMethodCall(MethodCall call, Result result) {
getBondedDevices(result);

} catch (Exception ex) {
result.error("Erro", ex.getMessage(), ex);
result.error("Error", ex.getMessage(), ex);
}

break;
Expand Down
44 changes: 39 additions & 5 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';

void main() => runApp(new MyApp());

///
///
///
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}

///
///
///
class _MyAppState extends State<MyApp> {
static final TextEditingController _message = new TextEditingController();
static final TextEditingController _text = new TextEditingController();
Expand All @@ -22,12 +28,18 @@ class _MyAppState extends State<MyApp> {
bool _connected = false;
bool _pressed = false;

///
///
///
@override
void initState() {
initPlatformState();
super.initState();
initPlatformState();
}

///
///
///
Future<void> initPlatformState() async {
List<BluetoothDevice> devices = [];

Expand Down Expand Up @@ -71,6 +83,9 @@ class _MyAppState extends State<MyApp> {
});
}

///
///
///
@override
Widget build(BuildContext context) {
return MaterialApp(
Expand All @@ -82,7 +97,7 @@ class _MyAppState extends State<MyApp> {
child: ListView(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 10.0, top: 10.0, right: 10.0),
padding: const EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 0.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expand All @@ -98,14 +113,15 @@ class _MyAppState extends State<MyApp> {
value: _device,
),
RaisedButton(
onPressed: _pressed ? null : _connected ? _disconnect : _connect,
onPressed:
_pressed ? null : _connected ? _disconnect : _connect,
child: Text(_connected ? 'Disconnect' : 'Connect'),
),
],
),
),
Padding(
padding: const EdgeInsets.only(left: 10.0, top: 6.0, right: 10.0),
padding: const EdgeInsets.fromLTRB(10.0, 6.0, 10.0, 0.0),
child: Row(
children: <Widget>[
Expanded(
Expand Down Expand Up @@ -145,6 +161,9 @@ class _MyAppState extends State<MyApp> {
);
}

///
///
///
List<DropdownMenuItem<BluetoothDevice>> _getDeviceItems() {
List<DropdownMenuItem<BluetoothDevice>> items = [];
if (_devices.isEmpty) {
Expand All @@ -162,6 +181,9 @@ class _MyAppState extends State<MyApp> {
return items;
}

///
///
///
void _connect() {
if (_device == null) {
show('No device selected.');
Expand All @@ -177,11 +199,17 @@ class _MyAppState extends State<MyApp> {
}
}

///
///
///
void _disconnect() {
bluetooth.disconnect();
setState(() => _pressed = true);
}

///
///
///
void _writeTest() {
bluetooth.isConnected.then((isConnected) {
if (isConnected) {
Expand All @@ -190,7 +218,13 @@ class _MyAppState extends State<MyApp> {
});
}

Future show(String message, {Duration duration: const Duration(seconds: 3)}) async {
///
///
///
Future show(
String message, {
Duration duration: const Duration(seconds: 3),
}) async {
await new Future.delayed(new Duration(milliseconds: 100));
Scaffold.of(context).showSnackBar(
new SnackBar(
Expand Down
36 changes: 26 additions & 10 deletions lib/flutter_bluetooth_serial.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import 'dart:async';

import 'package:flutter/services.dart';

///
///
///
class FlutterBluetoothSerial {
static const int STATE_OFF = 10;
static const int STATE_TURNING_ON = 11;
Expand All @@ -16,13 +19,17 @@ class FlutterBluetoothSerial {

static const String namespace = 'flutter_bluetooth_serial';

static const MethodChannel _channel = const MethodChannel('$namespace/methods');
static const MethodChannel _channel =
const MethodChannel('$namespace/methods');

static const EventChannel _readChannel = const EventChannel('$namespace/read');
static const EventChannel _readChannel =
const EventChannel('$namespace/read');

static const EventChannel _stateChannel = const EventChannel('$namespace/state');
static const EventChannel _stateChannel =
const EventChannel('$namespace/state');

final StreamController<MethodCall> _methodStreamController = new StreamController.broadcast();
final StreamController<MethodCall> _methodStreamController =
new StreamController.broadcast();

Stream<MethodCall> get _methodStream => _methodStreamController.stream;

Expand All @@ -36,28 +43,37 @@ class FlutterBluetoothSerial {

static FlutterBluetoothSerial get instance => _instance;

Stream<int> onStateChanged() => _stateChannel.receiveBroadcastStream().map((buffer) => buffer);
Stream<int> onStateChanged() =>
_stateChannel.receiveBroadcastStream().map((buffer) => buffer);

Stream<String> onRead() => _readChannel.receiveBroadcastStream().map((buffer) => buffer.toString());
Stream<String> onRead() =>
_readChannel.receiveBroadcastStream().map((buffer) => buffer.toString());

Future<bool> get isAvailable async => await _channel.invokeMethod('isAvailable');
Future<bool> get isAvailable async =>
await _channel.invokeMethod('isAvailable');

Future<bool> get isOn async => await _channel.invokeMethod('isOn');

Future<bool> get isConnected async => await _channel.invokeMethod('isConnected');
Future<bool> get isConnected async =>
await _channel.invokeMethod('isConnected');

Future<List> getBondedDevices() async {
final List list = await _channel.invokeMethod('getBondedDevices');
return list.map((map) => BluetoothDevice.fromMap(map)).toList();
}

Future<dynamic> connect(BluetoothDevice device) => _channel.invokeMethod('connect', device.toMap());
Future<dynamic> connect(BluetoothDevice device) =>
_channel.invokeMethod('connect', device.toMap());

Future<dynamic> disconnect() => _channel.invokeMethod('disconnect');

Future<dynamic> write(String message) => _channel.invokeMethod('write', {'message': message});
Future<dynamic> write(String message) =>
_channel.invokeMethod('write', {'message': message});
}

///
///
///
class BluetoothDevice {
final String name;
final String address;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_bluetooth_serial
description: A basic Flutter Bluetooth Serial
version: 0.0.1
version: 0.0.2
author: Eduardo Folly <[email protected]>
homepage: https://github.com/edufolly/flutter_bluetooth_serial

Expand Down

0 comments on commit 50688ef

Please sign in to comment.