天天看點

藍牙UUID及其128位換算

摘自藍牙核心規格文檔《Core_V4.0.pdf》

2.5.1 UUID

A UUID is a universally unique identifier that is guaranteed to be unique across all space and all time.  UUIDs can be independently  created in a distributed fashion. No central registry of assigned UUIDs is required.  A UUID is a 128-bit value.

To reduce the burden of storing and tr ansferring 128-bit UUID values, a range of UUID values has been pre-allocated for assignment to often-used, regis-tered purposes. The first UUID in this  pre-allocated range  is known as the Bluetooth Base UUID and has the value 000000 00-0000-1000-8000-00805F9B34FB, from the Bluetooth  Assigned Numbers document. UUID val-ues in the pre-allocated range have aliases that are represented as 16-bit or 32-bit values. These aliases  are often called 16-bit and  32-bit UUIDs, but it is important to note that each actually represents a 128-bit UUID value.

The full 128-bit value of a 16-bit or 32-bit UUID may be computed by a simple arithmetic operation.

128_bit_value=16_bit_value*2^96+Bluetooth_Base_UUID 

128_bit_value=32_bit_value*2^96+BIuetooth_Base_UUID

A 16-bit UUID may be converted to 32-bit UUID format by zero-extending the 16-bit value to 32-bits. An equivalent method is to add  the 16-bit UUID value to a zero-valued 32-bit UUID.

Note that two 16-bit UUIDs may be compared directly, as may two 32-bit UUIDs or two 128-bit UUID s. If two UUIDs of differing sizes are to be compared, the shorter UUID must be converted to the longer UUID fo rmat before comparison. 

在藍牙中,每個服務和服務屬性都唯一地由"全球唯一辨別符" (UUID)來校驗。正如它的名字所暗示的,每一個這樣的辨別符都要在時空上保證唯一。

UUID類可表現為短整形(16或32位)和長整形(128 位)UUID。

UUID值被固定配置設定在某個範圍,該範圍的第一個數值稱為藍牙UUID基數(Bluetooth_Base_UUID),其值為00000000—0000—1000—8000—00805F9B34FB。在此範圍中,UUID用一個 16位或32位的二進制數表示,經常被稱作16位或32位UUID。它的實際值代表一個 128位數。

128位的UUID值與16位或32位的UUID值之間的換算關系如下: 

128_bit_value=16_bit_value*2^96+Bluetooth_Base_UUID 

128_bit_value=32_bit_value*2^96+BIuetooth_Base_UUID

16位的UUID可以通過擴充16個0轉換成32位的UUID。如果兩個UUID值位數相同,則可以直接比較,如果位數不同,則需按照上述關系,将短UUID轉換成長UUID,位數相同後再比較。