Android Question Voice Recognition make UDP socket Null

max123

Well-Known Member
Licensed User
Longtime User
Hi all,

in my project I have a working UDP socket that do some things.

Now I want to use Voice Recognition, then send the result string over UDP socket, but for some strange reasons the already initialized UDP socket is Null in the Voice Recognition _Result event.

The application will not crash here, the word is returned as expected, but in the log I see a 'red' error that packet cannot be sent because socket is Null.

When _Result is called, the app is paused, is this the problem ?
How I can solve it ?

Many thanks.

Here is my _Ready event sub:
B4X:
Sub VR_Result (success As Boolean, Texts As List)
 
    Log("VR Success: " & success)

    If success = True Then
        If Texts.Size > 1 Then
            Dim sb As StringBuilder
            sb.Initialize
            sb.Append("ACK").Append(CM).Append("VoiceRecognition").Append(CM)
            For i = 0 To Texts.Size -1
                Log (Texts.Get(i))
                sb.Append(Texts.Get(i))
                If i < Texts.Size Then sb.Append(CM)
            Next
            Log("Invio pacchetto StringBuilder VR: " & sb.ToString)
            SendPacket(sb.ToString)
        Else
            Log("Invio pacchetto VR: " & "ACK" & CM & "VoiceRecognition" & CM & Texts.Get(0))

            ''''SendPacket("ACK" & CM & "VoiceRecognition" & CM & Texts.Get(0))

            Log (Texts.Get(0))
            Dim data As String = "ACK" & CM & "VoiceRecognition" & CM & Texts.Get(0)
            Dim pkt As UDPPacket
            Dim buffer() As Byte = data.GetBytes("UTF8")
            pkt.Initialize(buffer, remoteIP, PortOut) 'DATA,HOST,PortOut   ' Inizializza il pacchetto in invio

            udp.Send(pkt)

            ' Test back the message'
            Dim msgBack As String = BytesToString(buffer, 0, buffer.Length, "UTF8")
            Log("Successfully sent : " & remoteIP & ":" & PortOut & "  [" & msgBack & "]")
        End If
    Else
        Log("Cannot recognize voice")
        ToastMessageShow("Cannot recognize voice", True)
    End If

End Sub

This is the log:
Logger connesso a: asus ASUS_Z00LD
--------- beginning of main
** Activity (main) Pause, UserClosed = true **
** Activity (main) Create (first time) **
File.ExternalWritable = true
Main Dir = /data/user/0/com.vdisplay.pro/files
Activity.NumberOfViews on start: 4
** Activity (main) Resume **
VR: Ciao, parla con me ...
** Activity (main) Pause, UserClosed = false **
running waiting messages (1)
VR Success: true
Invio pacchetto VR: ACK,<>,VoiceRecognition,<>,telefono casa
telefono casa
java.lang.NullPointerException: Attempt to invoke virtual method 'void java.net.DatagramSocket.send(java.net.DatagramPacket)' on a null object reference
Message: 192.168.178.36:8889 [ACK,<>,VoiceRecognition,<>,telefono casa]
** Activity (main) Resume **
WakeLock already held.
 
Last edited:

JohnC

Expert
Licensed User
Longtime User
Please don't make duplicate posts:

 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Please don't make duplicate posts:

Hi @JohnC I posted it because not received any reply.

I work with B4A, it's not just my hobby pastime. I pay taxes even if I do not publish and sell apps. So please understand the situation.

Is B4X suited to work with ?

Sincerely I prefer to pay it and have some support, insted of have it free and not have support.

But this is not more possible I think.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
But this is not more possible I think.
Now I'm not too happy to help you...

Anyway, it looks like you are making the big mistake of not using B4XPages. Such issues will not happen with B4XPages.

Never write communication related code in activities. Move it to the starter service (not needed with B4XPages).
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Now I'm not too happy to help you...

Anyway, it looks like you are making the big mistake of not using B4XPages. Such issues will not happen with B4XPages.

Never write communication related code in activities. Move it to the starter service (not needed with B4XPages).
Hi @Erel , thanks for your reply.

Please don't take this badly, I've been using B4X for a bunch of years, since around 2017 I think, and will continue to use it.

I got a little pissed because I posted a lot of tutorials and free code, but when I asked a simple question a lot of people read it but no one answered. This has already happened several times and forced me not to be able to work.

Thanks for your advices, I have used B4XPages several times, but the problem I have encountered with it is that when I switch to another app and back, sometimes it works, other times the apps crash.

I'm sure I'm doing something wrong but I don't understand what. I have an app of mine that I've been working on for 5 years, I converted it to B4XPages to run in the background but it still has that problem. For the rest the app is almost finished but with that problem I can't release it on Google Play Store.

And sorry for my bad english.

Thanks
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
I will do it.
I appreciate a lot Your work on B4X.

Many Thanks.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Did you find the problem?
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Thanks, no not solved @JohnC, on all cases after VR sub is called with results the UDP socket is broken, not initialized, not only inside this sub but anywhere.

The only solution I found is to put this (to reinitialize again) before use the socket again and send messages.
It is not super, sometime works sometime does not works:

B4X:
    If udp.IsInitialized = False Or udp = Null Then
        udp.Initialize("UDP", PortIn, bufSize)
    End If

Note that now I converted the app to B4XPages, but I have some problems to know how really initialize the UDP socket, sometime I return back to the app and UDP socket is initialized, sometime it reinitialize, sometime an error occour that the port is already in use.

I see Appear and Disappear events, may use these ?

On Activity I Initialize the socket in Activity_Resume (or Activity_Create in FirstTime) and Close in Activity_Pause.

What I do not know is that once initialized the socket, should be usable until I press BACK key, right ?
But this do not happen, put the app in background, foreground some times, will end up to crash.

For sure I do something wrong.
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
From what I understand you are trying to do, this is what I would try next..

1) Create a service that it's sole purpose is to send data via UDP.
2) When the service starts, it will create the UDP object before it sends the data.
3) The service then sends the desired UDP data
4) After the data is sent, it will close and destroy the UDP object
5) The service will then stop itself

Then call this service (passing the data and port info in global vars) every time you want to send UDP data.

See if this will then reliably send UDP packets.
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
From what I understand you are trying to do, this is what I would try next..

1) Create a service that it's sole purpose is to send data via UDP.
2) When the service starts, it will create the UDP object before it sends the data.
3) The service then sends the desired UDP data
4) After the data is sent, it will close and destroy the UDP object
5) The service will then stop itself

Then call this service (passing the data and port info in global vars) every time you want to send UDP data.

See if this will then reliably send UDP packets.
Thanks @JohnC for tips,

my app works as WiFi TFT display for ESP8266/ESP32, for these micros I developed a library in Arduino enviroment, ESP send data as fast possible, Android receive fast commands, like 200-1000 every second.

For these reasons the UDP socket should start once and always remain active, I never close (destroyed) it, and is a problem reinitialize the socket before send back any packet, require time.... and I still search to optimize and decrease time.

Note that it send just some acknowledge packets to remote requests, eg, returns screen width, height, ping etc.

The main work here, is to receive (and parse) packets and socket should always be active to no miss packets sent from ESP.

I already used Services and it shoud work in background until the app is manually closed and Service destroyed, yes.

I will try to completely move UDP on a Service as Your and @Erel advices and initialize once on Service_Create.

May I can use Starter Service ? This is already in the project and never used.

I will try it, but I do not know at all because after VR event sub the socket (in B4XPages without Service) will end up the socket to be broken. I used a lots sockets and never encountered a similar problem. Please can you help me to know because this happen ?

Thanks
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
No, don't use the starter service - it is not designed for what you need to do.
 
Upvote 0
Top