Android: How do bluetooth UUIDs work?

121,224

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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.

Solution 5

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

Share:
121,224
ForeverWintr
Author by

ForeverWintr

Interface enthusiast

Updated on November 20, 2020

Comments

  • ForeverWintr
    ForeverWintr over 3 years

    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?

    • 0xC0DED00D
      0xC0DED00D over 11 years
      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.
    • ForeverWintr
      ForeverWintr over 11 years
      @Creator: That contradicts this answer: stackoverflow.com/a/4963069/1286571
    • 0xC0DED00D
      0xC0DED00D over 11 years
      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.‌​pdf
    • Orri
      Orri about 10 years
      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() )?
    • gumuruh
      gumuruh almost 10 years
      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?
    • ForeverWintr
      ForeverWintr almost 10 years
      @gumuruh, I don't know! Maybe you should open a new question?
    • Kevin Lee
      Kevin Lee over 8 years
      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.
    • behelit
      behelit over 6 years
      Then why can't 2 client android devices connect to the same host device using the same UUID ?
  • ForeverWintr
    ForeverWintr over 11 years
    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.
  • nullpotent
    nullpotent over 11 years
    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.
  • Orri
    Orri about 10 years
    Found this one on the web. All?? the UUIDs for BT. sviluppomobile.blogspot.de/2012/11/…
  • gyozo kudor
    gyozo kudor over 8 years
    So the full UUID for a RFCOMM service is: 00000003-0000-1000-8000-00805F9B34FB ?
  • sam msft
    sam msft over 8 years
    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-00805F9B34FB
  • user1725145
    user1725145 almost 5 years
    Link is dead - was it a list of common UUIDs?
  • luke cross
    luke cross over 3 years
    Where do I get these UUIDs and why are they always strings that seem to be random?
  • David C.
    David C. over 3 years
    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.