antonio__ antonio__ - 1 year ago 75
Objective-C Question

Sending few values at the same time over bluetooth

I am working on a Bluetooth based application and I am having problems when I try to send data from the iPhone to the other device.

I have no problem when I have to send just one value, using something like this:

- (void)sendData:(NSInteger)mel {
NSData *myData = [NSData dataWithBytes:&mel length:sizeof(mel)];
[self.myDevice writeValue:myData forCharacteristic:self.myCharacteristic type:CBCharacteristicWriteWithoutResponse];

But, for some characteristics I need send 2 or more values at the same time (for example in this case, variable mel and another one) but I haven’t been able yet to do it.

Does somebody know how to do this? Thanks in advance.


What I tried to send two values is

unsigned char bytes[] = {mel, interval};
NSMutableData *myData = [NSMutableData new];
[myData appendBytes:&bytes length:sizeof(bytes)];
[self.myDevice writeValue:myData forCharacteristic:self.myCharacteristic type:CBCharacteristicWriteWithoutResponse];

But this works like if the second value didn't exist

Answer Source

You can't use sizeof(bytes) to get the number of bytes in the array. It's simply going to return 4 since that is the size of a char *.

One options would be to use sizeof(mel) + sizeof(interval) instead of sizeof(bytes).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download