Android蓝牙UUID将APP连接到ANDROID

 爱被结束_347 发布于 2023-01-30 13:26

我正在构建一个Android应用程序,它可以在设备上保留蓝牙连接的跟踪,并在超出范围时触发警报.

Android文档要求UUID以建立连接.

'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;
}

我没有在两个设备上安装应用程序,所以我没有设置我自己的UUID,我想使用android的代替......但我无法在任何地方的文档中找到它.

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

1 个回答
  • 您可以从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) { }
    

    2023-01-30 13:28 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有