BLE device not found

I have modified an existing app to control a small car using BLE. When I send the app to the MIT AI2 Companion, everything works. I'm able to find my device, connect to it, send it commands and it functions as designed. When I export the .apk and install it on my phone, it throws up an Error 9007 when scanning and is unable to find the device. The Location and Nearby Devices permissions are turned on and I'm running Andriod 12. I've looked at the other topics and found no solution. I'm also running the latest BLE extension 20200828. Please help.

Worth ruling out this scenario first before considering other possibilities...
I suspect your app logic for scanning for the car/BLE device and then connecting to the BLE module is not quite correct (suggest sharing your code blocks for the scanning and connecting part), as the error code relates to your app trying to connect to a BLE device when it is not in the scanned device list yet (did you hard code this in your app?) .

Code snippets are below.
BLE_Code_1

The method "when BluetoothLE1.DeviceFound" is triggered when any device is found (as its updating the internal BLE device list) and is not necessarily the device that matches your address. As such you need to check there is a match first before moving to the next stage, and the next stage is to instruct your phone to stop scanning for devices. Then and only then do you attempt to connect.

An alternative is to use the ScanForDevice method which will only scan for your device. If a matching device is found you still need to use the stop scanning method before connecting.

component_method

Have you succeeded in listing your device Thomas? Before you run your App, ensure that Location is switched on - this is a Google Security Measure and a requirement on most Smartphones.

If you want to get a list of devices, give the scan time to build the list, around 3 to 5 seconds. You can do this by starting the scanning and then enabling a Clock Timer:


Snap4

Also, ensure that both of the devices to be connected actually support BLE!
My website:
https://www.professorcad.co.uk/appinventortips#TipsBluetooth

After some investigation, I have determined the issue to be a permissions issue. I am unable to enable the ACCESS_FINE_LOCATION permission for the app. When the app is piggybacking off of the AI2 companion, it works fine because, in the permissions for the app, there is an option for "Use Precise Location" that my app does not have. I only have ACCESS_COURSE_LOCATION enabled. When I try to ask for ACCESS_FINE_LOCATION permission using the Screen1.AskForPermission method, it is denied.

The code all works now and functions as designed. First issue was enabling the ACCESS_FINE_LOCATION permission. This was accomplished through adding a Location Sensor to the app. Then within the Screen1.Initialize block adding "set LocationSensor1.ProviderLocked = True" and "set LocationSensor1.ProviderName = 'gps'".
This allowed the android OS to prompt me for the ACCESS_FINE_LOCATION permission.

Next I had to upgrade the BluetoothLE extentions in AI2 to the latest to get the BluetoothLE.ScanforService method. This method provided the means for connection. I also added a BluetoothLE.StopScanning method as suggested in a previous post.

Thanks everybody for all your help.

Code is attached below.
BLE_CAR_CONTROLLER.aia (190.4 KB)

Code is for a small car powered by four DC motors through an L293 motor controller and Arduino Nano 33 IoT. I'm sure there is room for improvement in the code. Any suggestions welcome.

2 Likes