I put this code together but at this moment I have no devices to test it. I think it explains the basic idea better than a description. As-is, it always scans to check that the device is available, which is probably best.
The UUID and name will be the same for each customer, ( It's flashed into the ESP device ), however I've yet to work out how devices get 'bonded' to the phone or how to prevent anyone with the app simply connecting and controlling some other persons devices.
I'm sure it's all explained somewhere.
I'll check out your code.
Couple of things noticed in the program. I did extend the timer time to 8 seconds and disabled the 'not found' message so I could see the displayed list. ( I could have appended it of course )
If the target device isn't active, then it does end with the message target device not found.
If it is active, then we don't get the message, ( it is therefore 'found' by the code ) but it doesn't actually connect.
The displayed list quite often has duplicates.
I added the 'connecting message' - which does run.
We have had the 'ConnectWithAddress' Block for a long time, but it very much depends on how dependant the device is on Service UUIDs. It also has to be found every time if the MAC Address is dynamic.
The reason for duplicates - nothing in my code to remove them! (not important)
Making the unit unique for each customer - much depends on the number of customers. It's relatively easy to make Scripts with a unique variable, but not easy to make an App that 'knows' the same value unless it is in a hard-coded List, because the App of course is compiled.
I'm going to keep working on this for a while.
The BLE devices will have a fixed MAC address as they are ESP32 modules, so I assume they are reasonably unique and permanent. I haven't examined where the MAC is defined, but hey -- it's there for now.
I'm going be working on the security aspect which means working with local storage blocks.
I'll post an update when worthwhile. But I did note ( and in another thread ) that the RSSI value does not update itself - so that looks like a bug.
Yes, it probably is, but there are many issues with Bluetooth itself, mostly brought about by manufacturers not implementing the standards correctly. If you buy the cheaper ESP32 modules from the open markets, overall they are good performers and can save a lot of money, but the devil is in the details......
Since this topic has been very useful to me in my first project with MIT App. Even though it's been a long time since it opened, I want to contribute with the solution that I have found.
The typical mechanism proposed for the extension MIT BLE, although it works perfectly, I find it very inelegant if it is to connect a known device and it is not a simple experiment. Also, the number of keystrokes and the entire connection process, it is very cumbersome and only understandable to someone who knows how BLEs work.
My intention is that the App, just start, connect automatically and without user intervention to the known BLE device whose NAME has been previously configured. Of course, since the connection is not made instantly, The App should be giving simple messages that allow to know in which stage the connection is: start SCAN, stop SCAN, connect ... And, of course, give the corresponding OK or error messages.
Attached is a small test that I have used, isolating only the connection process.
I have carried out tests with HC-05 modules from different manufacturers until it works in all of them.
In the building where I live, the number of neighbors is huge and the number of BLE devices that appear when doing the SCAN, it is surprisingly high. It even depends a lot from the room where you run the App. This also created certain problems for me, but this version has worked correctly.
I have used the BLE extension 20200828 and the 20201223. With both it works. PrbaConexBLE.aia (409.7 KB)
If anyone finds it useful, I would be very happy to have been able to contribute to the MIT App community.
I have Micropython code that works with the "Serial Bluetooth Terminal" app which is great for getting started but it is very limited.. I downloaded the BLE_ConnectToKnownDevice.aia code and tried "filling in the blanks" for the global variables device name, service UUID and the Rx and Tx UUID's.
In ble_simple_peripheral.py I found what appears to be
device name "mpy-uart".
service UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
Rx UUID "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
Tx UUID "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
I entered these into the KnownDevice code but it doesn't connect or find the device.
I loaded the latest extension earlier this week when I found Chris' code blocks (from 2022?). Based on your advice, I dragged another ble component (ble2) onto screen1 and replaced all the ble1 references with ble2.
Everything works fine now. Thank you so much for your help!
I spoke too soon. The code blocks with the updated ble extension work but the success rate for connecting is about 1 out of every 5 tries. The Micropython code connects 5 out of 5 with the Serial Bluetooth Terminal app. As I stated before, I am using @ChrisWard 's "Connect to a Known Device" blocks. Any suggestions?
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.
Pleas wait for an answer from @ChrisWard
I'm not a BLE specialist, but let me ask the following questions:
Why do you scan for devices? Is it only to make sure, you are close to the device? And why is there a clock component which stops the scan after 5 seconds? Probably your device still was not found during that time? What about just connecting to the already known device when you are close without scanning?
EDIT: If you still want to scan before connecting, set the timer interval to a higher value... try 10000
Is the device including the UUID in the advertising packet? The ConnectToDeviceWithServiceAndName looks at the advertising packets of the devices and only matches the devices that contain both pieces of information.
I'm not terribly familiar with the BLE stack on the ESP 32, but it looks like you should be able to add the service UUID via a BLEAdvertising object:
So something like may provide the right information App Inventor is looking for in the advertising packet: