kakzaki / blue_thermal_printer

Working with thermal printer via bluetooth (Flutter)
MIT License
164 stars 205 forks source link

lacks permission android.permission.BLUETOOTH #148

Closed noebt closed 2 years ago

noebt commented 2 years ago

Hi, I'm implementing thermal printing in Android. I did everything as in the example and added the bluetooth permissions in the manifest (as shown in the official documentation).

Captura de pantalla 2022-05-08 a las 20 38 54

The thing is, the printer is found correctly, but when I try to connect to it, an error shows:

Captura de pantalla 2022-05-08 a las 20 39 56

I have set the targetSDK to both 30 and 31, did flutter clean and pub get a couple of times and I keep getting the same error. I don't understand why, my manifest has the correct permissions. I am using an Android 12 device. Thanks.

SanabriaDDi commented 2 years ago

I have the same problem, to temporarily correct what I did was directly edit the plugin.

blue_thermal_printer's AndroidManifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="id.kakzaki.blue_thermal_printer">
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <!-- Request legacy Bluetooth permissions on older devices. -->
    <uses-permission android:name="android.permission.BLUETOOTH"
         />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

    <!-- Needed only if your app looks for Bluetooth devices.
         You must add an attribute to this permission, or declare the
         ACCESS_FINE_LOCATION permission, depending on the results when you
         check location usage in your app. -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />

    <!-- Needed only if your app makes the device discoverable to Bluetooth
         devices. -->
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />

    <!-- Needed only if your app communicates with already-paired Bluetooth
         devices. -->
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

</manifest>

Also BlueThermalPrinterPlugin.java:

case "getBondedDevices":
        try {
            final String[] BLE_PERMISSIONS = new String[]{
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION,
            };

            final String[] ANDROID_12_BLE_PERMISSIONS = new String[]{
                    Manifest.permission.BLUETOOTH_SCAN,
                    Manifest.permission.BLUETOOTH_CONNECT,
                    Manifest.permission.ACCESS_FINE_LOCATION,
            };

            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {

                if (ContextCompat.checkSelfPermission(activity,
                        Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED ||
                        ContextCompat.checkSelfPermission(activity,
                                Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED ||
                        ContextCompat.checkSelfPermission(activity,
                                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                    ActivityCompat.requestPermissions(activity,
                            ANDROID_12_BLE_PERMISSIONS, REQUEST_BLE_12);

                    pendingResult = result;
                    break;
                }
            } else {

                if (ContextCompat.checkSelfPermission(activity,
                        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
                        ContextCompat.checkSelfPermission(activity,
                                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                    ActivityCompat.requestPermissions(activity,
                            BLE_PERMISSIONS, REQUEST_COARSE_LOCATION_PERMISSIONS);

                    pendingResult = result;
                    break;
                }
            }
          //Original code to request permissions
          /*if (ContextCompat.checkSelfPermission(activity,
                  Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(activity,
                    new String[] { Manifest.permission.ACCESS_COARSE_LOCATION }, REQUEST_COARSE_LOCATION_PERMISSIONS);

            pendingResult = result;
            break;
          }*/

          getBondedDevices(result);

        } catch (Exception ex) {
          result.error("Error", ex.getMessage(), exceptionToString(ex));
        }

        break;
noebt commented 2 years ago

@SanabriaDDi It worked!! Thanks a lot, I've spent hours trying to solve it 👍

cococolapepsi commented 2 years ago

@SanabriaDDi I have tried the code but i ran into this error

C:\src\flutter.pub-cache\hosted\pub.dartlang.org\blue_thermal_printer-1.2.0\android\src\main\java\id\kakzaki\blue_thermal_printer\BlueThermalPrinterPlugin.java:270: error: cannot find symbol Manifest.permission.permission.ACCESS_COARSE_LOCATION, ^ symbol: variable permission location: class permission C:\src\flutter.pub-cache\hosted\pub.dartlang.org\blue_thermal_printer-1.2.0\android\src\main\java\id\kakzaki\blue_thermal_printer\BlueThermalPrinterPlugin.java:275: error: cannot find symbol Manifest.permission.BLUETOOTH_SCAN, ^ symbol: variable BLUETOOTH_SCAN location: class permission C:\src\flutter.pub-cache\hosted\pub.dartlang.org\blue_thermal_printer-1.2.0\android\src\main\java\id\kakzaki\blue_thermal_printer\BlueThermalPrinterPlugin.java:276: error: cannot find symbol Manifest.permission.BLUETOOTH_CONNECT, ^ symbol: variable BLUETOOTH_CONNECT location: class permission C:\src\flutter.pub-cache\hosted\pub.dartlang.org\blue_thermal_printer-1.2.0\android\src\main\java\id\kakzaki\blue_thermal_printer\BlueThermalPrinterPlugin.java:280: error: package Build does not exist if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { ^ C:\src\flutter.pub-cache\hosted\pub.dartlang.org\blue_thermal_printer-1.2.0\android\src\main\java\id\kakzaki\blue_thermal_printer\BlueThermalPrinterPlugin.java:280: error: package Build does not exist if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { ^ C:\src\flutter.pub-cache\hosted\pub.dartlang.org\blue_thermal_printer-1.2.0\android\src\main\java\id\kakzaki\blue_thermal_printer\BlueThermalPrinterPlugin.java:283: error: cannot find symbol Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED || ^ symbol: variable BLUETOOTH_SCAN location: class permission C:\src\flutter.pub-cache\hosted\pub.dartlang.org\blue_thermal_printer-1.2.0\android\src\main\java\id\kakzaki\blue_thermal_printer\BlueThermalPrinterPlugin.java:285: error: cannot find symbol Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED || ^ symbol: variable BLUETOOTH_CONNECT location: class permission C:\src\flutter.pub-cache\hosted\pub.dartlang.org\blue_thermal_printer-1.2.0\android\src\main\java\id\kakzaki\blue_thermal_printer\BlueThermalPrinterPlugin.java:290: error: cannot find symbol ANDROID_12_BLE_PERMISSIONS, REQUEST_BLE_12); ^ symbol: variable REQUEST_BLE_12 location: class BlueThermalPrinterPlugin Note: C:\src\flutter.pub-cache\hosted\pub.dartlang.org\blue_thermal_printer-1.2.0\android\src\main\java\id\kakzaki\blue_thermal_printer\BlueThermalPrinterPlugin.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. 8 errors

FAILURE: Build failed with an exception.

giullianocht commented 2 years ago

@SanabriaDDi I have tried the code but i ran into this error

C:\src\flutter.pub-cache\hosted\pub.dartlang.org\blue_thermal_printer-1.2.0\android\src\main\java\id\kakzaki\blue_thermal_printer\BlueThermalPrinterPlugin.java:270: error: cannot find symbol Manifest.permission.permission.ACCESS_COARSE_LOCATION, ^ symbol: variable permission location: class permission C:\src\flutter.pub-cache\hosted\pub.dartlang.org\blue_thermal_printer-1.2.0\android\src\main\java\id\kakzaki\blue_thermal_printer\BlueThermalPrinterPlugin.java:275: error: cannot find symbol Manifest.permission.BLUETOOTH_SCAN, ^ symbol: variable BLUETOOTH_SCAN location: class permission C:\src\flutter.pub-cache\hosted\pub.dartlang.org\blue_thermal_printer-1.2.0\android\src\main\java\id\kakzaki\blue_thermal_printer\BlueThermalPrinterPlugin.java:276: error: cannot find symbol Manifest.permission.BLUETOOTH_CONNECT, ^ symbol: variable BLUETOOTH_CONNECT location: class permission C:\src\flutter.pub-cache\hosted\pub.dartlang.org\blue_thermal_printer-1.2.0\android\src\main\java\id\kakzaki\blue_thermal_printer\BlueThermalPrinterPlugin.java:280: error: package Build does not exist if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { ^ C:\src\flutter.pub-cache\hosted\pub.dartlang.org\blue_thermal_printer-1.2.0\android\src\main\java\id\kakzaki\blue_thermal_printer\BlueThermalPrinterPlugin.java:280: error: package Build does not exist if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { ^ C:\src\flutter.pub-cache\hosted\pub.dartlang.org\blue_thermal_printer-1.2.0\android\src\main\java\id\kakzaki\blue_thermal_printer\BlueThermalPrinterPlugin.java:283: error: cannot find symbol Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED || ^ symbol: variable BLUETOOTH_SCAN location: class permission C:\src\flutter.pub-cache\hosted\pub.dartlang.org\blue_thermal_printer-1.2.0\android\src\main\java\id\kakzaki\blue_thermal_printer\BlueThermalPrinterPlugin.java:285: error: cannot find symbol Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED || ^ symbol: variable BLUETOOTH_CONNECT location: class permission C:\src\flutter.pub-cache\hosted\pub.dartlang.org\blue_thermal_printer-1.2.0\android\src\main\java\id\kakzaki\blue_thermal_printer\BlueThermalPrinterPlugin.java:290: error: cannot find symbol ANDROID_12_BLE_PERMISSIONS, REQUEST_BLE_12); ^ symbol: variable REQUEST_BLE_12 location: class BlueThermalPrinterPlugin Note: C:\src\flutter.pub-cache\hosted\pub.dartlang.org\blue_thermal_printer-1.2.0\android\src\main\java\id\kakzaki\blue_thermal_printer\BlueThermalPrinterPlugin.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. 8 errors

FAILURE: Build failed with an exception.

I have this problem now, how did you fix it?

aqibaja commented 2 years ago

@giullianocht , fix for me ActivityCompat.requestPermissions(activity, ANDROID_12_BLE_PERMISSIONS, 1);

louisergi1 commented 2 years ago

Still got "lacks permission android.permission.BLUETOOTH" on it even after i follow the answers, it's not fixed

aqibaja commented 2 years ago

Still got "lacks permission android.permission.BLUETOOTH" on it even after i follow the answers, it's not fixed

@louisergi1 have follow this? https://developer.android.com/guide/topics/connectivity/bluetooth/permissions

louisergi1 commented 2 years ago

Still got "lacks permission android.permission.BLUETOOTH" on it even after i follow the answers, it's not fixed

@louisergi1 have follow this? https://developer.android.com/guide/topics/connectivity/bluetooth/permissions

actually nvm i need to add tools resource in ... my bad... and thank you so much for the response

aqibaja commented 2 years ago

Still got "lacks permission android.permission.BLUETOOTH" on it even after i follow the answers, it's not fixed

@louisergi1 have follow this? https://developer.android.com/guide/topics/connectivity/bluetooth/permissions

actually nvm i need to add tools resource in ... my bad... and thank you so much for the response

welcome

Alexis101120 commented 2 years ago

I have the same problem, to temporarily correct what I did was directly edit the plugin.

blue_thermal_printer's AndroidManifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="id.kakzaki.blue_thermal_printer">
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <!-- Request legacy Bluetooth permissions on older devices. -->
    <uses-permission android:name="android.permission.BLUETOOTH"
         />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

    <!-- Needed only if your app looks for Bluetooth devices.
         You must add an attribute to this permission, or declare the
         ACCESS_FINE_LOCATION permission, depending on the results when you
         check location usage in your app. -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />

    <!-- Needed only if your app makes the device discoverable to Bluetooth
         devices. -->
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />

    <!-- Needed only if your app communicates with already-paired Bluetooth
         devices. -->
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

</manifest>

Also BlueThermalPrinterPlugin.java:

case "getBondedDevices":
        try {
            final String[] BLE_PERMISSIONS = new String[]{
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION,
            };

            final String[] ANDROID_12_BLE_PERMISSIONS = new String[]{
                    Manifest.permission.BLUETOOTH_SCAN,
                    Manifest.permission.BLUETOOTH_CONNECT,
                    Manifest.permission.ACCESS_FINE_LOCATION,
            };

            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {

                if (ContextCompat.checkSelfPermission(activity,
                        Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED ||
                        ContextCompat.checkSelfPermission(activity,
                                Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED ||
                        ContextCompat.checkSelfPermission(activity,
                                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                    ActivityCompat.requestPermissions(activity,
                            ANDROID_12_BLE_PERMISSIONS, REQUEST_BLE_12);

                    pendingResult = result;
                    break;
                }
            } else {

                if (ContextCompat.checkSelfPermission(activity,
                        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
                        ContextCompat.checkSelfPermission(activity,
                                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                    ActivityCompat.requestPermissions(activity,
                            BLE_PERMISSIONS, REQUEST_COARSE_LOCATION_PERMISSIONS);

                    pendingResult = result;
                    break;
                }
            }
          //Original code to request permissions
          /*if (ContextCompat.checkSelfPermission(activity,
                  Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(activity,
                    new String[] { Manifest.permission.ACCESS_COARSE_LOCATION }, REQUEST_COARSE_LOCATION_PERMISSIONS);

            pendingResult = result;
            break;
          }*/

          getBondedDevices(result);

        } catch (Exception ex) {
          result.error("Error", ex.getMessage(), exceptionToString(ex));
        }

        break;

How to do this, how can find this plugin

Alexis101120 commented 2 years ago

@giullianocht , fix for me ActivityCompat.requestPermissions(activity, ANDROID_12_BLE_PERMISSIONS, 1);

can you show the code complete plis?

aqibaja commented 2 years ago

@giullianocht , fix for me ActivityCompat.requestPermissions(activity, ANDROID_12_BLE_PERMISSIONS, 1);

can you show the code complete plis?

same like @SanabriaDDi provided, but change REQUEST_BLE_12 to 1

kakzaki commented 2 years ago

I hope this is solved on the latest version