Hello,
I followed the instructions here to try to make the bluetooth scan work on Android 12 device with app targeted for API level 31.
www.b4x.com
However, even though the permission is added in the manifest and access_fine_location permission is requested in runtime in the code, I'm getting the below error in logcat, and the program crashes. I don't get typical Android nearby_devices permission dialog box.
Am I missing something? Is there a function to request runtime permission just like location? Similar to this:
Thanks
Android SDK used - Android 33:
platforms\android-33\android.jar
Logcat error:
Code requesting runtime location permission: (this is requested before Bluetooth scan operation)
Manifest:
I followed the instructions here to try to make the bluetooth scan work on Android 12 device with app targeted for API level 31.
[B4XPages] Bluetooth Chat Example
Updated implementation, based on B4XPages of https://www.b4x.com/android/forum/threads/android-bluetooth-bluetoothadmin-tutorial.14768/#content. The code is much simpler compared to the previous example. Note that you can call Serial.Listen without making the device discoverable. This is useful...
However, even though the permission is added in the manifest and access_fine_location permission is requested in runtime in the code, I'm getting the below error in logcat, and the program crashes. I don't get typical Android nearby_devices permission dialog box.
Am I missing something? Is there a function to request runtime permission just like location? Similar to this:
B4X:
rp.CheckAndRequest(rp.PERMISSION_BLUETOOTH_SCAN)
Thanks
Android SDK used - Android 33:
platforms\android-33\android.jar
Logcat error:
B4X:
12-11 23:59:12.123 18500 18500 E AndroidRuntime: java.lang.SecurityException: Need android.permission.BLUETOOTH_SCAN permission for AttributionSource { uid = 10702, packageName = X, attributionTag = null, token = android.os.BinderProxy@b1e0deb, next = null }: GattService registerScanner
12-11 23:59:12.123 18500 18500 E AndroidRuntime: at X.beaconmonitor._startscan(beaconmonitor.java:399)
12-11 23:59:12.123 18500 18500 E AndroidRuntime: at X.main._startscan(main.java:2070)
12-11 23:59:12.123 18500 18500 E AndroidRuntime: at X.main._timer2_tick(main.java:2151)
Code requesting runtime location permission: (this is requested before Bluetooth scan operation)
B4X:
rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
If Result Then
File.MakeDir(rp.GetSafeDirDefaultExternal(""), "X")
Else
ToastMessageShow("Location permission is needed to collect data. Please grant permission! Quitting...",True)
Sleep(3000)
Activity.Finish
End If
Manifest:
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="31"/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.
AddPermission(android.permission.BLUETOOTH_SCAN)