gpt4 book ai didi

java - 蓝牙 Android 开发者代码

转载 作者:行者123 更新时间:2023-12-01 15:47:41 25 4
gpt4 key购买 nike

我引用了Android Developer.中的代码在编译了代码并解决了一些错误之后,我无法弄清楚这一点。

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.bluetooth.BluetoothSocket;
import android.os.Handler;
import junit.framework.TestCase;

public class ConnectedThread extends Thread {
private static final String MESSAGE_READ = null;
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
private Handler mHandler = new Handler();
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
}
catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024];
// buffer store for the stream
int bytes;
// bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
break;
}
}
}
/* Call this from the main Activity to send data to the remote device */
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
}
catch (IOException e) { }
}
/* Call this from the main Activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }


}
}

Handler 类型中的 acquireMessage(int, int , object) 方法不适用于参数(String, int ,int, Byte[])

之前使用过此代码的人能否告诉我我需要广告什么或我缺少什么。这可能非常简单。谢谢。

最佳答案

obtainMessage 接受参数,例如

handler.obtainMessage(int)
handler.obtainMessage(int, object)
handler.obtainMessage(int, int, int)
handler.obtainMessage(int, int, int, object);

您已在 handler.obtainMessage() 中传递了 MESSAGE_READ 变量,因此看起来像

handler.obtainMessage(String, int, int, object);

MESSAGE_READ 这是字符串,这就是您收到此错误的原因MESSAGE_READ变量字符串更改为Int

关于java - 蓝牙 Android 开发者代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6819464/

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