我使用 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 扩展了 Thread 以创建一个单独的进程来监视输入流。此类通过 Handler.sendMessage 与其父类通信