我使用ACTION_REQUEST_ENABLE意圖startActivityForResult()。這就導致調用:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == imu.requestCode) {
if (resultCode == RESULT_OK) {
getPairedDevice(); // my function
initializeConnection(); // my function
}
}
}
initializeConnection()創建一個新的線程,它初始化我的輸入插座,創建另一個線程藍牙輸入處理,並創建我的輸出線。這些函數的片段包括:
myServerSocket = dev.createInsecureRfcommSocketToServiceRecord(uuid);
myBluetooth.cancelDiscovery();
myServerSocket.connect();
myBluetoothInputThread = new BluetoothInputThread(myServerSocket, handler); myBluetoothInputThread.setPriority(Thread.MAX_PRIORITY);
myBluetoothInputThread.start();
myBluetoothSocketOutputStream = myServerSocket.getOutputStream();
BluetoothInputThread extends Thread創建一個單獨的進程來監視輸入流。該類通過Handler.sendMessage與其父類進行通信