且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android的蓝牙UUID APP连接到Android

更新时间:2022-06-07 09:40:14

您可以从BluetoothDevice类获得UUID

You can get the UUID from the BluetoothDevice

    mmDevice = device;

    // Get a BluetoothSocket to connect with the given BluetoothDevice. This code below show how to do it and handle the case that the UUID from the device is not found and trying a default UUID.

    // Default UUID
    private UUID DEFAULT_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 

    try {
        // Use the UUID of the device that discovered // TODO Maybe need extra device object
        if (mmDevice != null)
        {
            Log.i(TAG, "Device Name: " + mmDevice.getName());
            Log.i(TAG, "Device UUID: " + mmDevice.getUuids()[0].getUuid());
            tmp = device.createRfcommSocketToServiceRecord(mmDevice.getUuids()[0].getUuid());

        }
        else Log.d(TAG, "Device is null.");
    }
    catch (NullPointerException e)
    {
        Log.d(TAG, " UUID from device is null, Using Default UUID, Device name: " + device.getName());
        try {
            tmp = device.createRfcommSocketToServiceRecord(DEFAULT_UUID);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    catch (IOException e) { }