我正在尝试与一个连接的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每次的角色都不同。
以防你看不到它:

发布于 2019-02-13 11:18:10
UUID每次都会改变,因为它是唯一的!生成的每一个新的都是不同的。
至于奇怪的字符,uuid (以及type)到StringByReference的映射是这里的罪魁祸首,因为您没有以本机存储的格式获取数据。
C中的方法签名(您应该已经将其与问题一起发布)指出,uuid的类型是**char,这是一个指向8位C值字符串的指针。深入研究源代码,它们似乎是字符串表示中的数字0-9和A-F,以及32个字节(不带连字符)或36个字节(带)加上空终止符。(注意,这并不总是显而易见的;它们可以存储在16字节中作为完整的字节值,这实际上是API应该记录的内容。)
在内部,StringByReference类使用Pointer.getString()方法:
public String getValue() {
return getPointer().getString(0);
}只有一个偏移量的getString() method使用您的平台的缺省编码,它可能是多字节字符集。这可能与UUID的8位编码不匹配(在您的例子中显然也不匹配)。
您应该将UUID映射为PointerByReference,并使用uuid.getValue().getString(0, "UTF-8")或uuid.getValue().getString(0, "US-ASCII")将字符串提取为它们所表示的8位字符。
(或者,您也可以获取一个字节数组并从中创建一个字符串,尽管我不确定您是否会得到32字节或36字节的结果,因此,如果您这样做,请尽情享受。为了获得真正的乐趣,您可以迭代偏移量并逐字节读取,直到得到0。但我离题了。)
对type字段执行相同的操作留给读者作为练习。
https://stackoverflow.com/questions/54643725
复制相似问题