Need to be able to scan and pair Bluetooth devices within the app

I have an app that processes multiple items through Bluetooth. As I need to pair and unpair with dozens of items every day, it’s a frustratingly lengthy process having to scan and pair in the Android OS, then start up my app, do the operations on the item, close the app completely, call up the Android pair & scan utility again, unpair the item, scan for any new items, pair again then start up the app again.

I have seen other Android apps that can call up the Android scan&pair routine from within the app, but this does not yet seem possible in App Inventor.

I had a look at Thunkable and it looks like it can be done in Bluetooth BLE.

Is there any plan to include this feature in App Inventor in the future?

the classic bluetooth extension is able to scan and pair

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by Taifun.

Oh wow, Taifun, that is exactly what I needed! Thank you so much for your help.

Ron

Hi Taifun, I dovetailed the Classic Bluetooth extension blocks into my app and all works fine in the Appinventor Companion, but when I compile and run the apk, I can only scan for devices once.
If I hit the “Scan” button again, a Bluetooth scan for devices does not happen.
Is there a way to reset the scan function to allow sequential scans in the apk?

In my app I have created now, I have the same function for disconnecting and connecting devices. On my HTC M7 it works just fine! For Samsung it seems it does not scans correctly, but you could try it. Perhaps it helps.
Edit: Do not saw you have a normal Bluetooth connection, but with the normal Bluetooth connection it is similar. Perhaps it helps anyway…

you might want to try the example project from the download folder
let me know, if that works for you

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by Taifun.

Thank you for the reply. I see you are using a BLE device, whereas i am using the Classic Bluetooth system.
Unfortunately there is no “Scan” block for the Classic Bluetooth extension, which is why I am having to use Taifun’s.

I have tested the test app, although it doesn’t display more than two out of the six paired devices at a time and frequently finds nothing paired or available to pair, which is why I have to use the “Bluetooth addresses and names” block in my app instead of the Taifun paired list.
The strange thing is that my app performs perfectly when running on the Companion, but when it is compiled to an apk, it scans only once then won’t scan again. It also seems to tie up the MIT Bluetooth extension, sometimes even stopping the phone from connecting to a device selected in the listview.

Would using six separate components for the six connections help prevent interference among them?

EDIT: please let us know, if scanning works fine repeatedly if you use the test project...

It would really help if you provided a screenshot of your relevant blocks, so we can see what you are trying to do, and where the problem may be.

To get an image of your blocks, right click in the Blocks Editor and select "Download Blocks as Image". You might want to use an image editor to crop etc. if required. Then post it here in the community.

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by Taifun.

The test apk will scan repeatedly, although it usually only returns the one or two devices that are powered on, out of the four paired devices. Because of this, I populate the “Paired devices” listpicker with the “AddressesAndNames” block of the MIT BT extension in order to display all paired devices.
I can also toggle between the two listviews in order to display all paired devices OR all available devices, not both at the same time. This is because the screen real estate is limited and I want to have 10 or more devices to pair/unpair at one time.
The real puzzle is why it works on the ai2 Companion, but not when compiled.

what you could try is

  • to move the AskForPermission block into the event Screen.Initialize
  • and move the Scan method directly into the event Scan.Click

you also might want to provide a Screen.PermissionDenied event, see again the example app here https://puravidaapps.com/bluetooth.php

which device / Android version are you using for your tests?

Taifun

Tried all of your suggestions, but no go.
Still runs well in the Companion, but not in the apk version.
The device is a Lenovo tablet TB-7304F running Android 7 (Nougat).

which means, it is a permission issue...
you might want to provide a screenshot of your updated relevant blocks after you followed these suggestions

Taifun

Another symptom is that after the first (and only) scan, it is virtually impossible to select any of the elements in either listview. It’s as if the app hangs.

you are starting the Scan twice, 1) in the PermissionGranted event and 2) in the Scan.Click event…
let me suggest you to remove the PermissionGranted event completely or alternatively just display a message for the user, that the permission has been granted successfully

Taifun

OK, I removed the PermissionGranted event but nothing changes.
I don’t know where to go from here. I’d say it was a quirk of the Lenovo Tab device, except for the fact that it works perfectly well in the Companion. What could be different about the Companion environment from the installed apk?

Try your apk on a device other than your Lenovo Tab? Let us know what happens. The problem could be a device issue or an issue related to the Android version. The only way to know is to do some experimentation with different actual devices and os versions.

Yes, that makes sense. I’ll do some hunting around.

let's summarize...

  1. scanning repeatedly works fine if you use the test project as apk file
  2. scanning repeatedly works fine, if you use your project in the companion app
  3. scanning works only once, if you use your project as apk file
    is this understanding correct?

concerning 3)
can you explain what happens step by step
a) you get the message to grant permission and you confirm
b) then you click the scan button, in the label the text "Scanning please wait" appears and after a while the result will be displayed in the listpicker
c) now you click the scan button again, so what happens here? does the text "Scanning please wait" appear? how do you know, that the second scan did not succeed?

Taifun