0

I am trying to get the I/Q signals from an RTL-SDR dongle (RTL2832U R820T2) connected to an android phone (samsung galaxy 7, android 8.0) using an OTG cable. I am using the rtl-tcp-android driver. I have installed the driver by compiling and downloading from it's source code, and I can receive FM signals using the SDR Touch app (created by the driver's author) so as far as I understand the driver installation itself should be fine.

The github page for the driver explains how to get I/Q samples. It says once the driver has been properly invoked using an Android intent I should be able to just read interleaving I/Q data in uint_8 format if I can read the driver's output using a tcp client. I am trying to use this answer to make a tcp client. In the TcpClinet class, inside the while (mRun) loop, I have changed the line

mServerMessage = mBufferIn.readLine();   

to

mServerMessage = Integer.toString( mBufferIn.read() );  

Apparently that answer expects the tcp data to come in as lines of text, while the data I should be getting is continuous streams of uint_8 data. (in fact, if I don't make this change the program eventually crashes with an outofmemory exception at the readLine() function). I have also changed the values of SERVER_IP and SERVER_PORT member variables in TcpClient class with appropriate values for my device. Everything else related to the tcp client is same as in that answer.

In onProgressUpdate() I am getting random sequences of 0x7f and 0xfffd.

This is the onProgressUpdate() function I am using now in MainActivity:

@Override
protected void onProgressUpdate(String...values){  
   super.onProgressUpdate(values);  
   Log.d("MYLOG", "response " + values[0] + String.format( "(0x%x)", Integer.parseInt( values[0] ) );  
}

Here is a section of my logcat:

2019-10-31 14:37:21.629 7232-7232/? D/MYLOG: response 65533(0xfffd)
2019-10-31 14:37:21.633 7232-7232/? D/MYLOG: response 127(0x7f)
2019-10-31 14:37:21.635 7232-7232/? D/MYLOG: response 127(0x7f)
2019-10-31 14:37:21.643 7232-7232/? D/MYLOG: response 127(0x7f)
2019-10-31 14:37:21.645 7232-7232/? D/MYLOG: response 127(0x7f)
2019-10-31 14:37:21.651 7232-7232/? D/MYLOG: response 65533(0xfffd)
2019-10-31 14:37:21.654 7232-7232/? D/MYLOG: response 65533(0xfffd)
2019-10-31 14:37:21.655 7232-7232/? D/MYLOG: response 127(0x7f)
2019-10-31 14:37:21.657 7232-7232/? D/MYLOG: response 127(0x7f)
2019-10-31 14:37:21.658 7232-7232/? D/MYLOG: response 65533(0xfffd)
2019-10-31 14:37:21.663 7232-7232/? D/MYLOG: response 65533(0xfffd)
2019-10-31 14:37:21.666 7232-7232/? D/MYLOG: response 127(0x7f)
2019-10-31 14:37:21.669 7232-7232/? D/MYLOG: response 127(0x7f)
2019-10-31 14:37:21.671 7232-7232/? D/MYLOG: response 65533(0xfffd)
2019-10-31 14:37:21.673 7232-7232/? D/MYLOG: response 127(0x7f)
2019-10-31 14:37:21.687 7232-7232/? D/MYLOG: response 127(0x7f)
2019-10-31 14:37:21.689 7232-7232/? D/MYLOG: response 65533(0xfffd)  

I am pretty sure this is not correct, and I suspect there is something wrong with the tcp client code. How can I modify this code to properly talk with the rtl-tcp-android driver?

EDIT 1---

This is an edit to the code that I tried but didn't work (based on this answer to the same linked question):

mRun = true;
try {
    InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
    Log.e("TCP Client", "C: Connecting...");
    Socket socket = new Socket(serverAddr, SERVER_PORT);
    try {
        mBufferOut = new PrintWriter(socket.getOutputStream());
        Log.e("MYLOG", "TCP Client; C: Sent.");
        mBufferIn = new DataInputStream(socket.getInputStream());
        int charsRead = 0; short[] buffer = new short[1024]; //choose your buffer size if you need other than 1024

        while (mRun) {
            // charsRead = mBufferIn.read(buffer);
            // int length = mBufferIn.readInt();
            int length = 5;
            // Log.d("MYLOG", String.format("length = %d", length ) );
            if(length > 0){
                byte[] message = new byte[length];
                // mBufferIn.readFully(message, 0, message.length);
                mBufferIn.read(message, 0, message.length);
                // just display here instead of sending as a String to onProgressUpdate()
                for(int i=0; i<length; i++){
                    Log.d("MYLOG", String.format("message[%d] = %d (0x%x)", i, message[i], message[i]) );
                }
            }
            // mServerMessage = new String(buffer).substring(0, charsRead);
            if (mServerMessage != null && mMessageListener != null) {
                mMessageListener.messageReceived(mServerMessage);}
            mServerMessage = null;
        }
        Log.e("MYLOG", "RESPONSE FROM SERVER; S: Received Message: '" + mServerMessage + "'");  

mBufferIn declaration has been changed to

private DataInputStream mBufferIn;  

I would like to first make sure the correct data is coming in the TcpClient class's run() function itself, before the program does anything else. With this update I am getting 0x7f and 0x80 in the logcat.

user13267
  • 6,871
  • 28
  • 80
  • 138
  • Dont make a String of the bytes you read(). After that you use that string (i think) in `onProgressUpdate(String...values)` . So twice a conversion. `onProgressUpdate(Byte...values)` would be better. Keep it clean. – blackapps Oct 31 '19 at 10:59
  • You should also not use (Buffered)Readers or (InputStream)Readers as they are for text messages. Just use a DataInputStream. No readers. – blackapps Oct 31 '19 at 11:08
  • Or a BufferedInputStream. – blackapps Oct 31 '19 at 11:16
  • @blackapps I have tried just displaying them after the `mServerMessage = Integer.toString( mBufferIn.read() ); ` line, changed it from reading Int to byte, but I still get the same type of numbers, 0x7f and 0x80 – user13267 Oct 31 '19 at 12:07
  • `changed it from reading Int to byte` ??? Please post your new code with all the adaptions i suggested. – blackapps Oct 31 '19 at 12:33
  • @blackapps added part of my update code to show what I meant, under EDIT 1 – user13267 Nov 01 '19 at 00:30
  • Why 5? Quote `By default each sample consists of two 8 bit unsigned bytes - one for the I and one for the Q part of the sample. ` Beter take an even amount. `mBufferIn.read(message, 0, message.length);` read() returns the actual amount of bytes read. Better: `int nread = mBufferIn.read(message, 0, message.length); charsRead+=nread;`. – blackapps Nov 01 '19 at 08:49
  • `With this update I am getting 0x7f and 0x80 in the logcat` OK. And what did you expect? – blackapps Nov 01 '19 at 08:51
  • `mMessageListener.messageReceived(mServerMessage);}` What is `mServerMessage`? Are you again converting bytes to String?` – blackapps Nov 01 '19 at 08:54
  • `// just display here instead of sending as a String to onProgressUpdate()` You would send the bytes to onProgressUpdate(), remember? – blackapps Nov 01 '19 at 08:55
  • `mBufferOut = new PrintWriter(socket.getOutputStream());` If you have to send byte commands to the device then dont use a PrintWriter. No Writers or Readers if handling bytes. – blackapps Nov 01 '19 at 08:57
  • @blackapps 5 was just a random number, I wanted to first make sure that `mBufferIn.read(message, 0, message.length);` itself was returning the correct value or not. I chose 5 to printf() a small amount of data within the while(mRun) loop. It is actually working the problem was due to the driver not being initialized properly – user13267 Nov 04 '19 at 06:38

0 Answers0