Android蓝牙UUID将APP连接到ANDROID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21457175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:36:23  来源:igfitidea点击:

Android bluetooth UUID connecting APP to ANDROID

androidbluetoothuuid

提问by FRR

I'm building an android application that keeps tracks of the Bluetooth connection on a device and triggers an alarm when they get out of range.

我正在构建一个 android 应用程序,它跟踪设备上的蓝牙连接并在它们超出范围时触发警报。

The Android documentation asks for a UUID in order to establish a connection.

Android 文档要求提供 UUID 以建立连接。

An 'uuid' is a Universally Unique Identifier (UUID) standardized 128-bit format for a string ID used to uniquely identify information. It's used to uniquely identify your application's Bluetooth service.

“uuid”是用于唯一标识信息的字符串 ID 的通用唯一标识符 (UUID) 标准化 128 位格式。它用于唯一标识您的应用程序的蓝牙服务。

 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 = device.createRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e) { }
    mmSocket = tmp;
}

I am not installing an app on both devices, so I don't get to set my own UUID, I want to use android's instead... but I can't find this in the docs anywhere.

我没有在两台设备上安装应用程序,所以我无法设置自己的 UUID,我想改用 android 的……但我在任何地方的文档中都找不到这个。

Maybe I'm not approaching the problem correctly. Any help will be appreciated. Thanks in advance

也许我没有正确解决问题。任何帮助将不胜感激。提前致谢

回答by Braunster

You can get the UUID from the BluetoothDevice

您可以从 BluetoothDevice 获取 UUID

    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) { }