Skip to content

Commit

Permalink
simplify scan handling
Browse files Browse the repository at this point in the history
  • Loading branch information
alerighi committed Nov 3, 2023
1 parent 711d6f0 commit b04c0b9
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions library/src/NativeBleManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NativeEventEmitter, NativeModules } from 'react-native'
import { BleError, BleErrorCode } from './BleError'
import { NativeBleInterface, type INativeBleInterface } from './NativeBleInterface'
import { PromiseSequencer } from './PromiseSequencer'
import { BleError, BleErrorCode } from './BleError'
import {
BleCharacteristicProperty,
type BleCharacteristicInfo,
Expand All @@ -21,7 +21,7 @@ export class NativeBleManager implements BleManager {
private connectedDevice: BleConnectedDeviceInfo | null = null
private readonly sequencer = new PromiseSequencer()

private nScanActive = 0
private isScanActive = false
private nSubscriptions = new Map<string, number>()
private onErrorSubscription: Subscription | null = null

Expand Down Expand Up @@ -96,35 +96,45 @@ export class NativeBleManager implements BleManager {
},
})

this.nScanActive += 1
if (this.nScanActive === 1) {
this.logger?.info('[BleManager] starting scan...')
const startScan = async () => {
if (this.isScanActive) {
this.isScanActive = true

this.nativeInterface!.scanStart(serviceUuids?.map((s) => s.toLowerCase()))
.then(() => {
this.logger?.info('[BleManager] scan started')
})
.catch((e) => {
this.logger?.error('[BleManager] error starting scan')
this.nScanActive -= 1
onError?.(new BleError(e.code, e.message))
this.logger?.warn('[BleManager] scan already active, stopping previous one...')
await this.nativeInterface!.scanStop().catch((error) => {
this.logger?.error('[BleManager] error stopping scan', error)
})
}

await this.nativeInterface!.scanStart(serviceUuids?.map((s) => s.toLowerCase()))
}

startScan()
.then(() => {
this.logger?.info('[BleManager] scan started')
})
.catch((e) => {
this.logger?.error('[BleManager] error starting scan')
onError?.(new BleError(e.code, e.message))
this.isScanActive = false
})

return {
unsubscribe: () => {
subscription.unsubscribe()

this.nScanActive -= 1
if (this.nScanActive <= 0) {
this.logger?.info('[BleManager] stopping scan...')
this.logger?.info('[BleManager] stopping scan...')

this.nativeInterface!.scanStop().catch((e) => {
this.isScanActive = false
this.nativeInterface!.scanStop()
.catch((e) => {
this.logger?.error('[BleManager] error stopping scan')

onError?.(new BleError(e.code, e.message))
})
}
.then(() => {
this.logger?.info('[BleManager] scan stopped')
})
},
}
}
Expand Down

0 comments on commit b04c0b9

Please sign in to comment.