When we find the characteristic we need, the most we want to do is look at what the value of this characteristic is. In other words, we need an operation to receive the data.
-(void) Peripheral: (cbperipheral *) Peripheral Diddiscovercharacteristicsforservice: (Cbservice *) Service error: ( Nserror *) Error
In Corebluetooth, two methods are available:
1. [Peripheral readvalueforcharacteristic:characteristic];
Peripheral is the peripheral in our program, characteristic is the one we're interested in characteristic when this method is called, the program will callback a method:
-(void) Peripheral: (cbperipheral *) Peripheral didupdatevalueforcharacteristic: (cbcharacteristic *) characteristic Error: (NSERROR *) error
The second parameter above: (cbcharacteristic *) characteristic----- We obtain the value of the characteristic of interest directly with Characteristic.value, the type is NSData, and then we can manipulate the value.
PS: The premise of the callback is to set up the agent, do not forget. This method is more straightforward and conforms to our general logic, and the second method is described below
2. [Peripheral Setnotifyvalue:yes forcharacteristic:characteristic]; subscribe "Subscription"
The first parameter is set to Yes, and as for why, I'm talking about my understanding. The second parameter is also a characteristic of interest to us.
After calling this method, the program will callback a method:
What can this method do? I think the main is some error judgment, the use of the third parameter above error, if the error, the system put the error message, you can print the display.
calling [Peripheral readvalueforcharacteristic:characteristic] Later in this method will also callback the callback method in 1,
-(void) Peripheral: (cbperipheral *) Peripheral didupdatevalueforcharacteristic: ( Cbcharacteristic *) Characteristic error: (Nserror *) Error
We also work on the data here, as in 1.
compared to 1, one more step, one more parameter notifyvalue, this parameter I feel is a sign, when yes, I want to subscribe to this characteristic, and then whenever the value of this characteristic change, Will callback
-(void) Peripheral: (cbperipheral *) Peripheral didupdatevalueforcharacteristic: (cbcharacteristic *) characteristic Error: (NSERROR *) error
It's like I'm going to subscribe to this magazine (characteristic), and whenever a new version of the magazine is released (value changed), you're going to send me a copy (callback method), why send it to me? Because I subscribed.
So, this method is more efficient in some cases than 1, that is, the value of the characteristic that we need to manipulate is often changed. Such as the heart rate of these devices, heart rate characteristic value.
Finally, there is an important addition, when we have found that we are interested in the characteristic, his value is not necessarily through the 1 method or 2 method can be received, even if can receive, sometimes can only use 1, sometimes can only use 2.
This involves a property of characteristic:
typedef enum
{
Cbcharacteristicpropertybroadcast = 0x01,
Cbcharacteristicpropertyread = 0x02,
Cbcharacteristicpropertywritewithoutresponse = 0x04,
Cbcharacteristicpropertywrite = 0x08,
Cbcharacteristicpropertynotify = 0x10,
Cbcharacteristicpropertyindicate = 0x20,
Cbcharacteristicpropertyauthenticatedsignedwrites = 0x40,
Cbcharacteristicpropertyextendedproperties = 0x80,
cbcharacteristicpropertynotifyencryptionrequired = 0x100,
cbcharacteristicpropertyindicateencryptionrequired = 0x200,
} cbcharacteristicproperties;
When read (0x02), we use 1 methods to view, with 2 will be error. When it comes to notify (0x10) we have to use the 2 method. Others don't say, like write can only write value
So when we want to look at value, let's look at the property of this characteristic to see if we can read it.
How to view? Find the device manufacturer for your BLE peripherals or view the instructions for your peripherals.
"<cbcharacteristic:0x1565ee50, UUID = System ID, properties = 0x2, value = (null), notifying = No> ",
"<cbcharacteristic:0x156806b0, UUID = Model number String, properties = 0x2, value = (null), notify ing = no> ",
"<cbcharacteristic:0x1567f9e0, UUID = Serial number String, properties = 0x2, value = (null), Noti fying = no> ",
"<cbcharacteristic:0x1565a640, UUID = Firmware Revision String, properties = 0x2, value = (null), not ifying = no> ",
"<cbcharacteristic:0x15682f70, UUID = Hardware Revision String, properties = 0x2, value = (null), not ifying = no> ",
"<cbcharacteristic:0x1564bdf0, UUID = Software Revision String, properties = 0x2, value = (null), n otifying = no> ",
"<CBCHARACTERISTIC:0X1565A8C0, UUID = Manufacturer Name String, properties = 0x2, value = (null), no tifying = no> ",
"<cbcharacteristic:0x15683cd0, UUID = IEEE Regulatory certification, properties = 0x2, value = (null), notifying = No> ",
"<cbcharacteristic:0x15683bf0, UUID = PnP ID, properties = 0x2, value = (null), Notifyin g = no> "
It is recommended to set the property of characteristic, which is constantly changing value, to notify (Cbcharacteristicpropertynotify).
IOS Learning Note III (2015.03.05)