-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9369d07
commit dc6c6ab
Showing
7 changed files
with
3,432 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from './provider'; | ||
export * from './types'; | ||
export * from './scan-devices'; | ||
export * from './try-to-init-bt'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import {Observable, Subscription} from 'rxjs'; | ||
import TransportBLE from '@ledgerhq/react-native-hw-transport-ble'; | ||
import EventEmitter from 'events'; | ||
|
||
export function scanDevices() { | ||
let sub: null | Subscription = null | ||
let transport: null | Subscription = null | ||
|
||
const emitter = new EventEmitter(); | ||
|
||
const listen = () => { | ||
try { | ||
if (transport) { | ||
transport.unsubscribe(); | ||
} | ||
|
||
transport = new Observable(TransportBLE.listen).subscribe({ | ||
complete: () => { | ||
emitter.emit('complete', true); | ||
}, | ||
next: e => { | ||
if (e.type === 'add') { | ||
emitter.emit('device', e.descriptor) | ||
} | ||
}, | ||
error: e => { | ||
emitter.emit('error', e); | ||
emitter.emit('complete', true); | ||
}, | ||
}); | ||
} catch (e) { | ||
emitter.emit('error', e); | ||
} | ||
} | ||
|
||
return { | ||
start() { | ||
let previousAvailable: boolean = false; | ||
sub = new Observable<{available: boolean}>( | ||
TransportBLE.observeState, | ||
).subscribe(e => { | ||
if (e.available !== previousAvailable) { | ||
previousAvailable = e.available; | ||
if (e.available) { | ||
listen(); | ||
} | ||
} | ||
}); | ||
|
||
listen(); | ||
}, | ||
stop() { | ||
if(sub) { | ||
sub.unsubscribe(); | ||
} | ||
if (transport) { | ||
transport.unsubscribe(); | ||
} | ||
}, | ||
on: emitter.on.bind(emitter), | ||
off: emitter.off.bind(emitter) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {BleManager, State, Subscription as BleSub} from 'react-native-ble-plx'; | ||
import {PermissionsAndroid, Platform} from 'react-native'; | ||
import {Observable} from 'rxjs'; | ||
|
||
export async function tryToInitBt(): Promise<Observable<State>> { | ||
if (Platform.OS === 'android') { | ||
await PermissionsAndroid.requestMultiple([ | ||
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, | ||
PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN, | ||
PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT, | ||
PermissionsAndroid.PERMISSIONS.BLUETOOTH_ADVERTISE, | ||
]); | ||
} | ||
const manager = new BleManager(); | ||
let sub: BleSub; | ||
let previousState = State.Unknown; | ||
return new Observable(observer => { | ||
const subs = (state: State) => { | ||
if (state !== previousState) { | ||
previousState = state; | ||
observer.next(state); | ||
} | ||
} | ||
|
||
manager.state().then(subs); | ||
sub = manager.onStateChange(subs, true); | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.