React-Native-Ble-Plx [BleError: Cannot start scanning operation]

问题描述 投票:0回答:0

我在android平台上开发了react-nativ-cli项目。一个导航项目,在建筑物内使用iBeacons提供指导。你选择你想去的地方,然后他指引你。我用的是react-native-ble-plx库。我在 android 5.0 的设备上进行开发(我知道它很旧,但我没有任何其他设备)。该应用程序在我的设备上运行顺利。 Android 5.0 也不需要任何权限。我开始使用 Android 5.0 以上的设备,例如 android 11.0 及更高版本。他想要获得这些设备的许可。看来我已经解决了权限问题。它请求蓝牙的位置许可。我通过在应用程序控制台的开头请求许可来解决这个问题。根据我的输出,这似乎是允许的。 我的错误是 = LOG [BleError: Cannot start scanning operation] 我该如何解决这个错误。

My androidManifest.xml file :
<!-- Start bluetooth permissions -->
  <uses-permission android:name="android.permission.BLUETOOTH" />
  <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
  <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE"/>
  <uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
  <uses-permission-sdk-23 android:name="android.permission.ACCESS_FINE_LOCATION" />
  <uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
  <uses-feature android:name="android.hardware.bluetooth" android:required="true"/>
  <uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />

  <!-- End bluetooth permissions -->

我的 ContextApi 状态文件:

async function requestBluetoothPermission() {
  try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
      {
        title: 'Konum İzni',
        message: 'The application needs location permission for Bluetooth scanning.',
        buttonNeutral: 'Ask Me Later',
        buttonNegative: 'Reject',
        buttonPositive: 'Okey',
      },
    );
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      console.log('location permission has been granted.');
    } else {
      console.log('location permission was not granted.');
    }
  } catch (err) {
    console.warn(err);
  }
}

const manager = new BleManager();
const defaultDeviceName = 'POI';
const defaultDeviceRssi = -1;
const dbInstance = new Db();


// Control the bluetooth permissions
manager.onStateChange(async state => {
  if (state === 'PoweredOn') {
    console.log('Bluetooth on.');

    // We take check value on database
    const checkValue = await dbInstance.getDefaultCheckValue();

    // İf chechk value is 1 we want to ble permissions
    if (checkValue === 0) {
      requestBluetoothPermission();
    }
  } else {
    console.log('Bluetooth off.');
    requestBluetoothPermission();
  }
}, true);

扫描功能:

// Scanning Operations
  const scanForDevices = useCallback(() => {
    const debouncedScan = debounce(() => {
      let deviceFound = false;
  
      manager.startDeviceScan(null, null, (error, device) => {
        if (error) {
          console.log(error);
          Alert.alert(
            'Error',
            'Lütfen bluetoothunuzu açıp kapatınız ve uygulamayı yeniden başlatınız',
            [{text: 'OK', onPress: () => console.log('OK Pressed')}],
            {cancelable: false},
          );
          return;
        }
        if (device.localName === defaultDeviceName) {
          deviceFound = true;
  
          const base64 = RNFetchBlob.base64;
          const advertisingData = stringToBytes(
            base64.decode(device.manufacturerData),
          );
          const rssi = device.rssi;
          const major = advertisingData[21];
          const minor = advertisingData[23];
  
          setBleData(rssi, major, minor);
  
          console.log(matchedDataRef.current);
        }
      });
  
      console.log('scanForDevices is finished');
    }, 1500);
  
    debouncedScan();
  }, [setBleData]);
android react-native mobile bluetooth bluetooth-lowenergy
© www.soinside.com 2019 - 2024. All rights reserved.