提问者:小点点

无法使用react native Bluetoothel


我已经在我的应用程序中添加了react-native-ble-plx。我还使用react native link CMD链接了它。我已经完成了lib文件中提供的所有必要步骤。但它不起作用。我从来没有请求用户的许可,它给了错误Deivce没有被授权使用Bluetootle。这是我的代码

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

deviceSearch.js

从“../constants/colors”导入颜色;从“../constants/images”导入图像;从“Native-Base”导入{按钮、图标、文本、容器、页眉、左侧、正文、标题、右侧};从“./styles/home”导入{HomeStyle};从“react-native-ble-plx”类导入{BleManager}DevicesSearch扩展组件{static navigationOptions={title:“DevicesSearch”};const manager=new BleManager();this.state={};}componentWillMount(){}render(){return(>this.props.navigation.navigate(“drawerOpen”)}>DevicesSearch搜索设备);}scanAndConnect=()=>{alert(“asd”)console.log(“cal”);

      if (error) {
        this.error(error.message);
        return
      }

      if (device.name ==='MyDevice') {
        this.info("Connecting to Tappy");
        this.manager.stopDeviceScan();

        device.connect()
          .then((device) => {
            this.info("Discovering services and characteristics");
            return device.discoverAllServicesAndCharacteristics()
          })
          .then((device) => {
            this.info(device.id);
            device.writeCharacteristicWithResponseForService('12ab', '34cd',

'AGVSBG8GBWLZCYB0YXBWEQ==')。then((特性)=>{this.info(特性.值);return})。catch((错误)=>{this.error(错误.消息)})}});}}函数mapStateToProps(状态){//传递提供程序返回{}}/*将操作映射到道具*/函数mapDispatchToProps(调度)>{return{Actions:bindActionCreators({},调度)};}导出默认连接(mapStateToProps,mapDispatchToProps)(DevicesSearch);

如果我的蓝牙是关闭的代码控制台。记录我的蓝牙是关闭的,但当它在它上,记录我的设备没有使用蓝牙的身份验证。我也很想使用AndroidPermission库,但没有成功。它不需要用户的权限


共1个答案

匿名用户

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

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