【问题标题】:react-native scanning bluetooth devicesreact-native 扫描蓝牙设备
【发布时间】:2019-01-24 01:08:26
【问题描述】:

对于我的项目,我正在尝试使用 react-native 应用程序扫描 HM-10 BLE。 我正在使用这个例子Scanning for Bluetooth devices with React Native。似乎我成功安装了该库,因为当我运行代码时,我没有收到任何错误。我做了以下步骤。

  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

但是,当我运行示例代码时,我没有找到任何设备。在我的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


    【解决方案1】:

    问题可能是由于这些原因造成的:

    1. 权限
    2. SDK 版本

    权限

    (适用于安卓) 将这些行添加到您的 android 清单文件中 路径下:

    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" />
    

    当你第一次运行你的应用程序时,如果没有尝试运行时权限,设备必须请求权限。 如下图:

    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");
              }
            });
          }
      });
    }  
    

    注意事项

    在 Android API 29 >= 您需要使用“ACCESS_FINE_LOCATION”而不是“ACCESS_COARSE_LOCATION”。


    SDK 版本

    在尝试了很多改变 sdk 版本后对我来说效果很好。我降级 sdk 版本 29 -> 28 并且问题解决了! 一切看起来都不错,但仍然无法工作尝试降级 sdk 版本。 转到 build.gradle 文件

    android/build.gradle

    buildscript {
    ext {
        buildToolsVersion = "28.0.3"
        minSdkVersion = 18
        compileSdkVersion = 28
        targetSdkVersion = 28
    }
    

    图书馆页面: https://github.com/innoveit/react-native-ble-manager

    【讨论】:

      【解决方案2】:

      也许你需要同时打开你的位置和蓝牙。

      【讨论】:

        【解决方案3】:

        在 Android API 29 >= 您需要使用“ACCESS_FINE_LOCATION”而不是“ACCESS_COARSE_LOCATION”。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-26
          • 1970-01-01
          • 1970-01-01
          • 2019-06-08
          • 2016-06-26
          相关资源
          最近更新 更多