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.

  1. react-native init reactnativeBLE
  2. npm i --save react-native-ble-manager
  3. npm install
  4. react-native link react-native-ble-manager
  5. 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.

  1. react-native init reactnativeBLE
  2. npm i --save react-native-ble-manager
  3. npm install
  4. react-native link react-native-ble-manager
  5. 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 badges
Add a ment  | 

3 Answers 3

Reset to default 1

Problem may caused because of thesee:

  1. Permission
  2. 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