Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 响应本机扫描蓝牙设备_Javascript_React Native_Bluetooth_Bluetooth Lowenergy_Core Bluetooth - Fatal编程技术网

Javascript 响应本机扫描蓝牙设备

Javascript 响应本机扫描蓝牙设备,javascript,react-native,bluetooth,bluetooth-lowenergy,core-bluetooth,Javascript,React Native,Bluetooth,Bluetooth Lowenergy,Core Bluetooth,对于我的项目,我试图用react原生应用程序扫描HM-10 BLE。 我正在使用这个例子。我似乎成功地安装了库,因为当我运行代码时,没有收到任何错误。我执行了以下步骤 react native init reactnativeBLE npm i--保存反应本机ble管理器 npm安装 react-native-link react-native-ble-manager react本机运行ios 但是,当我运行示例代码时,我没有找到任何设备。在我的App.js文件中,我复制了示例代码: impor

对于我的项目,我试图用react原生应用程序扫描HM-10 BLE。 我正在使用这个例子。我似乎成功地安装了库,因为当我运行代码时,没有收到任何错误。我执行了以下步骤

  • react native init reactnativeBLE
  • npm i--保存反应本机ble管理器
  • npm安装
  • react-native-link react-native-ble-manager
  • react本机运行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>
            );
        }
    }
    
    import React,{Component}来自'React';
    导入{
    评估学,
    ListView,
    民族主义者,
    看法
    文本,
    按钮}来自“反应本机”;
    从'react native ble manager'导入BleManager;
    //我将此更改为导出默认应用程序
    类BluetoothScanner扩展组件{
    建造师(道具){
    超级(道具);
    const dataSource=new ListView.dataSource({rowHasChanged:(r1,r2)=>r1!==r2});
    此参数为.devices=[];
    此.state={
    dataSource:dataSource.cloneWithRows(this.devices)
    };
    }
    componentDidMount(){
    console.log(“安装蓝牙扫描仪”);
    NativeAppEventMitter.addListener('BleManagerDiscoverPeripheral',(数据)=>
    {
    let device='找到的设备:'+data.name+'('+data.id+');
    if(this.devices.indexOf(device)=-1){
    这个。设备。推(设备);
    }
    让newState=this.state;
    newState.dataSource=newState.dataSource.cloneWithRows(this.devices);
    this.setState(newState);
    });
    BleManager.start({showarter:false})
    .然后(()=>{
    //成功代码
    log('Module initialized');
    });
    }
    开始扫描(){
    console.log(“开始扫描”);
    BleManager.scan([],120);
    }
    render(){
    返回(
    蓝牙扫描仪
    this.startScanning()}title=“开始扫描”/>
    {rowData}}
    />
    );
    }
    }
    
    我的问题 为什么单击“开始扫描”“时无法扫描BLE设备?”? 我需要额外的设置吗


    任何意见或建议将不胜感激!提前感谢:)

    可能您需要同时打开位置和蓝牙。

    问题可能是由于以下原因造成的:

  • 权限
  • SDK版本
  • 许可证 (适用于android) 将这些行添加到android的清单文件中 在路径下:

    android/app/src/main/AndroidManifest.xml

    注释


    在Android API 29>=中,您需要使用“访问精细位置”而不是“访问粗略位置”


    SDK版本 在尝试了很多东西后,修改sdk版本对我来说很好。我降级sdk版本29->28,问题解决了! 一切看起来都很好,但仍然不起作用。请尝试降级sdk版本。 转到build.gradle文件

    android/build.gradle

    图书馆网页:

    在Android API 29>=您需要使用“访问精细位置”而不是“访问粗略位置”

    <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");
              }
            });
          }
      });
    }  
    
    buildscript {
    ext {
        buildToolsVersion = "28.0.3"
        minSdkVersion = 18
        compileSdkVersion = 28
        targetSdkVersion = 28
    }