我从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类型中的方法obtainMessage (int, int , object)
不适用于参数(String, int ,int, Byte[])
。
可能有人在使用此代码之前可能告诉我我需要的广告或我缺少的内容。它可能非常简单。谢谢。
答案 0 :(得分:1)
acquireMessage接受像
这样的争论handler.obtainMessage(int)
handler.obtainMessage(int, object)
handler.obtainMessage(int, int, int)
handler.obtainMessage(int, int, int, object);
您在MESSAGE_READ
中传递handler.obtainMessage()
变量,因此它看起来像
handler.obtainMessage(String, int, int, object);
MESSAGE_READ
这是String,这就是你收到此错误的原因
将MESSAGE_READ
变量String更改为Int