@@ -498,10 +498,13 @@ function initLeScanCallback() {
498498 super ( {
499499 onLeScan : function ( device : android . bluetooth . BluetoothDevice , rssi : number , data : number [ ] ) {
500500 CLog ( CLogTypes . info , `TNS_LeScanCallback.onLeScan ---- device: ${ device } , rssi: ${ rssi } , scanRecord: ${ data } ` ) ;
501-
502- let stateObject = this . owner . get ( ) . connections [ device . getAddress ( ) ] ;
501+ const owner = this . owner && this . owner . get ( ) ;
502+ if ( ! owner ) {
503+ return ;
504+ }
505+ let stateObject = owner . connections [ device . getAddress ( ) ] ;
503506 if ( ! stateObject ) {
504- stateObject = this . owner . get ( ) . connections [ device . getAddress ( ) ] = {
507+ stateObject = owner . connections [ device . getAddress ( ) ] = {
505508 state : 'disconnected' ,
506509 } ;
507510 const scanRecord = parseFromBytes ( data ) ;
@@ -519,7 +522,7 @@ function initLeScanCallback() {
519522 } ;
520523 CLog ( CLogTypes . info , `TNS_LeScanCallback.onLeScan ---- payload: ${ JSON . stringify ( payload ) } ` ) ;
521524 this . onPeripheralDiscovered && this . onPeripheralDiscovered ( payload ) ;
522- this . owner . get ( ) . sendEvent ( Bluetooth . device_discovered_event , payload ) ;
525+ owner . sendEvent ( Bluetooth . device_discovered_event , payload ) ;
523526 }
524527 } ,
525528 } ) ;
@@ -591,9 +594,13 @@ function initScanCallback() {
591594 */
592595 onScanResult ( callbackType : number , result : android . bluetooth . le . ScanResult ) {
593596 CLog ( CLogTypes . info , `TNS_ScanCallback.onScanResult ---- callbackType: ${ callbackType } , result: ${ result } ` ) ;
594- let stateObject = this . owner . get ( ) . connections [ result . getDevice ( ) . getAddress ( ) ] ;
597+ const owner = this . owner && this . owner . get ( ) ;
598+ if ( ! owner ) {
599+ return ;
600+ }
601+ let stateObject = owner . connections [ result . getDevice ( ) . getAddress ( ) ] ;
595602 if ( ! stateObject ) {
596- stateObject = this . owner . get ( ) . connections [ result . getDevice ( ) . getAddress ( ) ] = {
603+ stateObject = owner . connections [ result . getDevice ( ) . getAddress ( ) ] = {
597604 state : 'disconnected' ,
598605 } ;
599606 }
@@ -612,7 +619,7 @@ function initScanCallback() {
612619 } ;
613620 CLog ( CLogTypes . info , `TNS_ScanCallback.onScanResult ---- payload: ${ JSON . stringify ( payload ) } ` ) ;
614621 this . onPeripheralDiscovered && this . onPeripheralDiscovered ( payload ) ;
615- this . owner . get ( ) . sendEvent ( Bluetooth . device_discovered_event , payload ) ;
622+ owner . sendEvent ( Bluetooth . device_discovered_event , payload ) ;
616623 }
617624 }
618625
@@ -734,21 +741,22 @@ function initBluetoothGattCallback() {
734741 d . onConnectionStateChange ( gatt , status , newState ) ;
735742 }
736743 } ) ;
737- if ( newState === android . bluetooth . BluetoothProfile . STATE_CONNECTED && status === GATT_SUCCESS ) {
744+ const owner = this . owner && this . owner . get ( ) ;
745+ if ( owner && newState === android . bluetooth . BluetoothProfile . STATE_CONNECTED && status === GATT_SUCCESS ) {
738746 const device = gatt . getDevice ( ) ;
739747 let address : string = null ;
740748 if ( device == null ) {
741749 // happens some time, why ... ?
742750 } else {
743751 address = device . getAddress ( ) ;
744752 }
745- const stateObject = this . owner . get ( ) . connections [ address ] ;
753+ const stateObject = owner . connections [ address ] ;
746754 if ( ! stateObject ) {
747- this . owner . get ( ) . gattDisconnect ( gatt ) ;
755+ owner . gattDisconnect ( gatt ) ;
748756 }
749757 } else {
750758 // perhaps the device was manually disconnected, or in use by another device
751- this . owner . get ( ) . gattDisconnect ( gatt ) ;
759+ owner . gattDisconnect ( gatt ) ;
752760 }
753761 }
754762
@@ -801,8 +809,11 @@ function initBluetoothGattCallback() {
801809 d . onCharacteristicChanged ( gatt , characteristic ) ;
802810 }
803811 } ) ;
804-
805- const stateObject = this . owner . get ( ) . connections [ address ] ;
812+ const owner = this . owner && this . owner . get ( ) ;
813+ if ( ! owner ) {
814+ return ;
815+ }
816+ const stateObject = owner . connections [ address ] ;
806817 if ( stateObject ) {
807818 const cUUID = uuidToString ( characteristic . getUuid ( ) ) ;
808819 const sUUID = uuidToString ( characteristic . getService ( ) . getUuid ( ) ) ;
0 commit comments