反应性:0.61 使用的库:react-native-ble-manager 问题:我已经使用react-native-ble-manager实现了蓝牙扫描仪.startScan将在渲染上被调用。扫描的时间为2秒。但是扫描不会在2秒后停止。它会在8-9秒后停止,并且如果正在进行扫描,屏幕将被挂起。
代码段:
class BluetoothScreen extends React.Component {
constructor(props) {
super(props);
this.state = {
scanning: false,
}
this.startScan()
this.handleDiscoverPeripheral = this.handleDiscoverPeripheral.bind(this);
this.handlerStop = bleManagerEmitter.addListener('BleManagerStopScan', this.handleStopScan );
this.handleAppStateChange = this.handleAppStateChange.bind(this);
}
startScan() {
console.log("from start scan")
this.setState({ scanning: false })
if (!this.state.scanning) {
BleManager.scan([], 2, false).then((results) => {
console.log('Scanning...');
this.setState({ scanning: true });
this.setState({ isVisibleOne: true })
return (
<View>
<Text>Scanning..</Text>
</View>
)
})
}
handleStopScan() {
console.log('Scan is stopped');
if (this.state.isVisibleOne === false)
this.setState({ scanning: false })
}
}