gpt4 book ai didi

java - 如何按原样发送字节数,而不是将它们作为 Android 蓝牙输出流中的单独数字发送?

转载 作者:行者123 更新时间:2023-12-01 18:52:49 25 4
gpt4 key购买 nike

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

Tx 端代码 (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

Rx 端代码 (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。

最佳答案

一些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()

关于java - 如何按原样发送字节数,而不是将它们作为 Android 蓝牙输出流中的单独数字发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59703119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com