Bluetooth Android Developers Code
-
26-10-2019 - |
Question
I referenced the code from Android Developer. After compliling the code and fooling around with some of the errors, I could not figure this one out.
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) { }
}
}
The method obtainMessage(int, int , object)
in the type Handler is not applicable for the arguments(String, int ,int, Byte[])
.
Could someone that has used this code before possibly tell me what i need to ad or what i am missing. Its probably something really simple. Thank You.
Solution
obtainMessage accept argumets like
handler.obtainMessage(int)
handler.obtainMessage(int, object)
handler.obtainMessage(int, int, int)
handler.obtainMessage(int, int, int, object);
You have passes MESSAGE_READ
variable in handler.obtainMessage()
so it looks like
handler.obtainMessage(String, int, int, object);
MESSAGE_READ
which is String and thats why you are getting this error
change MESSAGE_READ
variable String to Int
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow