Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
useEffect(() => {
console.log("new blestate", bleState);
if (bleState == "permOk") {
} else if (bleState == "started") {
changePeripherals({"action": "clear"});
BleManager.getConnectedPeripherals([]).then((peripheralsArray) => {
for (p of peripheralsArray) {
console.log(p);
p.connected = true;
BleManager.connect(p.id).then(() => {
BleManager.retrieveServices(p.id).then((peripheralInfo) => {
console.log(peripheralInfo);
p.advertising.serviceUUIDs = [peripheralInfo.services[2].uuid];
changePeripherals({"action": "add", "peripheral": p});
});
});
}
console.log('Connected peripherals: ' + peripheralsArray.length);
});
changePeripherals({"action": "scan"});
} else if (bleState == "disconnected") {
// set a timeout and try to reconnect
retrieveConnected(){
BleManager.getConnectedPeripherals([]).then((results) => {
if (results.length == 0) {
console.log('No connected peripherals')
}
console.log(results);
var peripherals = this.state.peripherals;
for (var i = 0; i < results.length; i++) {
var peripheral = results[i];
peripheral.connected = true;
peripherals.set(peripheral.id, peripheral);
this.setState({ peripherals });
}
});
}
getConnectedPeripherals() {
BleManager.getConnectedPeripherals([])
.then((peripheralsArray) => {
console.log('Connected peripherals: ', peripheralsArray);
});
}
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});
}
getConnectedPeripherals() {
BleManager.getConnectedPeripherals([])
.then((peripheralsArray) => {
console.log('Connected peripherals: ', peripheralsArray);
}).catch(error=>{
})
}
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});
}