Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – Android UDP returning random characters

Posted on November 14, 2022

Solution 1 :

For some reason, no matter how I try to turn the received bytes into a string, it just returns random characters.

When you do new String(bytes, ...) you are attempting to decode text encoded as bytes into Unicode characters … using your platform’s default character set:

  • If the data represented by the bytes is text encoded in a different character set, the characters will not be decoded correctly. You need to use the correct character set.

  • But if the data in the bytes is binary rather than text, it is unlikely that you will be able to turn it into anything other than “random characters” by decoding this way, no matter what character set you are using.

If is common for data sent in UDP packets to be binary. It looks like that is the case here.


I’m sure its a noob mistake somewhere but I just can’t figure out where

I think that your mistake is assuming that all data is fundamentally text.

When there is a specification for the format of those UDP packets, you should start by reading it. The format will determine how you should decode the packets.

A Google search found this for me:

  • https://forums.codemasters.com/topic/50942-f1-2020-udp-specification/

If there was no specification, you would have to reverse engineer the format from example packets. That is tedious and time-consuming, and it isn’t something we can teach you to do.

Problem :

I was trying to make an app that receives telemetry of F1 2020 via UDP.

For some reason, no matter how I try to turn the received bytes into a string, it just returns random characters.

READ  [ANSWERED] permissions - How can I build an android application with an old api (as 16)?
Powered by Inline Related Posts

I’m sure its a noob mistake somewhere but I just can’t figure out where

This is the class that receives and logs the packets:

class ClientListen implements Runnable {
    private Thread t;

    public void run() {
        boolean run = true;
        try {
            DatagramSocket udpSocket = new DatagramSocket(20777);
            byte[] buffer = new byte[2048];
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

            while (run) {
                udpSocket.receive(packet);
                String received = new String(packet.getData(), 0, packet.getLength());
                packet.setLength(buffer.length);
                Log.i("Received data", received);
                try{
                    Thread.sleep(50);

                } catch (InterruptedException e) {}
            }
            udpSocket.close();

        }catch (IOException e) {
            Log.e("UDP client has IOException", "error: ", e);
            run = false;
        }
    }

    public void start() {
        if (t == null) {
            t = new Thread();
            t.start();
        }
    }
}

The thread beggins when I click a button on the app, and I can see it logs data and stops when the game is paused, as it should

The data output is something like I/Received data: �����R<�<li�)C�������GndBƨ[email protected]��N��9��y�}�Ժ�~��g�r��~�ө;��J�qӼ��?�RQ=�k�<h���p�<@84�C�������...

I thought it was beacause I was logging bytes and not a string, but I tried many different ways to turn in into a string and it always has this result.

Comments

Comment posted by forums.codemasters.com/topic/…

Did you really expect to get palintext data over udp? Maybe this can help you:

Comment posted by Don Foumare

Is this about the game from codemasters?

Comment posted by user207421

The sleep is pointless. Remove it.

Comment posted by IVSOP

I’m new to programming and I was sure I was just being dumb somewhere. I know it looks like a stupid question now that I know what I did wrong, so thanks a lot for the help.

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress