有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何按原样发送字节数,而不是在Android Bluetooth outputStream中作为单个数字发送?

我想发送,例如:239作为239通过我的蓝牙输出流,但目前,我的蓝牙代码发送它作为“2”,然后是“3”,然后是“9”;总共3个字节,而不是1个字节,接收也发生在3个字节中

发送端代码(Java): 使用计时器每10秒发送一次数据

包装数据如下:

    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);
    }

以及写入功能:

//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() );
            }
        }

记录相同的cat输出:

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

接收端(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
                    }
                }
            }

logcat输出示例:

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

在上面的输出中,我需要的是:前两行数据应该是

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

我认为这个问题部分(如果不是全部)与我如何传输数据有关。在Tx侧logcat输出中,可以看到239的字节长度被打印为3,而不是1


共 (1) 个答案

  1. # 1 楼答案

    一些java流基础知识:

    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
    

    这应该允许您发送原始字节,而无需转换字符串

    在Kotlin中,如果需要无符号字节,可以将.toUByte()添加到inputStream.readByte()