【发布时间】:2016-11-14 17:04:18
【问题描述】:
我正在尝试在我的 Android 应用程序中连接蓝牙设备,但我遇到了问题。 似乎我第一次尝试时永远无法连接到蓝牙设备。
我在 BluetoothConnectThread 中有以下代码:
public class BluetoothConnectThread extends Thread {
private BluetoothSocket mmSocket;
private BluetoothDevice mmDevice;
private Context context;
private BluetoothManager manager;
public BluetoothConnectThread(BluetoothDevice mmDevice, UUID uuid, Context context, BluetoothManager manager) {
this.context = context;
this.manager = manager;
this.mmDevice = mmDevice;
this.uuid = uuid;
}
public void run() {
try {
System.out.println("Try to connect");
mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, Integer.valueOf(1));
mmSocket.connect();
} catch (Exception connectException) {
connectException.printStackTrace();
try {
mmSocket.close();
System.out.println("Couldn't establish Bluetooth connection! (1)");
} catch (IOException closeException) {
closeException.printStackTrace();
System.out.println("Couldn't establish Bluetooth connection! (2)");
}
try {
System.out.println("Try to connect again");
mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, Integer.valueOf(1));
mmSocket.connect();
} catch (Exception connectException2) {
connectException.printStackTrace();
try {
mmSocket.close();
System.out.println("Couldn't establish Bluetooth connection! (3)");
} catch (IOException closeException) {
closeException.printStackTrace();
System.out.println("Couldn't establish Bluetooth connection! (4)");
}
}
}
if(mmSocket.isConnected()) {
if(mmSocket.isConnected()) {
System.out.println("Connected");
//Do something with the connected socket
我在调用 run 方法时收到以下日志:
07-12 14:17:10.906 9941-10518/com.example.niekdewit.test I/System.out: Try to connect
07-12 14:17:10.910 9941-10518/com.example.niekdewit.test W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test W/System.err: at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:573)
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test W/System.err: at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:550)
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test W/System.err: at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:325)
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test W/System.err: at com.example.niekdewit.test.BluetoothConnectThread.run(BluetoothConnectThread.java:33)
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test I/System.out: Couldn't establish Bluetooth connection! (1)
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test I/System.out: Try to connect again
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
07-12 14:17:13.147 9941-9941/com.example.niekdewit.test I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@2e01eda7 time:762005544
07-12 14:17:16.503 9941-9941/com.example.niekdewit.test I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@2e01eda7 time:762008901
07-12 14:17:17.279 9941-10518/com.example.niekdewit.test I/System.out: Connected
我尝试替换 2 行
mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, Integer.valueOf(1));
与
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
但这也不起作用,但它会生成不同的日志。使用这种方法,我第二次尝试也无法连接。
07-12 14:27:15.968 15135-15622/com.example.niekdewit.test I/System.out: Try to connect
07-12 14:27:15.969 15135-15622/com.example.niekdewit.test W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
07-12 14:27:18.205 15135-15622/com.example.niekdewit.test W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
07-12 14:27:18.205 15135-15622/com.example.niekdewit.test W/System.err: at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:573)
07-12 14:27:18.205 15135-15622/com.example.niekdewit.test W/System.err: at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:550)
07-12 14:27:18.205 15135-15622/com.example.niekdewit.test W/System.err: at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:325)
07-12 14:27:18.205 15135-15622/com.example.niekdewit.test W/System.err: at com.example.niekdewit.test.BluetoothConnectThread.run(BluetoothConnectThread.java:36)
07-12 14:27:18.205 15135-15622/com.example.niekdewit.test I/System.out: Couldn't establish Bluetooth connection! (1)
07-12 14:27:18.206 15135-15622/com.example.niekdewit.test I/System.out: Try to connect again
07-12 14:27:18.206 15135-15622/com.example.niekdewit.test W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
07-12 14:27:18.319 15135-15135/com.example.niekdewit.test I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@2e01eda7 time:762610717
07-12 14:27:20.669 15135-15622/com.example.niekdewit.test W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
07-12 14:27:20.669 15135-15622/com.example.niekdewit.test W/System.err: at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:573)
07-12 14:27:20.669 15135-15622/com.example.niekdewit.test W/System.err: at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:550)
07-12 14:27:20.669 15135-15622/com.example.niekdewit.test W/System.err: at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:325)
07-12 14:27:20.669 15135-15622/com.example.niekdewit.test W/System.err: at com.example.niekdewit.test.BluetoothConnectThread.run(BluetoothConnectThread.java:36)
07-12 14:27:20.669 15135-15622/com.example.niekdewit.test I/System.out: Couldn't establish Bluetooth connection! (3)
07-12 14:27:20.854 15135-15135/com.example.niekdewit.test I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@2e01eda7 time:762613252
当我第一次尝试连接这条线时
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
并尝试使用此行进行第二次尝试(“尝试再次连接”)
mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, Integer.valueOf(1));
然后它会生成与我附加的第一个日志相同的日志
我不知道发生了什么? 我希望你们中的某个人能看到我的代码有什么问题,或者指出我正确的方向。
编辑: 如果设备已经配对,则可以在第一次尝试时使用这两种方法进行连接。
EDIT2:
mmSocket = mmDevice.createInsecureRfcommSocketToServiceRecord(uuid);
也不行
【问题讨论】:
标签: android sockets bluetooth connection ioexception