Change Text when Bluetooth disconnected

Hello,

i can change the text to “connected” and background color to “green” when bluetooth is connected but when i unplug the bluetooth receiver then it doesnt change the text to “disconnected” and background color to “red”.
neither this
1
nor this work
2

what am i doing wrong?

thanks in advance

1 Like

The when.clock1.timer run when the timer as gone off…it has triggered?

i dont fully understand your question but the timer is always enabled and always fires, interval is set to 100 and when i connect to the bluetooth receiver the text turns from “not connected” to “connected” and goes green

This went into AI2 last night in nb183:

  • Add DisconnectOnError property to BluetoothClient

See the release notes at http://appinventor.mit.edu/ai2/ReleaseNotes.html

It might be just what you need, if combined with polling the BlueTooth connection by trying to send regularly.

seems to work fine, thanks :slight_smile:

(added to FAQ)

according to the documentation Connectivity

DisconnectOnError
Specifies whether BluetoothClient/BluetoothServer should be disconnected automatically when an error occurs.

I don't think so, because you forgot to poll the bluetooth device as @ABG suggested

as the bluetooth protocol was designed, there is no way to detect, if a connection still is alive, therefore you have to send regularly (i.e. poll) some data to the device. And if you get an error, then you know, that the connection was lost...

Taifun


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

1 Like

ErrorNumber is 516

@Juan_Antonio the screenshot looks pretty good…

however let me suggest to set the new property DisconnectOnError to true during Screen.Initialize to disconnect automatically when an error occurs
you also can remove the DisconnectOnError statement in the Clock.Timer event…

Taifun

In this particular app, I am continuously polling the the bluetooth module for temperature updates. Do you think it is possible that I could write something that looks to see how long it has been since the last update and if it has been over a certain amount of time, make the assumption that the bluetooth has disconnected and act on it?

When the app gets error 516, it writes a text indicating: connection failure at hh:mm

Here an example:

Hello i am also having problem with the bluetooth connection checking... I have set a clock where every 3 secs it runs the blocks, but when i turn off the bluetooth device, the button keeps saying connected instead running the "else" block

Thank you in advance Panagiotis

See Change Text when Bluetooth disconnected

read the complete thread again to understand, why this happens

as the bluetooth protocol was designed, there is no way to detect, if a connection still is alive, therefore you have to send regularly (i.e. poll) some data to the device. And if you get an error, then you know, that the connection was lost…

Taifun

I have a "BT Terminal" application that can detect that the BT device is turned off without sending any data. Therefore, it must be said that there is some way, only the component in app inventor does not support it. That's why you have to deal with it in a different way.

his might be BLE then...
the old bluetooth 3 specification is not able to do it...

Taifun

I don't have any BLE module at home. Tested on the xm-15b module, this is a classic BT module. I do not know how the author of this application solved this problem, but it does not send anything because I would see an echo.

Unless you can send an empty string with bt? Maybe reading in the loop itself will cause an error if the device is turned off?

Thats true event headsets when are not connected are flashing fast which shows that the connection is lost.. As you said the mit appinventor needs to send something to realize that connection is lost.

Thank you all regards

I also tried this..... and althought the notifier appears, the button doesnt return to the blocks i have below the notifier.... any thoughts ?