Android: 蓝牙 UUID 是如何工作的?

我不明白蓝牙 UUID 是什么意思。UUID 是否表示协议(例如 射频通讯) ?如果是这样,为什么 createRfcommSocketToServiceRecord()方法需要 UUID,而它们却在名称中指定了 rfcomm 权限?为什么蓝牙聊天样本代码看起来有一个任意的、硬编码的 UUID?

我的问题出现了,因为根据 这个问题,当运行4.0.4的设备尝试使用反射连接(到一个外部的非 android 设备)时,我得到了一个空指针异常。然而,这个问题的解决方案对我来说并不奏效。UUID muuid = device.getUuids()[0].getUuid();引发异常。

编辑 : 通过按照 这个答案(使用 UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");)对串口服务的 UUID 进行硬编码,我解决了这个问题。

为什么我需要提供一个 UUID 来使用 createInsecureRfcommSocketToServiceRecord(),创建一个不安全的 rfcomm 套接字,而不使用反射方法,这让我更加困惑。

有人能教教我吗?

128565 次浏览

It usually represents some common service (protocol) that bluetooth device supports.

When creating your own rfcomm server (with listenUsingRfcommWithServiceRecord), you should specify your own UUID so that the clients connecting to it could identify it; it is one of the reasons why createRfcommSocketToServiceRecord requires an UUID parameter.

Otherwise, some common services have the same UUID, just find one you need and use it.

See here

To sum up: UUid is used to uniquely identify applications. Each application has a unique UUid

So, use the same UUid for each device

The UUID is used for uniquely identifying information. It identifies a particular service provided by a Bluetooth device. The standard defines a basic BASE_UUID: 00000000-0000-1000-8000-00805F9B34FB.

Devices such as healthcare sensors can provide a service, substituting the first eight digits with a predefined code. For example, a device that offers an RFCOMM connection uses the short code: 0x0003

So, an Android phone can connect to a device and then use the Service Discovery Protocol (SDP) to find out what services it provides (UUID).

In many cases, you don't need to use these fixed UUIDs. In the case your are creating a chat application, for example, one Android phone interacts with another Android phone that uses the same application and hence the same UUID.

So, you can set an arbitrary UUID for your application using, for example, one of the many random UUID generators on the web (for example).

UUID is similar in notion to port numbers in Internet. However, the difference between Bluetooth and the Internet is that, in Bluetooth, port numbers are assigned dynamically by the SDP (service discovery protocol) server during runtime where each UUID is given a port number. Other devices will ask the SDP server, who is registered under a reserved port number, about the available services on the device and it will reply with different services distinguishable from each other by being registered under different UUIDs.

UUID is just a number. It has no meaning except you create on the server side of an Android app. Then the client connects using that same UUID.

For example, on the server side you can first run uuid = UUID.randomUUID() to generate a random number like fb36491d-7c21-40ef-9f67-a63237b5bbea. Then save that and then hard code that into your listener program like this:

 UUID uuid = UUID.fromString("fb36491d-7c21-40ef-9f67-a63237b5bbea");

Your Android server program will listen for incoming requests with that UUID like this:

    BluetoothServerSocket server = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("anyName", uuid);

BluetoothSocket socket = server.accept();

In Bluetooth, all objects are identified by UUIDs. These include services, characteristics and many other things. Bluetooth maintains a database of assigned numbers for standard objects, and assigns sub-ranges for vendors (that have paid enough for a reservation). You can view this list here:

https://www.bluetooth.com/specifications/assigned-numbers/

If you are implementing a standard service (e.g. a serial port, keyboard, headset, etc.) then you should use that service's standard UUID - that will allow you to be interoperable with devices that you didn't develop.

If you are implementing a custom service, then you should generate unique UUIDs, in order to make sure incompatible third-party devices don't try to use your service thinking it is something else. The easiest way is to generate random ones and then hard-code the result in your application (and use the same UUIDs in the devices that will connect to your service, of course).

The UUID stands for Universally Unique Identifier. UUID is an simple 128 bit digit which uniquely distributed across the world.

Bluetooth sends data over air and all nearby device can receive it. Let's suppose, sometimes you have to send some important files via Bluetooth and all near by devices can access it in range. So when you pair with the other devices, they simply share the UUID number and match before sharing the files. When you send any file then your device encrypt that file with appropriate device UUID and share over the network. Now all Bluetooth devices in the range can access the encrypt file but they required right UUID number. So Only right UUID devices have access to encrypt the file and others will reject cause of wrong UUID.

In short, you can use UUID as a secret password for sharing files between any two Bluetooth devices.