Monitoring a Bluetooth connection

manios

Active Member
Licensed User
Longtime User
Hi all,

I am trying to code the following:

I want to monitor a Bluetooth connection, initiated by the android device, wether the connectoin is still valid (existing). Is this possible?

The status will drive an indicator so the user can see that the connection is still existing. The background is, that the external device should only be operated if the connection is existing. The external device can just send data, it is a serial printer port with an bluetooth adapter. There is no status checking on the external device.
 

boastrike

Member
Licensed User
Longtime User
Did the terminated event work for you manios?

I am having a similar issue where I am trying to monitor the bluetooth connection status. I have it where the application can auto detect when the bluetooth device is in range and it connects fine. The problem comes when the bluetooth device gets turned off. I was hoping the asyncstreams Terminated event would fire when the external device gets turned off but it does not seem to be the case. Does the bluetooth device need to send a disconnect in order for the asyncstreams Terminated event to fire successfully?

I would love any input. Thanks.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
AsyncStreams works with two threads. One is responsible for reading data from the stream and the other for writing data (Bluetooth SPP supports full duplex). The reading thread is blocked until there is data in the stream or when the underlying driver finds out that the connection is closed.
It is possible that the driver doesn't recognize the state change.

One possible solution is to use a timer to send a "ping" every second. If you control both sides of the connection you can reply with a ping. When the connection is broken an error will be raised when trying to send the ping. If no error is raised you will still know that the connection is broken as there is no reply (for example you can break the connection is there is no reply after 10 seconds).
 
Upvote 0

manios

Active Member
Licensed User
Longtime User
I am using the "ping", this works very well for me.
 
Upvote 0

boastrike

Member
Licensed User
Longtime User
Erel & Manios,

Thanks for the input. I have been playing with different ways to detect the loss. Does the 'ping' method still have a 10 second timeout before it notifies? I am getting the Astreams_Error to be raised after 10 seconds. I haven't been able to get the Astreams_Terminated to raise at all. I was hoping for a faster way to determine if the bluetooth device was powered off sooner than that if possible. My app only accepts incoming messages from the device and don't have control of the bluetooth device.

If 10 seconds is what is possible then I guess I will have to work to educate the user to the possibility and they can either wait or I can create a force disconnect/connect option.

I think this is a great product you are building and find myself at this forum every day, waiting for the next great new library or solution to be submitted by some very talented and giving members. Thanks for all the assistance. Jim
 
Upvote 0

maleche

Active Member
Licensed User
Longtime User
Bluetooth auto connect seems popular here.

I connect manually and would like to re-program for auto-connect.

Where may I find the basic B4A code structure to play with?

Is it in any example in this Forum?

Thank in advance!
 
Upvote 0

maleche

Active Member
Licensed User
Longtime User
Thanks Erel!

Will using a Service vice Activity be better for this process to check ("Ping") the device connection/re-connection at set intervals?


Regards!
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…