Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.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 无法使用react native Bluetoothel_Javascript_Android_Reactjs_React Native_Bluetooth - Fatal编程技术网

Javascript 无法使用react native Bluetoothel

Javascript 无法使用react native Bluetoothel,javascript,android,reactjs,react-native,bluetooth,Javascript,Android,Reactjs,React Native,Bluetooth,我在我的应用程序中添加了react-native ble plx。我还使用react native link cmd链接了它。我已遵循lib文档中提供的所有必要步骤。但它不起作用。我从不要求用户许可,这会导致错误,因为Deivce无权使用BluetoothLE。这是我的密码 AndroidManifest.xml <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.smart

我在我的应用程序中添加了react-native ble plx。我还使用react native link cmd链接了它。我已遵循lib文档中提供的所有必要步骤。但它不起作用。我从不要求用户许可,这会导致错误,因为Deivce无权使用BluetoothLE。这是我的密码

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.smartdeviceiot">
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
   <uses-permission android:name="android.permission.INTERNET" />
   <uses-permission android:name="android.permission.BLUETOOTH"/>
   <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
   <uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION"/>
   <uses-sdk
                 android:minSdkVersion="18"
                 android:targetSdkVersion="23"/> 
'aGVsbG8gbWlzcyB0YXBweQ==') .然后((特征)=>{ 此.info(特征值); 返回 }) }) .catch((错误)=>{ this.error(error.message) }) } }); }}函数MapStateTops(状态){ //传递提供者 返回{ }}/*将动作映射到道具*/函数mapDispatchToProps(调度)>{ 返回{ 操作:bindActionCreators({ },调度) };}导出默认连接( mapStateToProps、mapDispatchToProps)(设备研究)


如果我的蓝牙处于关闭状态,请关闭代码控制台。向我记录蓝牙处于关闭状态,但当蓝牙处于关闭状态时,请向我记录设备无权使用蓝牙。我也厌倦了使用AndroidPermission库,但没有成功。它不接受用户的权限

您需要明确地向用户请求权限。我也遇到了同样的问题,我通过在单独的文件中添加以下代码来解决:

export async function requestLocationPermission() {
  try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION, {
        title: 'Location permission for bluetooth scanning',
        message: 'wahtever',
        buttonNeutral: 'Ask Me Later',
        buttonNegative: 'Cancel',
        buttonPositive: 'OK',
      },
    ); 
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      console.log('Location permission for bluetooth scanning granted');
      return true;
    } else {
      console.log('Location permission for bluetooth scanning revoked');
      return false;
    }
  } catch (err) {
    console.warn(err);
    return false;
  }
}
然后,当我需要扫描时,在实际扫描代码之前,我会执行以下操作:

  scanAndConnect() {
    const permission = requestLocationPermission();
    if (permission) {        
      ...here I scan because the user has given permission

您可能需要等待requestLocationPermission()和scanAndConnect的小更正应该是异步的。而且好的地理位置对我们来说并不粗糙
  scanAndConnect() {
    const permission = requestLocationPermission();
    if (permission) {        
      ...here I scan because the user has given permission