Android Question Resetting the bluetooth adapter

dara hayes

Member
Licensed User
Longtime User
following several other posts re people suffering from various java errors service
java.io.IOException: [JSR82] connect: Connection is not created (failed or aborted). or just discovery failed or what ever the reason , once you get these errors you tend to keep getting them unless you power down the android device , at least this has been my experience so far, I have been looking to see what sort of tidy up code (what has to be tidied up ) people have been executing if one fails to connect on discovery or the discovery process itself fails, so I tried the timer as used in B4A bridge example and Disable the adapter and wait a second or two turn it back on again , and wait for the Admin changed status ADMIN STATUS CHANGED to come back to ON and use this event to drive the start of the discovery process once more , but I have a short circuit in the discovery process to find my hard coded name and mac so if I see this device I want to pair with I immediately connect to it and I dont wait for the end of the process, I use the discovery completion event only to determine if I haven't found my hard coded partner device then I exit the application and shut down, Unfortunately what is happening is I get a loop where I try to connect it fails with an IO Exception , so I go into a Reset the Bluetooth adapter turn it off and wait routine but it fails the second time round and goes round again and again , failing the same way each time , so what else besides turning off the adapter and turning it back on again do I have to do to clear pairing or discovery errors in Bluetooth
I attach the code below

code:
________________________________________________________________
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")

If FirstTime Then
admin.Initialize("admin")
serial1.Initialize("serial1")
resetTimer.Initialize("ResetTimer", 1000)
ticker = 0
'admin.enable
End If

mymac="" ' initalise the string for mymac
Activity.LoadLayout("Main")

End Sub
____________________________________________
Sub Activity_Resume
End Sub
_________________________________________________
Sub Activity_Pause (UserClosed As Boolean)
If UserClosed = True Then
serial1.Disconnect
btnConnect.Enabled = True
blocking_panel.Visible = True
End If
End Sub

Sub btnConnect_Click 'connect to bluetooth and open a serial connection

If admin.IsEnabled = False Then
admin.Enable
Return
End If



If admin.IsEnabled = False Then
If admin.Enable = False Then
ToastMessageShow("Error enabling Bluetooth adapter.", True)
Else
ToastMessageShow("Enabling Bluetooth adapter...", False)
'the StateChanged event will be soon raised
End If
End If
foundDevices.Initialize
resetTimer.Enabled = True

End Sub
Sub Serial1_Connected (Success As Boolean)
ProgressDialogHide

If Success = True Then
ToastMessageShow("Connected to Trigger Box Now.", True)
blocking_panel.Visible = False 'show we are able to communicate now
connected =True

If AStream.IsInitialized = False Then
AStream.Initialize(serial1.InputStream,serial1.OutputStream, "AStream")

End If

Else
Log(LastException.Message)
ToastMessageShow("Connection Failed... Resetting BlueTooth Adapter", True)
' ok we take control
reset_adapter ' turn off then on again bluetooth adapter wait for status to change

End If


End Sub
_______________________________________________
Sub start_bluetooth

foundDevices.Initialize
If admin.StartDiscovery = False Then
ToastMessageShow("Error starting discovery process.", True)
Log(LastException.Message)
Else
ProgressDialogShow("Searching for devices...")

End If

End Sub
____________________________________
Sub reset_adapter
admin.Disable ' turn off adapter now
resetTimer.Enabled = True ' start one second timer

End Sub
_____________________________________________________

Sub Admin_StateChanged (NewState As Int, OldState As Int)


If NewState = admin.STATE_ON AND waitingForBluetoothToBeReady= False Then
Log("Bluetooth adapter is now on")
admin.StartDiscovery
ProgressDialogShow("Searching for devices...")
Return
End If

If NewState = admin.STATE_ON AND waitingForBluetoothToBeReady Then
Log("Bluetooth adapter is now on")

If admin.StartDiscovery = False Then

ToastMessageShow("Error starting discovery process.", True)
Log(LastException.Message)
Else
ProgressDialogShow("Searching for devices...")
waitingForBluetoothToBeReady= False
End If

End If

If NewState = admin.STATE_OFF Then
Log("Bluetooth adapter is now off" )
End If

End Sub

Sub Admin_DiscoveryFinished
ProgressDialogHide


If mymac = "" Then

ToastMessageShow("SmarT-Trigger box not found.", True) 'we should get out of doge here and abandon all hope
ExitApplication ' this will get us out of trouble

End If




End Sub
_________________________________________________________

Sub Admin_DeviceFound (Name As String, MacAddress As String)

Log(Name & ":" & MacAddress)
Dim nm As NameAndMac
nm.Name = Name
nm.Mac = MacAddress
foundDevices.Add(nm)



If nm.Name= "TRIGGER4_101" OR nm.Mac ="00:0A:4F:01:29:D4" Then
mymac = "00:0A:4F:01:29:D4"
myname="TRIGGER4_101"
connectedDevice.mac = mymac
connectedDevice.Name= myname
serial1.Disconnect
serial1.Connect(connectedDevice.Mac)
btnConnect.Enabled = False
connected =False
ProgressDialogShow("Trying to connect to: " & connectedDevice.Name & " (" & connectedDevice.Mac & ")")
Return
End If

ProgressDialogShow("Searching for devices (~ device found)...".Replace("~", foundDevices.Size))
End Sub
 

dara hayes

Member
Licensed User
Longtime User
I have just seen Erels post about inter platform operability with android , and an other post about pre pairing , without user intervention, with pre pairing ,its not an issue for connection with my ASUS but without its not reliable at all , as it seems with my particular android device if pre paired all my android code works fine first and every time , if not it doesnt work first time every time ,. so pre pairing is a good idea for my own device vagaries
ME173X ASUS 7 inch tablet runing Android 4.2.2 , to the device no problem so a turn on and off and re pair using the android pairing itself seems to do the trick for me thanks to all the other posters on this subject !
 
Last edited:
Upvote 0
Top