How to send byte numbers as is, instead of sending them as individual digits in Android Bluetooth outputStream?

0

I want to send, for ex: 239 as 239 over my Bluetooth outputStream but currently, my Bluetooth code sends this as "2", then "3" and then "9"; 3 bytes in total instead of 1 byte and receiving is also happening in 3 bytes.

Code on the Tx side (Java): Sending data every 10 seconds using a timer.

Packaging data as:

    private void startTimer() {
        TimerTask mTimerTask = new TimerTask() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void run() {
                byte[] mByte = new byte[1];

                mTimeStamp += mPeriod;  //mPeriod = 10000; initial mTimeStamp = 0
                mData += 2; //initial mData = 0

                String s = "239";
                mByte = s.getBytes(StandardCharsets.UTF_8);
                mBluetoothConnection.write(mByte);
                s = Integer.toString(((mTimeStamp & 0xFF000000) >> 24));
                mByte = s.getBytes(StandardCharsets.UTF_8);
                mBluetoothConnection.write(mByte);
                s = Integer.toString(((mTimeStamp & 0x00FF0000) >> 16));
                mByte = s.getBytes(StandardCharsets.UTF_8);
                mBluetoothConnection.write(mByte);
                s = Integer.toString(((mTimeStamp & 0x0000FF00) >> 8));
                mByte = s.getBytes(StandardCharsets.UTF_8);
                mBluetoothConnection.write(mByte);
                s = Integer.toString(((mTimeStamp & 0x000000FF) >> 0));
                mByte = s.getBytes(StandardCharsets.UTF_8);
                mBluetoothConnection.write(mByte);
                s = "0";
                mByte = s.getBytes(StandardCharsets.UTF_8);
                mBluetoothConnection.write(mByte);
                s = "0";
                mByte = s.getBytes(StandardCharsets.UTF_8);
                mBluetoothConnection.write(mByte);
                s = Integer.toString(((mData & 0x0000FF00) >> 8));
                mByte = s.getBytes(StandardCharsets.UTF_8);
                mBluetoothConnection.write(mByte);
                s = Integer.toString(((mData & 0x000000FF) >> 0));
                mByte = s.getBytes(StandardCharsets.UTF_8);
                mBluetoothConnection.write(mByte);
            }
        };
        Timer timer = new Timer("schedule", true);

        timer.scheduleAtFixedRate(mTimerTask, 10, mPeriod);
    }

And the write function:

//Call this from the main activity to send data to the remote device
        public void write(byte[] bytes) {
            String text = new String(bytes, Charset.defaultCharset());
            Log.d(TAG, "write: Writing to outputstream: " + text);
            try {
                Log.d(TAG, "write: bytes length: " + bytes.length);
                mmOutStream.write(bytes);
            } catch (IOException e) {
                Log.e(TAG, "write: Error writing to output stream. " + e.getMessage() );
            }
        }

Log cat output for the same:

01-06 23:33:32.012 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: Writing to outputstream: 239
01-06 23:33:32.012 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: bytes length: 3
01-06 23:33:32.018 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: Write Called.
01-06 23:33:32.018 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: Writing to outputstream: 0
01-06 23:33:32.018 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: bytes length: 1
01-06 23:33:32.020 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: Write Called.
01-06 23:33:32.020 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: Writing to outputstream: 4
01-06 23:33:32.020 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: bytes length: 1
01-06 23:33:32.021 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: Write Called.
01-06 23:33:32.021 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: Writing to outputstream: 147
01-06 23:33:32.021 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: bytes length: 3
01-06 23:33:32.022 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: Write Called.
01-06 23:33:32.022 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: Writing to outputstream: 224
01-06 23:33:32.022 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: bytes length: 3
01-06 23:33:32.024 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: Write Called.
01-06 23:33:32.024 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: Writing to outputstream: 0
01-06 23:33:32.024 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: bytes length: 1
01-06 23:33:32.033 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: Write Called.
01-06 23:33:32.033 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: Writing to outputstream: 0
01-06 23:33:32.033 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: bytes length: 1
01-06 23:33:32.033 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: Write Called.
01-06 23:33:32.033 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: Writing to outputstream: 0
01-06 23:33:32.033 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: bytes length: 1
01-06 23:33:32.033 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: Write Called.
01-06 23:33:32.034 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: Writing to outputstream: 60
01-06 23:33:32.034 19455-19682/com.example.user.bluetooth_communication D/BluetoothConnectionServ: write: bytes length: 2

Code on the Rx side (Kotlin):

// Keep listening to the InputStream until an exception occurs
            Log.d("ConnectionActivity", "Inside ListeningThread")
            while (true) { // Read from the InputStream
                if(mBluetoothSocket != null)
                {
                    try {
                        bytes = mBluetoothSocket!!.inputStream.read(buffer)
                        val incomingMessage = String(buffer, 0, bytes)
                        Log.d("ConnectionActivity", "No of bytes: $bytes, " + "InputStream: $incomingMessage")
                        var intentIncomingMessage = Intent("incomingMessage")
                        intentIncomingMessage.putExtra("theMessage", incomingMessage)
                        LocalBroadcastManager.getInstance(mContext).sendBroadcast(intentIncomingMessage)

                    } catch (e: IOException) {
                        Log.e(TAG, "write: Error reading Input Stream. " + e.message)
                        break
                    }
                }
            }

Sample logcat output:

2020-01-12 16:12:10.817 13707-13937 D/ConnectionActivity: No of bytes: 3, InputStream: 239
2020-01-12 16:12:10.848 13707-13937 D/ConnectionActivity: No of bytes: 12, InputStream: 074324000094
2020-01-12 16:12:20.817 13707-13937 D/ConnectionActivity: No of bytes: 3, InputStream: 239
2020-01-12 16:12:20.842 13707-13937 D/ConnectionActivity: No of bytes: 10, InputStream: 0783000096
2020-01-12 16:12:30.817 13707-13937 D/ConnectionActivity: No of bytes: 3, InputStream: 239
2020-01-12 16:12:30.845 13707-13937 D/ConnectionActivity: No of bytes: 12, InputStream: 071221600098

In the above output, what I need is: 1st two lines data should have been as

D/ConnectionActivity: No of bytes: 1, InputStream: 239
D/ConnectionActivity: No of bytes: 1, InputStream: 0
D/ConnectionActivity: No of bytes: 1, InputStream: 74
D/ConnectionActivity: No of bytes: 1, InputStream: 32
D/ConnectionActivity: No of bytes: 1, InputStream: 40
D/ConnectionActivity: No of bytes: 1, InputStream: 0
D/ConnectionActivity: No of bytes: 1, InputStream: 0
D/ConnectionActivity: No of bytes: 1, InputStream: 0
D/ConnectionActivity: No of bytes: 1, InputStream: 94

I think this issue partly, if not fully, has to do with how I am transmitting data. In the Tx side logcat output, one can see, the byte length for 239 is being printed as 3 instead of 1.

java
android
kotlin
bluetooth
asked on Stack Overflow Jan 12, 2020 by Naresh • edited Jan 12, 2020 by Naresh

1 Answer

0

Some java stream basics:

final byte number1 = 89;
final byte number2 = 45;
final byte number3 = 75;
final ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
final DataOutputStream o = new DataOutputStream(byteOutput);
o.writeByte(number1);
o.writeByte(number2);
o.writeByte(number3);
//for other types you can do:
//        o.writeBoolean();
//        o.writeUTF();
//etc.
final byte[] bytes = byteOutput.toByteArray();
o.close();
//now you can send this array.
// to read the data sent simply take your input stream:
// or alternatively: val inputStream = DataInputStream(mBluetoothSocket!!.inputStream)
final ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
final DataInputStream inputStream = new DataInputStream(byteInputStream);
System.out.println(inputStream.readByte()); //prints number 1 aka 89
System.out.println(inputStream.readByte()); //prints number 2 aka 45
System.out.println(inputStream.readByte()); //prints number 3 aka 75

This should allow you to send raw bytes without converting to and from strings.

In Kotlin, you can add .toUByte() to inputStream.readByte() if unsigned bytes are desired.

answered on Stack Overflow Jan 12, 2020 by PiRocks • edited Jan 20, 2020 by Toastrackenigma

User contributions licensed under CC BY-SA 3.0