@@ -61,41 +61,42 @@ Future<void> _onConnectionStateChangedListener(
6161 bool isConnected,
6262 String ? error,
6363) async {
64- BleDevice ? bluetoothDevice = await getBleDeviceByID (id);
65- _logger.info ('name: ${bluetoothDevice ?.name } connected: $isConnected ' );
66- if (isConnected &&
67- bluetoothDevice != null &&
68- bluetoothDevice.name != null &&
69- bluetoothDevice.name! .trim ().isEmpty) {
70- _logger.warning ("Disconnecting from BLE device with blank platform name" );
71- disconnect (id);
72- }
7364 Map <String , StatefulDevice > knownDevices = KnownDevices .instance.state;
74-
75- StoredDevice storedDevice;
7665 StatefulDevice statefulDevice;
7766 //get existing entry
7867 if (knownDevices.containsKey (id)) {
7968 statefulDevice = knownDevices[id]! ;
80- storedDevice = statefulDevice.storedDevice;
8169 } else {
82- // Don't create a new entry on device disconnect if the stored device
83- // doesn't exist. Stops forgotten gear from immediately being repaired
84- if (! isConnected) {
85- return ;
86- }
87- if (bluetoothDevice == null ) {
88- _logger.severe ("BLEDevice not found for ID: $id " );
89- return ;
70+ if (isConnected) {
71+ await disconnect (id);
9072 }
91- DeviceDefinition ? deviceDefinition = DeviceRegistry .getByName (
92- bluetoothDevice.name! ,
93- );
73+ return ;
74+ }
75+ statefulDevice.deviceConnectionState.value = isConnected
76+ ? ConnectivityState .connected
77+ : ConnectivityState .disconnected;
78+ if (isConnected) {
79+ await discoverServices (id);
80+ int mtu = await UniversalBle .requestMtu (id, 512 );
81+ statefulDevice.mtu.value = mtu;
82+ }
83+ }
84+
85+ /// Create a new Stored/Stateful device entry if it doesn't exist and try to connect
86+ Future <void > createAndConnect (String id, String name) async {
87+ Map <String , StatefulDevice > knownDevices = KnownDevices .instance.state;
88+ StatefulDevice statefulDevice;
89+ //get existing entry
90+ if (knownDevices.containsKey (id)) {
91+ statefulDevice = knownDevices[id]! ;
92+ } else {
93+ _logger.info ("Registering new device $name $id " );
94+ DeviceDefinition ? deviceDefinition = DeviceRegistry .getByName (name);
9495 if (deviceDefinition == null ) {
95- _logger.severe ("Unknown device found: ${ bluetoothDevice . name } " );
96+ _logger.severe ("Unknown device found: $name " );
9697 return ;
9798 }
98- storedDevice = StoredDevice (
99+ StoredDevice storedDevice = StoredDevice (
99100 deviceDefinition.uuid,
100101 id,
101102 deviceDefinition.deviceType.color ().toARGB32 (),
@@ -104,18 +105,11 @@ Future<void> _onConnectionStateChangedListener(
104105 statefulDevice = StatefulDevice (deviceDefinition, storedDevice);
105106 await KnownDevices .instance.add (statefulDevice);
106107 }
107- statefulDevice.deviceConnectionState.value = isConnected
108- ? ConnectivityState .connected
109- : ConnectivityState .disconnected;
110- if (isConnected && bluetoothDevice != null ) {
111- await discoverServices (bluetoothDevice);
112- int mtu = await bluetoothDevice.requestMtu (512 );
113- statefulDevice.mtu.value = mtu;
114- }
108+ await _connect (id);
115109}
116110
117- Future <void > discoverServices (BleDevice device ) async {
118- List <BleService > services = await device .discoverServices ();
111+ Future <void > discoverServices (String id ) async {
112+ List <BleService > services = await UniversalBle .discoverServices (id );
119113 List <BleCharacteristic > characteristics = services
120114 .map ((e) => e.characteristics)
121115 .flattened
@@ -128,8 +122,7 @@ Future<void> discoverServices(BleDevice device) async {
128122 element.bleDeviceService.toLowerCase () == service.uuid.toLowerCase (),
129123 );
130124 if (bluetoothUartService != null ) {
131- StatefulDevice ? statefulDevice =
132- KnownDevices .instance.state[device.deviceId];
125+ StatefulDevice ? statefulDevice = KnownDevices .instance.state[id];
133126 statefulDevice? .bluetoothUartService.value = bluetoothUartService;
134127 }
135128 }
@@ -183,30 +176,22 @@ Future<void> _onScanResultsListener(BleDevice scanResult) async {
183176 ! knownDevices[scanResult.deviceId]! .disableAutoConnect) {
184177 knownDevices[scanResult.deviceId]? .deviceConnectionState.value =
185178 ConnectivityState .connecting;
186- await connect (scanResult.deviceId);
179+ await _connect (scanResult.deviceId);
187180 }
188181}
189182
190183Future <void > disconnect (String id) async {
191184 if (! _didInitBle) {
192185 return ;
193186 }
194- BleDevice ? device = await getBleDeviceByID (id);
195- KnownDevices .instance.state[id]? .deviceConnectionState.value =
196- ConnectivityState .disconnected;
187+ StatefulDevice ? statefulDevice = KnownDevices .instance.state[id];
188+ statefulDevice? .deviceConnectionState.value = ConnectivityState .disconnected;
197189
198- if (device != null ) {
199- _logger.info ("disconnecting from ${device .name }" );
200- await device.disconnect ();
190+ if (statefulDevice != null && isDemoGear (statefulDevice)) {
191+ return ;
201192 }
202- }
203-
204- Future <BleDevice ?> getBleDeviceByID (String id) async {
205- List <BleDevice > connectedDevices = await UniversalBle .getSystemDevices ();
206- BleDevice ? device = connectedDevices.firstWhereOrNull (
207- (element) => element.deviceId == id,
208- );
209- return device;
193+ _logger.info ("disconnecting from $id " );
194+ await UniversalBle .disconnect (id);
210195}
211196
212197Future <void > forgetBond (String id) async {
@@ -217,14 +202,12 @@ Future<void> forgetBond(String id) async {
217202 if (! Platform .isAndroid) {
218203 return ;
219204 }
220- BleDevice ? device = await getBleDeviceByID (id);
221- if (device != null ) {
222- _logger.info ("forgetting ${device .name }" );
223- await device.unpair ();
224- }
205+ _logger.info ("forgetting $id " );
206+ await UniversalBle .unpair (id);
225207}
226208
227- Future <void > connect (String id) async {
209+ /// Attempt to connect to the ble mac address, tries a few times
210+ Future <void > _connect (String id) async {
228211 if (! _didInitBle) {
229212 return ;
230213 }
@@ -320,7 +303,7 @@ class Scan with ChangeNotifier {
320303 .disableAutoConnect,
321304 )
322305 .where ((element) => element.isSystemDevice == true )
323- .forEach ((bluetoothDevice) => connect (bluetoothDevice.deviceId)),
306+ .forEach ((bluetoothDevice) => _connect (bluetoothDevice.deviceId)),
324307 );
325308 // Or optionally add a scan filter
326309 await UniversalBle .startScan (
0 commit comments