蓝牙使用设备UUID连接

时间:2015-02-16 17:33:38

标签: android bluetooth

我正在尝试使用蓝牙和以下代码连接到Android设备:

public ConnectThread(BluetoothDevice bluetoothDevice) {
    this.bluetoothDevice = bluetoothDevice;

    ParcelUuid[] uuids =  this.bluetoothDevice.getUuids();

    for(ParcelUuid parcelUuid : uuids) {
        Log.v("BluetoothService", "UUID: " + parcelUuid.getUuid().toString());
    }

    BluetoothSocket tempBluetoothSocket = null;

    try {
        tempBluetoothSocket = this.bluetoothDevice.createRfcommSocketToServiceRecord(uuids[0].getUuid());
    } catch (IOException e) {
        Log.e(TAG, "Socket Type: " + "create() failed", e);
    }

    this.bluetoothSocket = tempBluetoothSocket;
}

UUID的输出如下:

02-16 18:27:34.334: V/BluetoothService(2865): UUID: 0000110a-0000-1000-8000-00805f9b34fb
02-16 18:27:34.334: V/BluetoothService(2865): UUID: 00001105-0000-1000-8000-00805f9b34fb
02-16 18:27:34.334: V/BluetoothService(2865): UUID: 00001115-0000-1000-8000-00805f9b34fb
02-16 18:27:34.334: V/BluetoothService(2865): UUID: 00001116-0000-1000-8000-00805f9b34fb
02-16 18:27:34.334: V/BluetoothService(2865): UUID: 0000110e-0000-1000-8000-00805f9b34fb
02-16 18:27:34.334: V/BluetoothService(2865): UUID: 0000112f-0000-1000-8000-00805f9b34fb
02-16 18:27:34.334: V/BluetoothService(2865): UUID: 00001112-0000-1000-8000-00805f9b34fb
02-16 18:27:34.334: V/BluetoothService(2865): UUID: 0000111f-0000-1000-8000-00805f9b34fb
02-16 18:27:34.335: V/BluetoothService(2865): UUID: 00001132-0000-1000-8000-00805f9b34fb

因此,如果我理解正确,那些是设备支持的UUID。但是,如果这是真的,为什么我无法使用其中一个UUID连接到设备,在我的情况下,索引为0的UUID?

02-16 18:27:34.369: E/BluetoothService(2865): read failed, socket might closed or timeout, read ret: -1
02-16 18:27:34.369: E/BluetoothService(2865): java.io.IOException: read failed, socket might closed or timeout, read ret: -1
02-16 18:27:34.369: E/BluetoothService(2865):   at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:512)
02-16 18:27:34.369: E/BluetoothService(2865):   at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:523)
02-16 18:27:34.369: E/BluetoothService(2865):   at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:315)

1 个答案:

答案 0 :(得分:1)

当停止应用程序而非关闭连接时,我遇到了同样的问题。电话重启后才会显示所需的UUID。 BluetoothDevice已缓存UUID。运行bluetoothDevice.FetchUuidsWithSdp();用于刷新UUIDS列表。