B4R Question Weird behavior with HC-05 and B4A....

Yayou49

Active Member
Licensed User
Hi,

Trying to test a HC-05 with a nano, I'm facing an issue.
I have a code working very well as follow:
(code linked with BluetoothManager)

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("init")
    connection
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then
        Starter.Manager.Disconnect
    End If
End Sub

public Sub connection
    Dim device As NameAndMac
    device.Name = "HC-05"
    device.Mac = "AA:BB:CC:DD:EE:FF"
    Starter.Manager.ConnectTo(device)
    ProgressDialogShow2($"Trying to connect to: ${device.Name} (${device.Mac})"$, False)
End Sub

Public Sub AfterConnect (Success As Boolean)
    ProgressDialogHide
End Sub

Sub Bt1On_Click
    Starter.Manager.SendMessage("Hello")
End Sub

Now, my problem is to release the connection to HC-05 when event is done.
Target is to allow multiple devices to access the same HC-05.
So I've change my code to start the connection on push button and release the connection after sending message:

B4X:
Sub Globals
    Dim Message As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("init")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then
        Starter.Manager.Disconnect
    End If
End Sub


public Sub connection
    Dim device As NameAndMac
    device.Name = "HC-05"
    device.Mac = "AA:BB:CC:DD:EE:FF"
    Starter.Manager.ConnectTo(device)
    ProgressDialogShow2($"Trying to connect to: ${device.Name} (${device.Mac})"$, False)
End Sub

Public Sub AfterConnect (Success As Boolean)
    ProgressDialogHide

    If Success = True Then
            Send
    End If
End Sub

Sub Send
    
    Starter.Manager.SendMessage(Message)
    Starter.Manager.Disconnect
    
End Sub

Sub Bt1On_Click
    connection
    Message = "Hello"
End Sub

With this code, message is not received by HC-05 and app crash.
What do I miss ?

Thanks in advance for your help.
 

Yayou49

Active Member
Licensed User
Is this a B4A or B4R question? What is the error message?

I was asking myself a long time to know if it's a B4R or B4A question ....
I don't know if it is relevant of one of them especially.

BTW, there is no error message.
text sent to the HC-05 is not received and app fully crash without error message.

I know this is poor as information.

Do you think it can be better to add a callsub at the end of Starter.Manager.SendMessage to call a sub that close the connection only when message is send ?
 
Upvote 0

Yayou49

Active Member
Licensed User
sorry for the late, I was out.

Error message is:

connected: true
bluetoothmanager_serial_connected (java line: 224)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.InputStream android.bluetooth.BluetoothSocket.getInputStream()' on a null object reference
at anywheresoftware.b4a.objects.Serial.getInputStream(Serial.java:254)
at rm.unlock.bluetoothmanager._serial_connected(bluetoothmanager.java:224)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
at anywheresoftware.b4a.BA$2.run(BA.java:365)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
 
Upvote 0

Yayou49

Active Member
Licensed User
Add Sleep(500) before you disconnect.

Yes, this solution will surely feet my goal.

BTW, calling the send message from "Public Sub AfterConnect", doesn't work.

Even if I call directly the Starter.Manager.SendMessage(Message) without calling "Send", BluetoothManager.sendMessage is raised but Arduino did not receive the message.

Adding a new button with Starter.Manager.SendMessage(Message) only, works perfectly.

So what can be the reason that calling sendMessage from "Public Sub AfterConnect" doesn't work ???

Thx in advance.
 
Upvote 0
Top