我希望从Android设备中的应用程序连接到远程设备(配对)。远程设备是HC-05模块。我的鳕鱼是:
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket tmp = 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
tmp = mmDevice.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
}
mmSocket = tmp;
}
@Override
public void run() {
// Cancel discovery because it will slow down the connection
ba.cancelDiscovery();
try {
mmSocket.connect();
} catch (IOException e) {}
// Do work to manage the connection (in a separate thread)
// manageConnectedSocket(mmSocket);
// connected();
tv1.setText("connect");
}
/** Will cancel an in-progress connection, and close the socket */
@SuppressWarnings("unused")
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
}
}
}
但是在mSocket.connect()行中出错。 当我运行我的应用程序然后获取消息:不幸的是(应用程序名称)已停止。
请帮忙。
答案 0 :(得分:0)
不要默默忽略IOException
。如果你得到的IOException
意味着你。由于任何原因,无法创建套接字。然后mSocket保持为null,因此您将获得异常。
你可能在清单中没有蓝牙权限。