@@ -45,9 +45,10 @@ type WebBleInternalDevice = {
45
45
46
46
@injectable ( )
47
47
export class WebBleTransport implements Transport {
48
+ private _connectedDevices : Array < BluetoothDevice > ;
48
49
private _internalDevicesById : Map < DeviceId , WebBleInternalDevice > ;
49
- private _deviceConnectionById : Map < string , BleDeviceConnection > ;
50
- private _disconnectionHandlersById : Map < string , ( ) => void > ;
50
+ private _deviceConnectionById : Map < DeviceId , BleDeviceConnection > ;
51
+ private _disconnectionHandlersById : Map < DeviceId , ( ) => void > ;
51
52
private _logger : LoggerPublisherService ;
52
53
private readonly connectionType : ConnectionType = "BLE" ;
53
54
private readonly identifier : TransportIdentifier = BuiltinTransports . BLE ;
@@ -60,6 +61,7 @@ export class WebBleTransport implements Transport {
60
61
@inject ( bleDiTypes . BleDeviceConnectionFactory )
61
62
private _bleDeviceConnectionFactory : BleDeviceConnectionFactory ,
62
63
) {
64
+ this . _connectedDevices = [ ] ;
63
65
this . _internalDevicesById = new Map ( ) ;
64
66
this . _deviceConnectionById = new Map ( ) ;
65
67
this . _disconnectionHandlersById = new Map ( ) ;
@@ -181,10 +183,14 @@ export class WebBleTransport implements Transport {
181
183
bleDevice : BluetoothDevice ,
182
184
bleGattService : BluetoothRemoteGATTService ,
183
185
bleDeviceInfos : BleDeviceInfos ,
184
- ) {
186
+ ) : InternalDiscoveredDevice {
187
+ if ( this . _connectedDevices . includes ( bleDevice ) ) {
188
+ this . _logger . debug ( "Device already discovered" ) ;
189
+ throw new Error ( "Device already discovered" ) ;
190
+ }
185
191
const id = uuid ( ) ;
186
192
187
- const discoveredDevice = {
193
+ const discoveredDevice : InternalDiscoveredDevice = {
188
194
id,
189
195
deviceModel : bleDeviceInfos . deviceModel ,
190
196
transport : this . identifier ,
@@ -202,7 +208,6 @@ export class WebBleTransport implements Transport {
202
208
`Discovered device ${ id } ${ discoveredDevice . deviceModel . productName } ` ,
203
209
) ;
204
210
this . _internalDevicesById . set ( id , internalDevice ) ;
205
-
206
211
return discoveredDevice ;
207
212
}
208
213
@@ -287,35 +292,42 @@ export class WebBleTransport implements Transport {
287
292
discoveredDevice : { deviceModel } ,
288
293
} = internalDevice ;
289
294
290
- const [ writeCharacteristic , notifyCharacteristic ] = await Promise . all ( [
291
- internalDevice . bleGattService . getCharacteristic (
292
- internalDevice . bleDeviceInfos . writeUuid ,
293
- ) ,
294
- internalDevice . bleGattService . getCharacteristic (
295
- internalDevice . bleDeviceInfos . notifyUuid ,
296
- ) ,
297
- ] ) ;
298
-
299
- const deviceConnection = this . _bleDeviceConnectionFactory . create (
300
- writeCharacteristic ,
301
- notifyCharacteristic ,
302
- ) ;
303
- await deviceConnection . setup ( ) ;
304
- this . _deviceConnectionById . set ( internalDevice . id , deviceConnection ) ;
305
- const connectedDevice = new InternalConnectedDevice ( {
306
- sendApdu : ( apdu , triggersDisconnection ) =>
307
- deviceConnection . sendApdu ( apdu , triggersDisconnection ) ,
308
- deviceModel,
309
- id : deviceId ,
310
- type : this . connectionType ,
311
- transport : this . identifier ,
312
- } ) ;
313
- internalDevice . bleDevice . ongattserverdisconnected =
314
- this . _getDeviceDisconnectedHandler ( internalDevice , deviceConnection ) ;
315
- this . _disconnectionHandlersById . set ( internalDevice . id , ( ) => {
316
- this . disconnect ( { connectedDevice } ) . then ( ( ) => onDisconnect ( deviceId ) ) ;
317
- } ) ;
318
- return Right ( connectedDevice ) ;
295
+ try {
296
+ const [ writeCharacteristic , notifyCharacteristic ] = await Promise . all ( [
297
+ internalDevice . bleGattService . getCharacteristic (
298
+ internalDevice . bleDeviceInfos . writeUuid ,
299
+ ) ,
300
+ internalDevice . bleGattService . getCharacteristic (
301
+ internalDevice . bleDeviceInfos . notifyUuid ,
302
+ ) ,
303
+ ] ) ;
304
+ const deviceConnection = this . _bleDeviceConnectionFactory . create (
305
+ writeCharacteristic ,
306
+ notifyCharacteristic ,
307
+ ) ;
308
+ this . _deviceConnectionById . set ( internalDevice . id , deviceConnection ) ;
309
+ await deviceConnection . setup ( ) ;
310
+ const connectedDevice = new InternalConnectedDevice ( {
311
+ sendApdu : ( apdu , triggersDisconnection ) =>
312
+ deviceConnection . sendApdu ( apdu , triggersDisconnection ) ,
313
+ deviceModel,
314
+ id : deviceId ,
315
+ type : this . connectionType ,
316
+ transport : this . identifier ,
317
+ } ) ;
318
+ internalDevice . bleDevice . ongattserverdisconnected =
319
+ this . _getDeviceDisconnectedHandler ( internalDevice , deviceConnection ) ;
320
+ this . _disconnectionHandlersById . set ( internalDevice . id , ( ) => {
321
+ this . disconnect ( { connectedDevice } ) . then ( ( ) => onDisconnect ( deviceId ) ) ;
322
+ } ) ;
323
+ this . _connectedDevices . push ( internalDevice . bleDevice ) ;
324
+ return Right ( connectedDevice ) ;
325
+ } catch ( error ) {
326
+ this . _logger . error ( "Error while getting characteristics" , {
327
+ data : { error } ,
328
+ } ) ;
329
+ return Left ( new OpeningConnectionError ( error ) ) ;
330
+ }
319
331
}
320
332
321
333
/**
0 commit comments