admin管理员组文章数量:1420677
For my project I am trying to scan HM-10 BLE with react-native app. I am using this example Scanning for Bluetooth devices with React Native. It seems like I successfully installed the library because when I run the code I get no error. I did the following steps.
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, when I run the example code, I don't find any devices. In my App.js
file I copied the example code:
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)
};
}
ponentDidMount() {
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>
);
}
}
My question
Why can't I scan BLE devices when I click start scanning
?
Do I need extra setup?
Any ments or advise would be really appreciated! Thanks in advance :)
For my project I am trying to scan HM-10 BLE with react-native app. I am using this example Scanning for Bluetooth devices with React Native. It seems like I successfully installed the library because when I run the code I get no error. I did the following steps.
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, when I run the example code, I don't find any devices. In my App.js
file I copied the example code:
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)
};
}
ponentDidMount() {
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>
);
}
}
My question
Why can't I scan BLE devices when I click start scanning
?
Do I need extra setup?
Any ments or advise would be really appreciated! Thanks in advance :)
Share Improve this question asked Jan 24, 2019 at 1:08 kirimikirimi 1,4005 gold badges33 silver badges61 bronze badges3 Answers
Reset to default 1Problem may caused because of thesee:
- Permission
- SDK version
PERMISSONS
(For android) Add these lines to your manifest file for android under the path :
android/app/src/main/AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
when you run your app first time, device must ask for permission if not try run time permission. As shown below:
if (Platform.OS === 'android' && Platform.Version >= 23) {
PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION).then((result) => {
if (result) {
console.log("Permission is OK");
} else {
PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION).then((result) => {
if (result) {
console.log("User accept");
} else {
console.log("User refuse");
}
});
}
});
}
NOTES
In Android API 29 >= you need to use "ACCESS_FINE_LOCATION" instead of "ACCESS_COARSE_LOCATION".
SDK VERSION
After try lots of thing changing sdk version is worked fine for me. I downgrade sdk version 29 -> 28 and problem solved! Everything looks good but still not work try to downgrade sdk version. goto build.gradle file
android/build.gradle
buildscript {
ext {
buildToolsVersion = "28.0.3"
minSdkVersion = 18
pileSdkVersion = 28
targetSdkVersion = 28
}
Library Page: https://github./innoveit/react-native-ble-manager
Maybe you need to turn on your location and bluetooth at the same time.
In Android API 29 >= you need to use "ACCESS_FINE_LOCATION" instead of "ACCESS_COARSE_LOCATION".
本文标签: javascriptreactnative scanning bluetooth devicesStack Overflow
版权声明:本文标题:javascript - react-native scanning bluetooth devices - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745327480a2653659.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论