Android Question Bluetooth - disable-enable-listen behaviour

Arf

Well-Known Member
Licensed User
Longtime User
My bluetooth product is frequently turned on and off, and I need to detect this much quicker than the ACL_DISCONNECT allows. I've moved all bluetooth connection handling into a service, in which BT is enabled at Service start (if not already enabled), then Listen is called, and my products connects quickly and send s data which I get.

If I stop getting data, my unit has been turned off, so I flag the unit as disconnected, but I can't wait around for ACL_DISCONNECT before re-enabling, I need the BT to be listening again asap for when my product powers back up. This part I am having problems with.

So the moment when I detect data isn't arriving and it's clear the unit is off, I do admin.Disable, then in the State_Changed function I enable the BT again if it is disabled, and then admin.Listen right away and expect my unit to reconnect when I power it on (the auto reconnect is initiated by my product, the nexus BT never needs to actively connect), but it never reconnects when I try this.

So am I missing something in the strategy in the above paragraph? The other way I thought to do it was after disabling the BT, if there was some way I could force a restart of the whole service. Is that possible?
 

Arf

Well-Known Member
Licensed User
Longtime User
Just to add, I've now added Astream.Close before I disable the bluetooth, thought this might fix it but still don't get a reconnection after enabling and listening again.
 
Upvote 0

Arf

Well-Known Member
Licensed User
Longtime User
Never mind, I got it working. I added serial1.Disconnect before disabling the bluetooth, and re-init serial when re-enabling, I think that fixed it.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
So, what is order of calls to re-init the connection now when power on?
Icluding BT module admin, serial-port and stream commands.
And stop BT excange, if device is switching off.
 
Upvote 0

Arf

Well-Known Member
Licensed User
Longtime User
When I detect that data isn't coming in (therefore my bluetooth device is switched off) I shut down the bluetooth like this:
AStream.Close
serial1.Disconnect
admin.Disable
Then, my Admin_statechanged function looks like below, it just switched the blueooth back on the moment it gets turned off and gets it listening again:

Sub Admin_StateChanged (NewState As Int, OldState As Int)
If NewState = admin.STATE_ON Then
serial1.Listen
'ToastMessageShow("listening",False)
Else
UNIT_CONNECTED = False
If admin.IsEnabled = False Then
admin.Initialize("admin")
If admin.Enable = False Then
ToastMessageShow("Error enabling Bluetooth adapter.", True)
End If
End If
End If
End Sub

and job done :)
 
Upvote 0

mrodriguez

Member
Licensed User
Longtime User
When I detect that data isn't coming in (therefore my bluetooth device is switched off) I shut down the bluetooth like this:
AStream.Close
serial1.Disconnect
admin.Disable
Then, my Admin_statechanged function looks like below, it just switched the blueooth back on the moment it gets turned off and gets it listening again:

Sub Admin_StateChanged (NewState As Int, OldState As Int)
If NewState = admin.STATE_ON Then
serial1.Listen
'ToastMessageShow("listening",False)
Else
UNIT_CONNECTED = False
If admin.IsEnabled = False Then
admin.Initialize("admin")
If admin.Enable = False Then
ToastMessageShow("Error enabling Bluetooth adapter.", True)
End If
End If
End If
End Sub

and job done :)
I had the same problem. Thanks Arf!
 
Upvote 0
Top