0
看看有關這個問題的各種示例和問題,我只是想以編程方式連接到以前配對的藍牙設備。在所有的例子中,似乎你必須選擇一個配置文件。我想要做的就像當你進入設置>藍牙並點擊配對設備時發生的事情,它只是連接。到目前爲止,我可以找到綁定的設備:將Android手機連接到配對的藍牙設備,如在設置
BluetoothDevice result;
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> devices = adapter.getBondedDevices();
if (devices != null) {
for (BluetoothDevice device : devices) {
Log.i("TAG", "Device name:" + device.getName());
if (deviceName.equals(device.getName())) {
result = device;
Log.i("TAG", "Found:" + result.getName() + " attempting to connect");
break;
}
}
}
現在我只是想嘗試連接到該設備以同樣的方式從藍牙設置頁面出現。
Jackabe,似乎是在做 「東西」 這裏是我得到:
07-17 10:50:55.346 11494-11494/com.daford.autowificonnect I/BT: Attempting to connect to Protocol: 00001101-0000-1000-8000-00805f9b34fb
07-17 10:50:55.348 11494-11494/com.daford.autowificonnect W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
07-17 10:50:56.263 11494-11494/com.daford.autowificonnect W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
07-17 10:50:56.357 11494-11494/com.daford.autowificonnect I/messageFromSensor: AT+VGS=14
(在這裏掛了那麼一段時間)
07-20 14:43:04.314 18793-28387/com.daford.bluetoothdetection I/BT: Attempting to connect to Protocol: 00001101-0000-1000-8000-00805f9b34fb
07-20 14:43:04.321 18793-28387/com.daford.bluetoothdetection W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
07-20 14:43:05.172 18793-28387/com.daford.bluetoothdetection W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
07-20 14:43:05.187 18793-28387/com.daford.bluetoothdetection W/BT: Fallback failed. Cancelling.
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:738)
at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:697)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:379)
at com.daford.bluetoothdetection.ConnectThread$FallbackBluetoothSocket.connect(ConnectThread.java:199)
at com.daford.bluetoothdetection.ConnectThread.connect(ConnectThread.java:66)
at com.daford.bluetoothdetection.MainActivity.connectToDevice(MainActivity.java:53)
at com.daford.bluetoothdetection.MainActivity$1.run(MainActivity.java:37)
at java.lang.Thread.run(Thread.java:761)
07-20 14:43:05.188 18793-28387/com.daford.bluetoothdetection W/System.err: java.io.IOException: Could not connect to device: 44:5E:F3:9F:35:1D
07-20 14:43:05.188 18793-28387/com.daford.bluetoothdetection W/System.err: at com.daford.bluetoothdetection.ConnectThread.connect(ConnectThread.java:80)
07-20 14:43:05.194 18793-28387/com.daford.bluetoothdetection W/System.err: at com.daford.bluetoothdetection.MainActivity.connectToDevice(MainActivity.java:53)
07-20 14:43:05.196 18793-28387/com.daford.bluetoothdetection W/System.err: at com.daford.bluetoothdetection.MainActivity$1.run(MainActivity.java:37)
07-20 14:43:05.196 18793-28387/com.daford.bluetoothdetection W/System.err: at java.lang.Thread.run(Thread.java:761)
查看我的更新。 –
關於您的更新。這不是一個錯誤。在我看來,該計劃正在向您發出信號,表示應用程序無響應。確保你在一個單獨的線程上運行它到主UI線程。如果你的主UI線程正在做太多的工作連接到設備,那麼應用程序將變得沒有反應。 – jackabe
也許包裝你的device.connect()在一個新的線程可運行 – jackabe