Android的蓝牙UUID APP连接到Android [英] Android bluetooth UUID connecting APP to ANDROID

查看:573
本文介绍了Android的蓝牙UUID APP连接到Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建一个Android应用程序,保持设备上的蓝牙连接的轨道,并触发的情况下报警其中一些迷路(远程设备是否超出范围或已关闭其蓝牙)

I'm building an android APP that keeps tracks of the bluetooth connection on a device and triggers an alarm in case some of them get lost (whether the remote device is out of range or turned off its Bluetooth).

的事情是,在Android文件,他们问你一个UUID,以建立连接。

The thing is that on the android documentation they ask you for a UUID in order to make a connection.

一个UUID是通用唯一标识符(UUID)标准的128位格式,用来唯一标识信息的字符串ID。它是用来唯一标识应用程序的蓝牙服务。

A 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.

 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的... ...但是我找不到它的任何地方。

As I am NOT installing an APP on both devices, I don't get to set my own UUID, I want to use android's instead... but I can't find it anywhere.

也许是我没有正确地接近问题......可能你们帮助我吗? :) 在此先感谢

Maybe I'm not approaching the problem correctly... could you guys help me out? :) Thanks in advance

推荐答案

您可以从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) { }

这篇关于Android的蓝牙UUID APP连接到Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆