通过蓝牙同时发送少量值

时间:2016-09-13 18:16:18

标签: ios objective-c bluetooth

我正在开发基于蓝牙的应用程序,当我尝试将数据从iPhone发送到其他设备时,我遇到了问题。

当我必须使用以下内容发送一个值时,我没有问题:

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

但是,对于某些特性,我需要同时发送2个或更多值(例如,在这种情况下,变量mel和另一个值),但我还没有做到。

有人知道怎么做吗?提前谢谢。

更新1

我尝试发送两个值的是

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

但这就像第二个值不存在一样

1 个答案:

答案 0 :(得分:0)

您无法使用sizeof(bytes)来获取数组中的字节数。它只是返回4,因为它的大小为char *

一种选择是使用sizeof(mel) + sizeof(interval)代替sizeof(bytes)