I’m using B4J to connect to an HC-05 (SPP mode) via BluetoothSerial and AStream. The first connection works fine. After I disconnect (AStream.Close), any subsequent Connect attempt fails until I power cycle the HC-05.
I get this with reconnect
I’ve tried:
…but nothing reliably allows reconnect without restarting the app or power cycling the HC-05.
Question: Is there any way in B4J (or JavaObject) to fully release the Bluetooth stack so I can reconnect without manually power cycling the HC-05 or restarting app?
Thanks!
I get this with reconnect
Service search completed. Exit code: 4
B4X:
Private Sub btnSearch_Click 'new
If btHC05.IsEnabled = True Then
Dim res As Boolean = btHC05.StartDiscovery
If res Then
ListView1.Items.Clear
txtLog.Text = ""
btnSearch.Enabled = False
btnConnect.Enabled = False
foundDevices.Clear
LogMessage("Bluetooth", "Searching, please wait...")
Else
Log("Error starting discovery")
End If
Else
xui.Msgbox2Async("Bluetooth is disabled. Please turn it on!", "Bluetooth", "Ok", "", "", Null)
End If
End Sub
Private Sub btnConnect_Click 'new
btHC05.CancelDiscovery
' USE (57600 Baud) from HC05 to PIC
If btnConnect.Text = "Connect" Then
If ListView1.SelectedIndex <> - 1 Then
' Connect Bluetooth with Address selected from listview
Dim address As String = foundDevices.Get(ListView1.SelectedItem)
btHC05.Connect(address)
LogMessage("Status", "Connecting to " & ListView1.SelectedItem & "...")
Else
xui.Msgbox2Async("Please select Bluetooth fom the list!", "Select Bluetooth", "Ok", "", "", Null)
End If
' Disconnect
Else
' If its flashing inform with msgbox
If btnFlash.Text = "Stop" Then
Dim sf3 As Object = xui.Msgbox2Async("Flash in progress! Do you want to stop?", "Flashing", "Yes", "", "No", Null)
Wait For (sf3) Msgbox_Result(ret2 As Int)
If ret2 = xui.DialogResponse_Positive Then
LogMessage("Status", "User stop flash!")
EnableFunction
End If
End If
LogMessage("Note", "After disconnect, the HC-05 may require a power cycle before reconnecting due to Bluetooth stack limitations.")
' close Astream
If astream.IsInitialized Then
astream.Close
End If
btnConnect.Text = "Connect"
LogMessage("Status", "Connection Closed")
End If
End Sub
I’ve tried:
- Closing AStream
- Reinitializing BluetoothSerial
- Adding delays
…but nothing reliably allows reconnect without restarting the app or power cycling the HC-05.
Question: Is there any way in B4J (or JavaObject) to fully release the Bluetooth stack so I can reconnect without manually power cycling the HC-05 or restarting app?
Thanks!
Last edited: