Android: How do bluetooth UUIDs work?
Asked Answered
M

7

92

I don't understand what a bluetooth UUID denotes. Do UUIDs denote protocols (e.g. RFCOMM)? If so, why do the createRfcommSocketToServiceRecord() methods require UUIDs, when they specify rfcomm right in their names? Why does the BluetoothChat sample code have a seemingly arbitrary, hardcoded UUID?

My question arises because, as per this question, I'm getting a null pointer exception when devices running 4.0.4 try to connect (to an external, non-android device) using reflection. However, the solution to that question doesn't work for me. UUID muuid = device.getUuids()[0].getUuid(); raises an exception.

Edit: I've solved that problem by hardcoding the UUID for Serial port service as per this answer (using UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");).

I'm further puzzled by why I need to supply a UUID to create an unsecured rfcomm socket using createInsecureRfcommSocketToServiceRecord(), but not using the reflection method.

Can anyone straighten me out?

Mcloughlin answered 20/12, 2012 at 2:21 Comment(8)
Never used bluetooth connection except for academic purpose, but what I know is, UUID is identifier of the device, something like an address of the device, and remains same for a device.Whoever
@Creator: That contradicts this answer: https://mcmap.net/q/242392/-android-obtaining-uuid-of-a-bluetooth-deviceMcloughlin
Well That's all I knew about bluetooth UUIDs, you can try reading this pdf for better understanding of Bluetooth protocols and UUIDs - people.csail.mit.edu/rudolph/Teaching/Articles/PartOfBTBook.pdfWhoever
Question: Does it have any consequences when the Android phone is not supporting the UUID from the remote device. (UUID not listed in Android ... getUuids() )?Bartolommeo
hey hey hey! i want to ask something, @ForeverWintr, is it okay if i define a single UUID with my own format and used in two android device? I mean, could they communicate each other?Linen
@gumuruh, I don't know! Maybe you should open a new question?Mcloughlin
To find out what uuids are supported on a remote device via SDP, use fetchUuidsWithSdp() with an ACTION_UUID broadcast receiver instead of getUuids(); the latter serves to get Uuids supported by the local Android device. Each notable service like A2DP has a common Uuid that Android recognizes, so a manufacturer doesn't have to ship out a custom app to allow uuids to match every time. That's how Android automatically recognizes headsets and keyboards.Ostrogoth
Then why can't 2 client android devices connect to the same host device using the same UUID ?Alumina
D
18

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

Dewdrop answered 3/7, 2019 at 21:2 Comment(2)
Where do I get these UUIDs and why are they always strings that seem to be random?Antitrust
UUIDs are generated according to RFC 4122. There are five different UUID versions to choose from. The most popular are version 1 (based on timestamp and a MAC address) and version 4 (random). See also en.wikipedia.org/wiki/Universally_unique_identifier. A web page that can generate UUIDs for you is uuidgenerator.net. If you are a Bluetooth SIG member, then they provide an algorithm for generating UUIDs tied to your membership ID.Dewdrop
G
36

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

Genu answered 13/2, 2014 at 16:38 Comment(2)
So the full UUID for a RFCOMM service is: 00000003-0000-1000-8000-00805F9B34FB ?Lindesnes
Note: they list the short form of the service ID which is 32bit. You combine it with the BT SIG’s base UUID 00000000-0000-1000-8000-00805F9B34FB replacing the first 4 digits of the GUID. Ex. RFCOMM is 0x0003, so the UUID for the RFCOMM service is 00030000-0000-1000-8000-00805F9B34FBVeriee
A
24

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

Affenpinscher answered 20/12, 2012 at 17:22 Comment(4)
it usually does? When doesn't it? What I don't understand is why, if I'm creating an RCOMM server, I wouldn't use the RFCOMM UUID.Mcloughlin
Well, UUID is universal identifier. RFCOMM is just a layer on bluetooth protocol stack, and has associated UUID as per bluetooth specification; and is used by the SDP, not entirely sure what for; but you can always read about it in the spec. Now, if you're creating a service on top of RFCOMM protocol, you'll have to provide an UUID as it is required by SDP, otherwise your service won't be discovered.Affenpinscher
Found this one on the web. All?? the UUIDs for BT. sviluppomobile.blogspot.de/2012/11/…Bartolommeo
Link is dead - was it a list of common UUIDs?Acicula
D
18

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

Dewdrop answered 3/7, 2019 at 21:2 Comment(2)
Where do I get these UUIDs and why are they always strings that seem to be random?Antitrust
UUIDs are generated according to RFC 4122. There are five different UUID versions to choose from. The most popular are version 1 (based on timestamp and a MAC address) and version 4 (random). See also en.wikipedia.org/wiki/Universally_unique_identifier. A web page that can generate UUIDs for you is uuidgenerator.net. If you are a Bluetooth SIG member, then they provide an algorithm for generating UUIDs tied to your membership ID.Dewdrop
K
8

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.

Kip answered 14/2, 2014 at 3:49 Comment(0)
P
4

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();

Phonemics answered 9/7, 2015 at 17:28 Comment(0)
C
2

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.

Callie answered 20/11, 2019 at 6:38 Comment(0)
D
1

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

So, use the same UUid for each device

Deform answered 21/12, 2012 at 16:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.