I am fairly new to programming and I am trying to develop a bluetooth fitness device using CoreBluetooth for iOS. I bought a bunch of different devices from different hardware manufacturers for testing and one of the instructions for sending values to the device
我对编程相当陌生,我正在尝试开发一个蓝牙健身设备,使用CoreBluetooth来进行iOS操作。我从不同的硬件制造商那里购买了一堆不同的设备进行测试,其中一个是向设备发送值的指令。
I want to be able to write a value (fitness goal) to the Bluetooth fitness device. I know how to read/write with Bluetooth.
我希望能够为蓝牙健身设备写一个价值(健身目标)。我知道如何用蓝牙读写。
I just don't know how to convert the values I want to uint8_t and uint16_t and then combine it into an 8 byte NSMutableData.
我只是不知道如何将我想要的值转换为uint8_t和uint16_t,然后将它合并成一个8字节的NSMutableData。
Can anyone tell me how I would do this?
有人能告诉我怎么做吗?
1 个解决方案
#1
8
The first thing to worry about is byte ordering. It doesn't matter for the uint8_t
but it matters for anything larger. You can have big-endian or little-endian. Check the docs for device.
首先要考虑的是字节顺序。对于uint8_t来说这并不重要,但对于任何更大的东西来说都很重要。你可以有大的或小的。检查设备的文档。
Start with your values:
开始你的价值观:
uint8_t days = 12;
uint16_t walked = 225;
uint16_t ran = 110
uint16_t steps = 750;
uint8_t users = 2;
NSMutableData *data = [NSMutableData data];
[data appendBytes:&days length:1];
uint16_t network = CFSwapInt16HostToBig(walked);
[data appendBytes:&network length:2];
network = CFSwapInt16HostToBig(ran);
[data appendBytes:&network length:2];
network = CFSwapInt16HostToBig(steps);
[data appendBytes:&network length:2];
[data appendBytes:&users length:1];
The above assumes you need the data in big-endian format. If it need to be little-endian then change CFSwapInt16HostToBig
to CFSwapInt16HostToLittle
.
以上假设您需要使用big-endian格式的数据。如果它需要是little-endian,那么将CFSwapInt16HostToBig转换为CFSwapInt16HostToLittle。
#1
8
The first thing to worry about is byte ordering. It doesn't matter for the uint8_t
but it matters for anything larger. You can have big-endian or little-endian. Check the docs for device.
首先要考虑的是字节顺序。对于uint8_t来说这并不重要,但对于任何更大的东西来说都很重要。你可以有大的或小的。检查设备的文档。
Start with your values:
开始你的价值观:
uint8_t days = 12;
uint16_t walked = 225;
uint16_t ran = 110
uint16_t steps = 750;
uint8_t users = 2;
NSMutableData *data = [NSMutableData data];
[data appendBytes:&days length:1];
uint16_t network = CFSwapInt16HostToBig(walked);
[data appendBytes:&network length:2];
network = CFSwapInt16HostToBig(ran);
[data appendBytes:&network length:2];
network = CFSwapInt16HostToBig(steps);
[data appendBytes:&network length:2];
[data appendBytes:&users length:1];
The above assumes you need the data in big-endian format. If it need to be little-endian then change CFSwapInt16HostToBig
to CFSwapInt16HostToLittle
.
以上假设您需要使用big-endian格式的数据。如果它需要是little-endian,那么将CFSwapInt16HostToBig转换为CFSwapInt16HostToLittle。