我有一個連接到藍牙設備的代碼,打開一個與正在運行的線程通信的藍牙套接字,該線程運行在主要活動中運行的函數。我想將所有連接序列移動到另一個活動,然后像現在一樣從主線程操作線程。問題是它們都是相連的。我希望可以選擇在這些活動之間發送消息(意味著保持套接字從其他活動運行),即這條消息: mHandler.obtainMessage(CONNECTING_STATUS, 1, -1, name) .sendToTarget();因為不可能在活動之間傳遞處理程序,我不知道如何/如果可能的話。做這樣的事情最好的方法是什么?添加了部分代碼。謝謝。 mHandler = new Handler(){ public void handleMessage(android.os.Message msg){ if(msg.what == MESSAGE_READ){ String readMessage = null; try { readMessage = new String((byte[]) msg.obj, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } RxMessage = readMessage.split(" "); if (sH.isStringInCorrectOrder(RxMessage,Weight)) populateListView(RxMessage); mReadBuffer.setText(readMessage); } if(msg.what == CONNECTING_STATUS){ if(msg.arg1 == 1) mBluetoothStatus.setText("Connected to Device: " + (String)(msg.obj)); else mBluetoothStatus.setText("Connection Failed"); } } };private void connectBT (){ mBluetoothStatus.setText("Connecting..."); // Get the device MAC address, which is the last 17 chars in the View final String address = "98:D3:31:30:39:75"; final String name = "HC-06"; // Spawn a new thread to avoid blocking the GUI one new Thread() { public void run() { boolean fail = false; BluetoothDevice device = mBTAdapter.getRemoteDevice(address); try { mBTSocket = createBluetoothSocket(device); } catch (IOException e) { fail = true; Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_SHORT).show(); }
添加回答
舉報
0/150
提交
取消