Android Question Does Serial.Disconnect really close the Bluetooth connection?

Alessandro71

Well-Known Member
Licensed User
Longtime User
I'm getting some inconsistent behavior in after a Serial.Disconnect call on Bluetooth connections.

Here is the tipical sequence of events:
- Call Serial.Connect(macaddress) with success
- Data exchange happens with no errors using AsyncStreams.Write
- Call Serial.Disconnect
- after a while call again Serial.Connect(macaddress)

after the last step, the outcome may be either one of these, with no apparent condition causing one or the other:
a) connection successful and the previous cycle runs again with no problems
b) java.io.IOException: read failed, socket might closed or timeout, read ret: -1

if the exception in case b) occurs, a power cycle of the remote Bluetooth device will cause the next Connect attempt to succeed, otherwise, no other connections will be established.
note that even if I kill and restart the app, the Connect will fail until the remote device is also reset, so I think the loose end, is at the remote device, not at the phone side.

am I missing some connection closing detail?
 

Alessandro71

Well-Known Member
Licensed User
Longtime User
1. Are you also closing AsyncStreams?
2. Dim and Initialize the serial object before you reuse it.

Serial.Disconnect does close all its resources.
Since the Serial object is a Global, does an Initialize will suffice, or a Dim is required (and some code restructuring also...)
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
Better to "dim" it again:
B4X:
Dim Serial1 As Serial
Serial1.Initialize(...)

My guess is that it won't change anything but it is worth a try. It is possible that the problem is in the Bluetooth peripheral object.
I can "dim" again a Global object, just by using the same name?
The issue happens with 2 different peripheral objects, from different manufacturers, with different hw components
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
update on this issue:

it looks like the remote Bluetooth device has erratic behavior only if there is data exchange going on:
a loop of Serial.Connect/Serial.Disconnect, with no AsyncStreams calls in between, runs flawlessy.
this seems to point out that the Serial object is not the culprit here
 
Upvote 0
Top