0

我正在尝试与连接的 iOS 设备配对并使用 libimobiledevice 和 JNA 获取 UDID。这就是我声明本机函数的方式:

static native int idevice_new(PointerByReference device, Pointer udid);
static native int lockdownd_client_new(Pointer device, PointerByReference client, String label);
static native int idevice_get_udid(Pointer idevice, StringByReference udid);
static native int lockdownd_query_type(Pointer lockdownd_client, StringByReference type);

对于测试,我正在尝试完全执行运行命令的idevicepair pair操作。

这是我的主要方法:

PointerByReference device = new PointerByReference();
System.out.println("idevice_new error code: " + idevice_new(device, Pointer.NULL));
PointerByReference client = new PointerByReference();
System.out.println("lockdownd_client_new error code: " + lockdownd_client_new(device.getValue(), client, "java"));
StringByReference udid = new StringByReference();
System.out.println("idevice_get_udid error code: " + idevice_get_udid(device.getValue(), udid));
System.out.println("udid: " + udid.getValue());
StringByReference type = new StringByReference();
System.out.println("lockdownd_query_type error code: " + lockdownd_query_type(client.getValue(), type));
System.out.println("lockdownd_query_type: " + type.getValue());
System.out.println("lockdownd_pair error code: " + lockdownd_pair(client.getValue(), Pointer.NULL));

每当我尝试获取任何字符串值时,它都会输出这些奇怪的问号字符:

idevice_new error code: 0
lockdownd_client_new error code: 0
idevice_get_udid error code: 0
udid: ��AZ�
lockdownd_query_type error code: 0
lockdownd_query_type: �HbZ�
lockdownd_pair error code: 0

每次的角色都不一样。

如果你看不到它:

程序输出

4

1 回答 1

3

UUID 每次都会更改,因为它是唯一的!每个新生成的都是不同的。

至于奇怪的字符,您将uuid(和type)映射到StringByReference是这里的罪魁祸首,因为您没有以本机存储的格式获取数据。

C 中的方法签名(您应该在您的问题中发布)指出类型uuid**char,一个指向 8 位 C 值字符串的指针的指针。深入研究源代码,它们似乎是字符串表示中的数字 0-9 和 AF,以及 32 个字节(不带连字符)或 36 个字节(带)加上空终止符。(请注意,这并不总是显而易见的;它们可以作为完整字节值存储在 16 个字节中,API 应该实际记录这些内容。)

在内部,StringByReference该类使用以下Pointer.getString()方法:

public String getValue() {
    return getPointer().getString(0);
}

只有一个偏移量的getString() 方法使用您平台的默认编码,这可能是一个多字节字符集。这可能与 UUID 的 8 位编码不匹配(在您的情况下显然不匹配)。

您应该将 UUID 映射为 aPointerByReference并使用uuid.getValue().getString(0, "UTF-8")uuid.getValue().getString(0, "US-ASCII")获取字符串作为它们所代表的 8 位字符。

(或者,你可以得到一个字节数组并从中创建一个字符串,虽然我不确定你是否会得到 32 字节或 36 字节的结果,所以如果你走那条路就玩得开心。为了真正的乐趣,你可以迭代偏移量并逐字节读取,直到得到 0。但我离题了。)

为该type领域做同样的事情留给读者作为练习。

于 2019-02-13T03:18:09.887 回答