【发布时间】:2019-01-24 01:08:26
【问题描述】:
对于我的项目,我正在尝试使用 react-native 应用程序扫描 HM-10 BLE。 我正在使用这个例子Scanning for Bluetooth devices with React Native。似乎我成功安装了该库,因为当我运行代码时,我没有收到任何错误。我做了以下步骤。
react-native init reactnativeBLEnpm i --save react-native-ble-managernpm installreact-native link react-native-ble-managerreact-native run-ios
但是,当我运行示例代码时,我没有找到任何设备。在我的App.js 文件中,我复制了示例代码:
import React, { Component } from 'react';
import {
AppRegistry,
ListView,
NativeAppEventEmitter,
View,
Text,
Button } from 'react-native';
import BleManager from 'react-native-ble-manager';
// I changed this to export default App
class BluetoothScanner extends Component {
constructor(props){
super(props);
const dataSource = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.devices = [];
this.state = {
dataSource: dataSource.cloneWithRows(this.devices)
};
}
componentDidMount() {
console.log('bluetooth scanner mounted');
NativeAppEventEmitter.addListener('BleManagerDiscoverPeripheral',(data) =>
{
let device = 'device found: ' + data.name + '(' + data.id + ')';
if(this.devices.indexOf(device) == -1) {
this.devices.push(device);
}
let newState = this.state;
newState.dataSource = newState.dataSource.cloneWithRows(this.devices);
this.setState(newState);
});
BleManager.start({showAlert: false})
.then(() => {
// Success code
console.log('Module initialized');
});
}
startScanning() {
console.log('start scanning');
BleManager.scan([], 120);
}
render() {
return (
<View style={{padding: 50 }}>
<Text>Bluetooth scanner</Text>
<Button onPress={() => this.startScanning()} title="Start scanning"/>
<ListView
dataSource={this.state.dataSource}
renderRow={(rowData) => <Text>{rowData}</Text>}
/>
</View>
);
}
}
我的问题
为什么点击start scanning时无法扫描BLE设备?
我需要额外的设置吗?
任何 cmets 或建议将不胜感激!在此先感谢:)
【问题讨论】:
标签: javascript react-native bluetooth bluetooth-lowenergy core-bluetooth