In my project, I am working on scanning HM-10 BLE with a react-native app. To achieve this, I referred to the example provided in Scanning for Bluetooth devices with React Native. So far, the library seems to be successfully installed without any errors during execution.
react-native init reactnativeBLE
npm i --save react-native-ble-manager
npm install
react-native link react-native-ble-manager
react-native run-ios
However, despite following all the steps mentioned above, when I run the code, it is unable to detect any devices. Within my App.js
file, I have included the sample code as provided:
import React, { Component } from 'react';
import {
AppRegistry,
ListView,
NativeAppEventEmitter,
View,
Text,
Button } from 'react-native';
import BleManager from 'react-native-ble-manager';
// Modified the export to 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>
);
}
}
Query: Why am I unable to scan BLE devices even after clicking on start scanning
? Is there any additional setup required?
I would greatly appreciate any feedback or advice! Thank you in advance :)