BLE デバイスと対話するための BLE Manager を備えた React Native アプリケーション。
デバイスをスキャンできます。デフォルトでは 10 秒間実行されます。
デバイスを見つけて [接続] ボタン [デバイスがスキャンされている同じページ] をクリックすると、予期されるアクションが発生し、さらにスキャンを停止して接続を開始する必要があります。
問題は、スキャン中にボタン操作に反応しないことです。一定時間後にスキャンが完了して初めて、接続要求が開始されます。
接続ボタンがクリックされたときのコード スニペットを以下に示します。
try {
console.log('connect....');
await BleManager.stopScan();
setIsScanning(false);
await connectBle(peripheral.id);
Alert.alert(
'Alert Title',
`Successfully Connected to ${peripheral.name}`,
[
{
text: 'OK',
onPress: () => handleConnected(peripheral),
},
],
);
getConnectedPeripherals();
var newPeripherals1 = newPeripherals.filter(n => n.id !== peripheral.id);
console.log('newPeripherals1111', newPeripherals1);
setNewPeripherals(newPeripherals1);
} catch (error) {
Alert.alert(
'Alert Title',
`Successfully Connected to ${peripheral.name}`,
[
{
text: 'OK',
onPress: () => {},
// navigation.navigate('Configuration', {
// peripheral: peripheral,
// }),
},
],
);
}
}```