Last active
October 30, 2019 09:42
-
-
Save oflarcade/22976f0428704a1a34ad44bfbaf83df2 to your computer and use it in GitHub Desktop.
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
.... | |
import BleManager from 'react-native-ble-manager'; | |
const bleManagerEmitter = new NativeEventEmitter(BleManagerModule); | |
... | |
componentDidMount() { | |
AppState.addEventListener('change', this.handleAppStateChange); | |
BleManager.start({showAlert: false}); | |
this.handlerDiscover = bleManagerEmitter.addListener( | |
'BleManagerDiscoverPeripheral', | |
this.handleDiscoverPeripheral, | |
); | |
this.handlerStop = bleManagerEmitter.addListener( | |
'BleManagerStopScan', | |
this.handleStopScan, | |
); | |
this.handlerDisconnect = bleManagerEmitter.addListener( | |
'BleManagerDisconnectPeripheral', | |
this.handleDisconnectedPeripheral, | |
); | |
this.handlerUpdate = bleManagerEmitter.addListener( | |
'BleManagerDidUpdateValueForCharacteristic', | |
this.handleUpdateValueForCharacteristic, | |
); | |
if (Platform.OS === 'android' && Platform.Version >= 23) { | |
PermissionsAndroid.check( | |
PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION, | |
).then(result => { | |
if (result) { | |
console.log('Permission is OK'); | |
} else { | |
PermissionsAndroid.requestPermission( | |
PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION, | |
).then(result => { | |
if (result) { | |
console.log('User accept'); | |
} else { | |
console.log('User refuse'); | |
} | |
}); | |
} | |
}); | |
} | |
} | |
handleAppStateChange(nextAppState) { | |
if ( | |
this.state.appState.match(/inactive|background/) && | |
nextAppState === 'active' | |
) { | |
console.log('App has come to the foreground!'); | |
BleManager.getConnectedPeripherals([]).then(peripheralsArray => { | |
console.log('Connected peripherals: ' + peripheralsArray.length); | |
}); | |
} | |
this.setState({appState: nextAppState}); | |
} | |
componentWillUnmount() { | |
this.handlerDiscover.remove(); | |
this.handlerStop.remove(); | |
this.handlerDisconnect.remove(); | |
this.handlerUpdate.remove(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment