我正在尝试与已连接的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
每次角色都不同。
万一你看不到它:
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
字段做同样的事情留给读者练习。