I have 4 distinct int values that I need to send to a BLE device (connection established OK).
I'll call the int values A,B,C,D for clarity. A and B range between 0-100, C has a range of 0-2000 and D has a range of 0-10000. All values are determined by user input.
I need to send these four values to the BLE device in quick succession, and package each of them differently: A and B (8 bits), C (16 bits) and D (32 bits). I'm unsure as to how to package the values correctly.
Below are three methods I've tried with varying degrees of success.
Convert int to data and send, e.g. for A (8 bit) int:
const unsigned char CHR = (float)A; float size = sizeof(CHR); NSData * aData = [NSData dataWithBytes:&CHR length:size]; [p writeValue:aData forCharacteristic:aCHAR type:CBCharacteristicWriteWithResponse];Convert to string first, e.g. for (16 bit) C:
NSString * cString = [NSString stringWithFormat:@"%i",C]; NSData * cData = [cString dataUsingEncoding:NSUTF16StringEncoding]; [p writeValue:cData forCharacteristic:cCHAR type:CBCharacteristicWriteWithResponse];Use uint, e.g. for (32 bit) D int:
uint32_t val = D; float size = sizeof(val); NSData * dData = [NSData dataWithBytes:(void*)&val length:size]; [p writeValue:valData forCharacteristic:dCHAR type:CBCharacteristicWriteWithResponse];
What am I doing wrong in the above, and how best to convert and send an int value to the device, allowing for the 3 formats required?