123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- 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<Uint8List> _readController =
- StreamController<Uint8List>();
- Stream<Uint8List> 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._channel.invokeMethod(
- 'write', {'address': address, 'uuid': uuid, 'data': data});
- }
- close() async {
- await FlutterBluetooth._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 }
|