Differences between CBUUID and NSUUID in swift

爱⌒轻易说出口 提交于 2019-12-10 12:45:57

问题


Where are the differences between the CBUUID and the NSUUID object?

I have seen to fill the scanForPeripheralsWithServices with both objects in different cases... Apple expect an CBUUID object so I would prefer to use this type or?

My example:

let MY_SERVICE_UUID = CBUUID(string: "hdl83h6sd-gl95-bn4f-37gd-jd73hd0tn8za") func scanForPeripheralsWithServices(serviceUUIDs: MY_SERVICE_UUID,nil)


回答1:


An NSUUID is a 128 bit number that is used to uniquely identify objects, types and other items. It can be accessed as bytes or is commonly used in a string form - 68753A44-4D6F-1226-9C60-0050E4C00067. The class includes methods for creating both random UUIDs and instances initialised with a specialised value.

Bluetooth also uses 128 bit identifiers for characteristics and services. The Bluetooth SIG has defined many "well known" services and characteristics. These are represented as a 16 bit value which is combined with the Bluetooth base UUID to get the full 128 bit value.

The CBUUID methods are aware of these well known values and can automatically transform the 16 bit values into their 128 bit equivalent. A CBUUID can also be initialised with a 128 bit value if you aren't using well known values (I.e. You have created your own private services and characteristics)

For example initialising a CBUUID with CBUUID(string:"180F") is ok (this is the battery service) but NSUUID(string:"180F") would fail because it needs all 128 bits.

When working with Core Bluetooth you will use CBUUID for services and characteristics. An NSUUID is used for the peripheral identifier since that is just a MAC with no special values defined.



来源:https://stackoverflow.com/questions/32749194/differences-between-cbuuid-and-nsuuid-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!