part of flutter_bluetooth; class BluetoothSocket { static const int _MESSAGE_READ = 1; static const int _MESSAGE_EXCEPTIONS = 2; static const int _MESSAGE_SUCCESS = 3; static const int _MESSAGE_CLOSE = 4; final String _address; String get address => _address; final String _uuid; String get uuid => _uuid; final StreamController _readController = StreamController(); Stream get readStream => _readController.stream; final StreamController _closeController = StreamController(); Stream get closeStream => _closeController.stream; BluetoothSocketStatusEnum _status = BluetoothSocketStatusEnum.Connected; BluetoothSocketStatusEnum get status => _status; BluetoothSocket._(this._address, this._uuid) { this._handlerEvent(); } write(Uint8List data) async { await FlutterBluetooth.instance._channel.invokeMethod( 'write', {'address': address, 'uuid': uuid, 'data': data}); } close() async { await FlutterBluetooth.instance._channel .invokeMethod('close', {'address': address, 'uuid': uuid}); } _handlerEvent() { EventChannel('flutter_bluetooth/connectedEvent$address-$uuid') .receiveBroadcastStream() .takeUntil(closeStream) .listen((event) { switch (event['type']) { case _MESSAGE_READ: this._readController.add(event['data']); break; case _MESSAGE_CLOSE: _status = BluetoothSocketStatusEnum.Disconnect; _closeController.add(null); break; default: } }); } } enum BluetoothSocketStatusEnum { Connected, Disconnect }