@@ -187,34 +187,40 @@ class BluetoothAudioService {
187187 }
188188 }
189189
190+ private addEventListener ( listener : { remove : ( ) => void } ) : void {
191+ if ( ! this . eventListeners . includes ( listener ) ) {
192+ this . eventListeners . push ( listener ) ;
193+ }
194+ }
195+
196+ private removeEventListener ( listener : { remove : ( ) => void } | null ) : void {
197+ if ( ! listener ) {
198+ return ;
199+ }
200+
201+ this . eventListeners = this . eventListeners . filter ( ( registeredListener ) => registeredListener !== listener ) ;
202+ }
203+
190204 private setupEventListeners ( ) : void {
191205 // Bluetooth state change listener
192- //const stateListener = DeviceEventEmitter.addListener('BleManagerDidUpdateState', this.handleBluetoothStateChange.bind(this));
193206 const stateListener = BleManager . onDidUpdateState ( this . handleBluetoothStateChange . bind ( this ) ) ;
194- this . eventListeners . push ( stateListener ) ;
207+ this . addEventListener ( stateListener ) ;
195208
196209 // Device disconnection listener
197- //const disconnectListener = DeviceEventEmitter.addListener('BleManagerDisconnectPeripheral', this.handleDeviceDisconnected.bind(this));
198210 const disconnectListener = BleManager . onDisconnectPeripheral ( this . handleDeviceDisconnected . bind ( this ) ) ;
199- this . eventListeners . push ( disconnectListener ) ;
211+ this . addEventListener ( disconnectListener ) ;
200212
201213 // Device discovered listener
202- //const discoverListener = DeviceEventEmitter.addListener('BleManagerDiscoverPeripheral', this.handleDeviceDiscovered.bind(this));
203214 const discoverListener = BleManager . onDiscoverPeripheral ( this . handleDeviceDiscovered . bind ( this ) ) ;
204- this . eventListeners . push ( discoverListener ) ;
215+ this . addEventListener ( discoverListener ) ;
205216
206217 // Characteristic value update listener
207- //const valueUpdateListener = DeviceEventEmitter.addListener('BleManagerDidUpdateValueForCharacteristic', this.handleCharacteristicValueUpdate.bind(this));
208218 const valueUpdateListener = BleManager . onDidUpdateValueForCharacteristic ( this . handleCharacteristicValueUpdate . bind ( this ) ) ;
209- this . eventListeners . push ( valueUpdateListener ) ;
210-
211- const legacyValueUpdateListener = DeviceEventEmitter . addListener ( 'BleManagerDidUpdateValueForCharacteristic' , this . handleCharacteristicValueUpdate . bind ( this ) ) ;
212- this . eventListeners . push ( legacyValueUpdateListener ) ;
219+ this . addEventListener ( valueUpdateListener ) ;
213220
214221 // Stop scan listener
215- //const stopScanListener = DeviceEventEmitter.addListener('BleManagerStopScan', this.handleScanStopped.bind(this));
216222 const stopScanListener = BleManager . onStopScan ( this . handleScanStopped . bind ( this ) ) ;
217- this . eventListeners . push ( stopScanListener ) ;
223+ this . addEventListener ( stopScanListener ) ;
218224 }
219225
220226 private handleBluetoothStateChange ( args : { state : BleState } ) : void {
@@ -1126,11 +1132,12 @@ class BluetoothAudioService {
11261132 }
11271133
11281134 this . mediaButtonEventListener = DeviceEventEmitter . addListener ( 'onMediaButtonEvent' , this . handleMediaButtonFallbackEvent . bind ( this ) ) ;
1129- this . eventListeners . push ( this . mediaButtonEventListener ) ;
1135+ this . addEventListener ( this . mediaButtonEventListener ) ;
11301136 }
11311137
11321138 private stopMediaButtonFallbackMonitoring ( ) : void {
11331139 if ( this . mediaButtonEventListener ) {
1140+ this . removeEventListener ( this . mediaButtonEventListener ) ;
11341141 this . mediaButtonEventListener . remove ( ) ;
11351142 this . mediaButtonEventListener = null ;
11361143 }
@@ -1522,7 +1529,7 @@ class BluetoothAudioService {
15221529 return false ;
15231530 }
15241531
1525- return value === true || value === 1 || normalizedValue === normalizedKey || normalizedValue === 'true' || normalizedValue === '1' || normalizedValue . length > 0 ;
1532+ return value === true || value === 1 || normalizedValue === normalizedKey || normalizedValue === 'true' || normalizedValue === '1' ;
15261533 } ) ;
15271534 }
15281535
@@ -1558,7 +1565,7 @@ class BluetoothAudioService {
15581565 return false ;
15591566 }
15601567
1561- return value === true || value === 1 || normalizedValue === normalizedKey || normalizedValue === 'true' || normalizedValue === '1' || normalizedValue . length > 0 ;
1568+ return value === true || value === 1 || normalizedValue === normalizedKey || normalizedValue === 'true' || normalizedValue === '1' ;
15621569 } ) ;
15631570 }
15641571
0 commit comments