Since Api31 android has introduced new bluetooth permissions at runtime, for this we need the ApiLevel procedure to check what Api level we have. If it is api31 or higher it will ask for permissions, if the level is lower it will load devices into the list. If you request permission in API less than 31, an error will be displayed in the application. To be able to list bluetooth devices in api31 or higher, we need the BluetoothScan permission. An appropriate message will be displayed in the application asking us to confirm permissions. After confirmation, the PermisionGranted event will be triggered, in which we load devices to the list and ask for the next BluetoothConnect permission that will be needed in subsequent appinventor blocks where we want to connect to the selected device. We also save the appropriate tag to TinyDb so that the next time the application is launched, it does not ask for permissions again but loads the devices to the list immediately. You can simplify the blocks, but this will not guarantee operational stability,
These are not my original blocks and I have posted many examples and solutions for the BluetoothClient
component (as well as the BLE
extension) in the past months.
My blocks always looked or should look something like this:
Scanning for devices is only possible after both permissions (SCAN & CONNECT
) have been granted (on Android 12+, tested on Pixel 4 XL / Android 13).
With your blocks I get a permission error, "Error 908: The permission ..."
To check it out, here are two AIAs.
BTclient_test1.aia (7.5 KB)
BTclient_test2.aia (7.5 KB)
BTclient_test1.aia
uses your blocks and doesn't work. BTclient_test2.aia
uses my blocks as I always posted them. This works. So build the APKs and check it on Android 12+...
And this version works with Companion & APK:
Corrected boiler plate:
(Canned Response ABG - Bluetooth non-BLE SCAN Permission Blocks)
The easiest solution, for immediate relief
(from @Barry_Meaker) ...
I had the same issue. The problem is your app does not have permission to see nearby devices. The solution is to give your app permission on your phone (no code changes in your app).
on your phone,
- goto settings
- search for your app
- in App Info for your app select Permissions
- change Nearby Devices from Not Allowed to Allowed
- Done
By the way, the very first time you run the app, Android will ask if you want to grant the app this permission. If you say no, or ignore the pop-up, the permission will be set as Denied. Android will not ask again.
A more complex approach, for professional app development:
See Bluetooth liste of devices deosn't work anymore - #7 by Anke
Special note for Xiaomi devices:
I have an error with bluetooth on android 12, Xiaomi Poco X3 NFC - #20 by Patryk_F
Dear Anke
Thanks a lot for the fantastic code.
I have tried it and add some flowers around it.
-
I have 2 questions on two points in the following picture.
-
I succed in connecting my BT module, but the list of BT devices is still displayed in Screen1. How to hide it after the selection of my BT module ?
Thanks in advance for your help.
PS : here is my aia :
BTclient_test2+DRFlowers.aia (8.1 KB)
-
One is without extension and the other is an extension.
-
I explicitly pointed out this problem in my answer(s) to ABG. So please read this again. (I deliberately left this block there "grayed out" to point out the problem and the solution.)
-
Use a timer to hide the ListView once a connection is established.
Is there a penalty from Android for asking for a permission that has already been granted?
I'm not sure but a feel like your question is a litle bit ironic ?
Thanks a lot for your wise help.
Things are a litlle bit clearer for me but i still have a basic issue.
I'm trying to send a 2 bytes Number 513 but not able to receive the correct number in Arduino serialDisplay. I read lot of discution but still don't understand where i screw up ?
No, I am just rummaging for a way to reduce the complexity of getting permissions without having to remember asking for it or without having a way to ask Android if I already have permission.
I'm searching for an easy stock answer for the board, without the experience of the people with actual experience.
you can try to ask as much as you like for permissions...
if you already have a permission, you do not get the permission window anymore and the event PermissionGranted does not fire anymore... because of the latter it might be a good idea to know if you already have that permission
Taifun
As I've said often enough, Google makes life more difficult for all of us – every year again with these (mandatory) idiotic targetSdkVersion updates. As long as users and developers just put up with this BS, this probably won't change. Btw, all Android users face these problems, not just AI2 users.
Sending 513 and receiving the sequence 0,120,128 is a bit of a puzzle.
Using Google to convert for me, 120 in binary is 1111000.
128 in binary is 10000000
But binary for 513 is 1000000001.
So where did that sequence of 1's in the 120 come from?
The BTSerial.begin(38400) is asking for a high bit rate.
Would lowering it to a more common 9600 fix bit errors?
Thanks a lot for your answer.
I 've checked your remarks regarding BTSerial speed and lower it @9600.
I've also changed 513 to 512
I've also replace BTSerial by the embedded Serial channel, but same results.
Please find bellow results of my tests, and obviously the transmission BTserial speed has change something but still don't understand.
I feel that the issue is around sending and receiving 2 bytes because i've also tested communication with 1 byte and it work in a very nice way.
Look around in the BlueToothClient blocks pallette for more transmission modes (integer, text, etc.)
Try those and see what arrives.
in the same status, I just had to change the mobile setting to allow the app for all access.
Everything is fine now ! thanks
bluetooth_arduino_grue.ino (2.0 KB)
Buetooth_arduino_grue.aia (498.6 KB)