diff --git a/CHANGELOG.md b/CHANGELOG.md index 310bf8c4..10d75246 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. \ No newline at end of file diff --git a/android/src/main/java/io/github/edufolly/flutterbluetoothserial/FlutterBluetoothSerialPlugin.java b/android/src/main/java/io/github/edufolly/flutterbluetoothserial/FlutterBluetoothSerialPlugin.java index d5664bba..971782ed 100644 --- a/android/src/main/java/io/github/edufolly/flutterbluetoothserial/FlutterBluetoothSerialPlugin.java +++ b/android/src/main/java/io/github/edufolly/flutterbluetoothserial/FlutterBluetoothSerialPlugin.java @@ -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); @@ -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": @@ -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; diff --git a/example/lib/main.dart b/example/lib/main.dart index 977bd839..b6e574f1 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -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 { static final TextEditingController _message = new TextEditingController(); static final TextEditingController _text = new TextEditingController(); @@ -22,12 +28,18 @@ class _MyAppState extends State { bool _connected = false; bool _pressed = false; + /// + /// + /// @override void initState() { - initPlatformState(); super.initState(); + initPlatformState(); } + /// + /// + /// Future initPlatformState() async { List devices = []; @@ -71,6 +83,9 @@ class _MyAppState extends State { }); } + /// + /// + /// @override Widget build(BuildContext context) { return MaterialApp( @@ -82,7 +97,7 @@ class _MyAppState extends State { child: ListView( children: [ 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: [ @@ -98,14 +113,15 @@ class _MyAppState extends State { 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: [ Expanded( @@ -145,6 +161,9 @@ class _MyAppState extends State { ); } + /// + /// + /// List> _getDeviceItems() { List> items = []; if (_devices.isEmpty) { @@ -162,6 +181,9 @@ class _MyAppState extends State { return items; } + /// + /// + /// void _connect() { if (_device == null) { show('No device selected.'); @@ -177,11 +199,17 @@ class _MyAppState extends State { } } + /// + /// + /// void _disconnect() { bluetooth.disconnect(); setState(() => _pressed = true); } + /// + /// + /// void _writeTest() { bluetooth.isConnected.then((isConnected) { if (isConnected) { @@ -190,7 +218,13 @@ class _MyAppState extends State { }); } - 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( diff --git a/lib/flutter_bluetooth_serial.dart b/lib/flutter_bluetooth_serial.dart index 73a6da85..d548e697 100644 --- a/lib/flutter_bluetooth_serial.dart +++ b/lib/flutter_bluetooth_serial.dart @@ -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; @@ -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 _methodStreamController = new StreamController.broadcast(); + final StreamController _methodStreamController = + new StreamController.broadcast(); Stream get _methodStream => _methodStreamController.stream; @@ -36,28 +43,37 @@ class FlutterBluetoothSerial { static FlutterBluetoothSerial get instance => _instance; - Stream onStateChanged() => _stateChannel.receiveBroadcastStream().map((buffer) => buffer); + Stream onStateChanged() => + _stateChannel.receiveBroadcastStream().map((buffer) => buffer); - Stream onRead() => _readChannel.receiveBroadcastStream().map((buffer) => buffer.toString()); + Stream onRead() => + _readChannel.receiveBroadcastStream().map((buffer) => buffer.toString()); - Future get isAvailable async => await _channel.invokeMethod('isAvailable'); + Future get isAvailable async => + await _channel.invokeMethod('isAvailable'); Future get isOn async => await _channel.invokeMethod('isOn'); - Future get isConnected async => await _channel.invokeMethod('isConnected'); + Future get isConnected async => + await _channel.invokeMethod('isConnected'); Future getBondedDevices() async { final List list = await _channel.invokeMethod('getBondedDevices'); return list.map((map) => BluetoothDevice.fromMap(map)).toList(); } - Future connect(BluetoothDevice device) => _channel.invokeMethod('connect', device.toMap()); + Future connect(BluetoothDevice device) => + _channel.invokeMethod('connect', device.toMap()); Future disconnect() => _channel.invokeMethod('disconnect'); - Future write(String message) => _channel.invokeMethod('write', {'message': message}); + Future write(String message) => + _channel.invokeMethod('write', {'message': message}); } +/// +/// +/// class BluetoothDevice { final String name; final String address; diff --git a/pubspec.yaml b/pubspec.yaml index 11b70b99..86cdb057 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 homepage: https://github.com/edufolly/flutter_bluetooth_serial