Handle
Android アプリで友達を手伝っていますが、関数を取得できない理由がわかりませんhandleMessage()
。私は現在、Bluetooth経由で通信しようとしています。書き込みを処理するスレッドと読み取りを処理するスレッドがあるため、どこかを台無しにしたと思います。私は本当にThread
s に熟練していません。誰かが私の間違いを見つけられるかどうか疑問に思っていました! "$$$"
以下のコードを書いた後、ブルートゥースチップがコマンドモードになるのを見るので、書き込み機能が機能していることがわかります。
主な活動:
public class MainActivity extends Activity{
....
private final Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
System.out.println("in mHandler");//NOT CALLED
switch (msg.what) {
case (MESSAGE_READING):
byte[] readBuf = (byte[])msg.obj;
String readMessage = new String(readBuf,0,msg.arg1);
System.out.println("READ: " + readMessage);
break;
default:
System.out.println("default!");
break;
}
}
};
...
}
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mConnectThread = new ConnectThread(device, mHandler);
mConnectThread.run();
ListenerThread mListener = new ListenerThread(mConnectThread,mHandler);
mListener.run();
}
接続スレッド:
public class ConnectThread{
public final BluetoothSocket mmSocket;
public final BluetoothDevice mmDevice;
private final Handler mHandler;
public static ConnectedThread mConnectedThread;
public ConnectThread(BluetoothDevice device, Handler mHandler) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket bs = null;
mmDevice = device;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server code
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard Serial Port Service ID
bs = device.createRfcommSocketToServiceRecord(uuid);
} catch (IOException e) {
System.err.println("IOException in ConnectThread");
}
this.mHandler = mHandler;
mmSocket = bs;
}
public void run() {
//TODO Cancel discovery because it will slow down the connection
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}
// Do work to manage the connection (in a separate thread)
mConnectedThread = new ConnectedThread(mmSocket, mHandler);
mConnectedThread.run();
}
...
}
接続されたスレッド:
public class ConnectedThread extends Thread {
....
public void manageConnectedSocket() {
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);
System.out.println("Reading bytes.");
status = MESSAGE_READING;
// Send the obtained bytes to the UI activity
Message msg = Message.obtain(mHandler, MESSAGE_READING, bytes, -1, buffer);
mHandler.sendMessage(msg);
} catch (IOException e) {
System.err.println("Error reading input");
break;
}
status = READY;
}
}
...
}
リスナースレッド:
public class ListenerThread extends Thread {
...
public final BluetoothSocket mmSocket;
private final Handler mHandler;
private final ConnectedThread mConnectedThread;
public ListenerThread(ConnectThread mConnectThread, Handler mHandler){
this.mHandler = mHandler;
mmSocket = mConnectThread.mmSocket;
this.mConnectedThread = mConnectThread.getConnectedThread();
}
public void run(){
while (true){
if (getStatus() == READY){
write("$$$".getBytes());
break;
}
}
}
...
}
他のいくつかの質問を見ましたhandleMessage
が、役に立ちませんでした。何か案は?
編集:これは多くのコードであることに気付きました。基本的に、私は別のスレッドに回っmHandler
ています。ここで悪いことが起こっていると思っていました。と がConnectThread
ありConnectedThread
ますListenerThread
。bluetooth について調べていた Android のドキュメントでは、一部の呼び出し ( write
、read
、device.connect()
) が呼び出しをブロックしているため、バックグラウンドで実行するように言われていました。