I am using the following code to write the 0xDE value for a Bluetooth Caracteristic (Reset Device) using the IOS Core Bluetooth :
...
NSData *bytes = [@\"0xDE\"
The answer by bensarz is almost correct. Except one thing: you shouldn't use sizeof(int)
as the length for NSData
. The size of int
is 4 or 8 bytes (depending on the architecture). As you want to send 1 byte, use uint8_t
or Byte
instead:
uint8_t byteToWrite = 0xDE;
NSData *data = [[NSData alloc] initWithBytes:&byteToWrite length:sizeof(&byteToWrite)];
[peripheral writeValue:data
forCharacteristic:characteristic
type:CBCharacteristicWriteWithResponse];
Of courser you could also use int
as the variable's type, but you have to initialize NSData
with the length of 1.