Skip to first unread message

Jacques Dumarest

unread,
Jan 5, 2018, 3:31:05 AM1/5/18
to MIT App Inventor Forum
 I am using a BLE device, and I know its address (previously saved from a scan using TinyDB). I want to connect using "ConnectWithAdress", but I get an error if if I do not first perform a scan. Why do I need to perform a scan first? Is there a way to properly init without scanning when the address is known?

 Thanks

 Jacques

Jacques Dumarest

unread,
Jan 5, 2018, 7:18:53 AM1/5/18
to MIT App Inventor Forum
 The exact error message I get is: "Error 3300: Error 9007 in Extension BlutoothLE: You cannot connect to a device when the device list is empty! Try scanning again"

 Is there a way to add a known device in the list without scanning?

 Thanks

 Jacques

Evan Patton

unread,
Jan 5, 2018, 10:57:43 AM1/5/18
to MIT App Inventor Forum
Thanks. I'll record this as an issue. In the meantime, you might want to look into the AutoReconnect property. Depending on what you want to do, it might be a good workaround. Once you have connected to a device the BLE component will remember it and attempt to reconnect in the future. However, it does not work between application restarts.

Regards,
Evan

gerrikoio

unread,
Jan 7, 2018, 3:09:57 PM1/7/18
to mitappinv...@googlegroups.com
Surely a scan is required to confirm that the BLE device is actually present before initiating a connect request. The trick is to initiate the BLE scan automatically at the start of screen initialise and this scanning process will continue until you time it out etc. The unfortunate bit is that we don't have a filter process built in for the scanning method to make it more efficient etc.

Then while the scanning process is initiated you would need to periodically check the BLE device list (which auto updates on every new device found), as there is no event trigger whenever a new device is added to the list. Then you stop scanning when it finds the saved BLE device and then you trigger the connect request.

Otherwise, without a scan method you would have to ignore the "trying to connect" error until it connects to the save device and you would still have to initiate a period check on each failed to connect attempt.

So in my opinion, I think the process as it stands is adequate.

Jacques Dumarest

unread,
Jan 8, 2018, 4:08:26 AM1/8/18
to MIT App Inventor Forum
Thanks for your feedback. You are right, I start a scan in Screen1.Initialize, and I check the BLE ID in BluetoothLE1.DeviceFound, and test until I find the right one, then stop scanning.

 I tried to enable AutoReconnect: 

 but it deos not work: If BLE device is powered OFF / Powered ON, it does not reconnect. How should it work? Do I need to do something in "When  BluetoothLE1.Disconnected "? (start a new scan?... or is it fully automatic?

 Thanks

 Jacques

gerrikoio

unread,
Jan 8, 2018, 5:22:55 AM1/8/18
to MIT App Inventor Forum
Yes, I would still keep the "BluetoothLE1.AutoReconnect" method set to true.

If your BLE device powered off then on again, or some other failure scenario caused the disconnect, maybe check your BLE device API / AT Commands, as sometimes you also need to set a software flag to allow an auto-connect. 

Failing all that, there may be edge cases for some reason where none of this applies. Then to account for these, you're right, you would place code within the "when BluetoothLE1.Disconnected" method. first check to see that you have items in the Device list ( BluetoothLE1.DeviceList) and that the BLE device is listed, otherwise need to reinitialise a scan. You may need to check if the device is advertising (not sure here so only implement if next step does not work). If BLE device is in the list then initiate a connect request. 

Also important to note that you will probably need an error handler to catch error messages as all will flag. This may happen in "when BluetoothLE1.ConnectionFailed" method or most likely in the catch-all method "when Screen1.ErrorOccurred". A bit of trial and error here but with the catch-all option it allows you to suppress the error message and reinitiate scans etc.
Reply all
Reply to author
Forward
0 new messages