Search code examples
androidflutterdartbluetoothandroid-bluetooth

while using flutter `esc_pos_bluetooth` showing error `Error starting scan. ` only in android 12. no problem below android 10


Showing error while running in esc_pos_bluetooth in flutter I added permission in manifest page like this. <uses-permission android: name="android.permission.BLUETOOTH" android:maxSdkVersion="31" tools:targetApi="donut" /> <uses-permission android: name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="31" tools:targetApi="donut" /> <uses-permission android: name="android.permission.BLUETOOTH_SCAN" tools:targetApi="31" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="31" tools:targetApi="donut" /> <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" /> <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" android:maxSdkVersion="31" tools:targetApi="donut" /> <uses-permission android: name="android.permission.ACCESS_COARSE_LOCATION" />. And still having the error. Error starting scan. E/flutter (16326): [ERROR: flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Platform Exception(startscan, Need android.permission.BLUETOOTH_SCAN permission for android.content.AttributionSource@8a7ab34e: GattService register Scanner, null, null).

this problem is only showing in android 10 or above devices only. under android 10 version phones support this smoothly and showing near devices also.


Solution

  • Using 0.4.1 with the following manifest on a real device works for me:

        <uses-permission android:name="android.permission.BLUETOOTH" />
        <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
        <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
        <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
        <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"/>
    

    What you are missing is the uses-feature flag. Check it out here

    You can also read about hickups für Bluetooth < 12.0 on Android here