Regarding reconnecting issue / BLE extension

About reconnecting to a known device:
First of all, you save its device address in a TinyDB, like this (my example is about microbits):

Then, when you want to reconnect, you use:


In my app I ask with a notifier whether the device is on, but in your case it would be better to find the device in the list of scanned devices.


can u tell me where the code is executing after bytes received
block.....is it going to when disconnected block if yes we can call a function inside app disconnected block for reconnecting. I'm totally stuck.

  • I have solved your problem at previous topic, what happened?

  • so many blocks are disabled, do you have the previous project?

  • which blocks are changed?

  • still there is a problem?

  • why a new topic???

Same place the sun goes at night.

Dawn will come when new data arrives or you click a button.

2 Likes

Actually the problem is not fixed yet still facing same error

Then you should keep using your previous topic. I merged the two topics.

and also Arman can you explain the logic behind the blocks I mean why u have assigned get global address = 122

where?

I did not see any answers to my questions. Therefore it was solved?
Anyway, my opinion is that you do not want to automatically reconnect the device, just keep it connected.


her why do we need to assign 122 and what is the logic behind this code

because if you directly put call tiny DB get value it may returns a invalid Bluetooth address (122) then we call it to start scanning (if the returned value is 122) or we call it to connect a address (if the returned value not is 122)

yeah seems like you didn't understood my requirement my device sends ADC values (sample values for time being) and goes to sleep for 10 seconds during which the connection also will be gone and the same device wakes up and starts advertising I want the central to immediately re-establish the connection without delay

But still the code is not working as per my requirement, its not establishing an immediate connection

Maybe there is some mutual misunderstanding.
Let me make a few things clear:

  • Your device will only disconnect when either your device switches off or, the app on your phone will take the iniative to disconnect.
  • So, does your device really switch off?
  • The app on your phone may go to sleep if you do not keep it active in the foreground of your phone. If that happens, your device will be disconnected.
  • Maybe your 10 second timer is enough to keep your app active on your phone. The logic you could you could use is this:
    If the device is still connected, ask for the new ADC values, if not connected, try to reconnect using ConnectWithAddress, using the address you have stored at the first connect in a TinyDB.
  • If that fails using the WhenConnectionFailed block, it must be the case that the device is switched off. You cannot do much, except wait until the device comes back on, using your timer.
  • It is not possible for your device to initiate the connection, it can just react to a connection attempt from your app.
  • One of the questions I tried to ask is, do you want your phone to be dedicated to this single app? If not, you must do a lot more, like finding an extension that can help your app to stay active in the background and activate your app every 10 seconds or so. Your connection cannot stay active in the background.

As you see there are a few questions that you need to answer before we can discuss a reasonable scenario.

2 Likes