Flutter – 在 AndroidManifest 文件中确切的位置来放置 flutter_reactive_ble 库的权限。

huangapple go评论73阅读模式
英文:

Flutter - Exact place to put permissions in AndroidManifest file(s) for flutter_reactive_ble library

问题

我在Flutter移动应用程序中运行flutter_reactive_ble library的示例代码时遇到了问题。我尝试了一些示例,但没有成功。

我发现错误的主要原因是蓝牙权限不足。flutter_reactive_ble library的文档显示我们需要将两部分代码放入Android Manifest文件中...

第一部分:

<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="30" />

第二部分:

<uses-permission android:name="android.permission.BLUETOOTH_SCAN" 
                 tools:remove="android:usesPermissionFlags"
                 tools:targetApi="s" />

这是此库的文档链接: https://pub.dev/packages/flutter_reactive_ble

我不知道在哪里放置它们,特别是第二部分。如果没有提供这些代码,代码将无法工作。

另一个问题是,在Flutter应用中有三个Manifest文件,分别位于三个不同的目录:

  • main
  • profile
  • debug

这些文件是不同的,我应该编辑哪一个?

整个Manifest文件(来自main目录):

<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" tools:remove="android:usesPermissionFlags" tools:targetApi="s" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="30" />

    <application
        android:label="fl_ble_3"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>
英文:

I have problems to manage how to run sample / example codes for flutter_reactive_ble library in Flutter mobile app for Android. I have tried few examples, with no luck.

I found that the main reason for errors is lack of provided permissions for Bluetooth. Docs of flutter_reactive_ble library show that we need to put two parts of code to Android Manifest file(s)...

One:

<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="30" />

Two:

<uses-permission android:name="android.permission.BLUETOOTH_SCAN" 
                     tools:remove="android:usesPermissionFlags"
                     tools:targetApi="s" />

Here are docs for this lib: https://pub.dev/packages/flutter_reactive_ble

I don't know where to place them, especially the second part. Without them provided codes won't work.

Another problem is that in Flutter app there are three Manifest files, in three different dirs:

  • main
  • profile
  • debug

Those files are different, which should I edit?

--

Whole Manifest file (from main dir):

<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" tools:remove="android:usesPermissionFlags" tools:targetApi="s" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="30" />

    <application
        android:label="fl_ble_3"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

答案1

得分: 1

Android清单文件的位置位于[项目根目录]\android\app\src\main\AndroidManifest.xml,您的清单应该如下所示:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.blockchaincommodities.hippo_wallet"
    xmlns:tools="http://schemas.android.com/tools">
   
   <uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />
   <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30" />
   <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="30" />


   <application>
   ...
   <\application>
<\manifest>

正如文档中所述,如果您使用BLUETOOTH_SCAN来确定位置,您必须修改权限,您的清单可能如下所示:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.blockchaincommodities.hippo_wallet"
    xmlns:tools="http://schemas.android.com/tools">
   
   <uses-permission android:name="android.permission.BLUETOOTH_SCAN" 
                     tools:remove="android:usesPermissionFlags"
                     tools:targetApi="s" />
   <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30" />
   <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="30" />


   <application>
   ...
   <\application>
<\manifest>

由于这些权限很关键,您必须首先要求用户接受权限。在Flutter中,您可以通过像permission_handler这样的库来处理权限。将此库添加到您的项目中,并在扫描设备之前添加以下行:

PermissionStatus locationPermission = await Permission.location.request();
PermissionStatus bleScan = await Permission.bluetoothScan.request();
PermissionStatus bleConnect = await Permission.bluetoothConnect.request();
英文:

The Android Manifest's location is inside [Project_Root]\android\app\src\main\AndroidManifest.xml And your manifest would be look like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.blockchaincommodities.hippo_wallet"
    xmlns:tools="http://schemas.android.com/tools">
   
   <uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />
   <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30" />
   <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="30" />


   <application>
   ...
   <\application>
<\manifest>

As it's mentioned in the document, If you use BLUETOOTH_SCAN to determine location, you have to modify the permission and your manifest would be looked like this alternatively:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.blockchaincommodities.hippo_wallet"
    xmlns:tools="http://schemas.android.com/tools">
   
   <uses-permission android:name="android.permission.BLUETOOTH_SCAN" 
                     tools:remove="android:usesPermissionFlags"
                     tools:targetApi="s" />
   <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30" />
   <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="30" />


   <application>
   ...
   <\application>
<\manifest>

Since these permission are critical, You have to ask the user to accept the permissions first. In flutter, you can handle permissions by libraries like pemission_handler. Add this library to your project and add these lines before scanning for devices.

PermissionStatus locationPermission = await Permission.location.request();
PermissionStatus bleScan = await Permission.bluetoothScan.request();
PermissionStatus bleConnect = await Permission.bluetoothConnect.request();

huangapple
  • 本文由 发表于 2023年4月16日 23:32:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76028672.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定