小尾数字节顺序(iOS,BLE扫描响应) [英] Little-Endian Byte Order (iOS, BLE scan response)

查看:145
本文介绍了小尾数字节顺序(iOS,BLE扫描响应)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本而言,我正在将nativescript用于跨平台应用程序。该应用程序与BLE设备进行交互。 BLE设备在广告包之后发送扫描响应,我可以在iOS端将其作为NSData对象进行检索。

In basic terms, I am using nativescript for a cross platform application. This application interacts with BLE devices. The BLE devices send a scan response after the advertisement packet, which I am able to retrieve as an NSData object on the iOS side.

这是来自的(伪)输出description属性:< 54fca267 0b00>。

Here is the (pseudo) output from the description property: <54fca267 0b00>.

输出代表外围设备的硬件地址。

The output represents the hardware address of the peripheral.

硬件人员告诉我,这几乎没有-endian,我知道它是一个48位字符串。

The hardware guys are telling me this is in little-endian, and I know it is a 48-bit string. It is coming from the peripheral, which has its firmware written in C.

我已经尝试了几天,将其转换为字符串(具有正确的字节序)。这样我就可以将硬件地址存储在后端,但是无法提出一种优雅的解决方案。

I have been trying for a few days to convert this into a string (with the correct endianness) so that I can store the hardware address on our back-end, but am unable to come up with an elegant solution.

我想出的一个解决方案是读取NSData对象的:description属性,然后手动(使用javascript)对其进行解码。我只是觉得这种解决方案不适用于生产环境,而是想使用本机实用程序。

One solution I did come up with was reading the :description property of the NSData object, and then manually (in javascript) 'decoding' it. I just do not feel like this solution is proper enough for a production environment, and would like to use native utilities instead.

我尝试使用:

NSString.alloc().initWithDataEncoding(mac, NSUTF8StringEncoding);

..每种编码类型均来自此页面页。

..with every encoding type from THIS page of the documentation. I am either getting nil in return, or incorrect characters.

就像我之前说过的那样,这是用NativeScript编写的。但是,我对目标C有一个不错的了解,因此我应该能够转换以该语言提供的任何示例。

Like I previously stated, this is being written in NativeScript; however, I have a decent understanding of objective C, so I should be able to convert any examples provided in that language.

任何输入将不胜感激。

Any input would be greatly appreciated. Thanks in advance!

推荐答案

CRD的答案是正确的,但我想这不是您要的内容。您想要一个看起来像 54:fc:a2:67:0b:00(蓝牙/ MAC地址)的结果,对吗?这是一种简单的蛮力方法(您可以循环执行类似操作,但不值得IMO)。

CRD's answer is on the right track, but I'm guessing is not what you're asking for. You want a result that looks like "54:fc:a2:67:0b:00" (a Bluetooth/MAC address), right? Here's a simple, brute force approach (you could do this in a loop or the like, but not worth it IMO).

// Just for testing; you already have data.
uint8_t address[6] = { 0x54, 0xfc, 0xa2, 0x67, 0x0b, 0x00 };
NSData *data = [NSData dataWithBytes:address length:6];

// Check the length before proceeding (maybe return an error rather than crash :D)
assert([data length] == 6);

// Grab the bytes
const uint8_t *bytes = data.bytes;

// Format them
NSString *addressString = [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x",
                           bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5]];

// 3. Profit
NSLog(@"%@", addressString);

但是....

I我不确定工程师在这里所说的小尾数是什么意思。 54:fc:a2 不是分配的制造商代码。通常,我会假设 little-endian表示它们已经将16位字(我在CSR平台上遇到过)进行了字节交换,因此应为 fc:54:67 ,但这两者均未分配。完全将整个东西视为 倒向我们 00:0b:67 分配给了制造蓝牙摄像头的TopView,因此我敢打赌这就是小端的含义(这很有意义,因为在线路上写入MAC地址的正常方法恰好相反。并且是 big-endian。)

I'm not quite certain what the engineer means here by "little-endian." 54:fc:a2 isn't an assigned manufacturer code. Typically, I would assume "little-endian" means that they've byte-swapped the 16-bit words (which I've run into on CSR platforms), and so this should be fc:54:67, but that isn't assigned either. Treating the whole thing as completely backwards gives us 00:0b:67 which is assigned to TopView, who makes Bluetooth cameras, so I'm betting that's what "little-endian" means here (which makes sense, because the normal way to write MAC addresses on the wire is exactly backwards of that, and is "big-endian").

因此这会将代码更改为:

So that would change the code to:

NSString *addressString = [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x",
                           bytes[5], bytes[4], bytes[3], bytes[2], bytes[1], bytes[0]];

地址是00:b0:67:a2:fc:54吗?

Is the address 00:b0:67:a2:fc:54?

这篇关于小尾数字节顺序(iOS,BLE扫描响应)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆