在上一篇文章中,已经讲过了使用AMDeviceNotificationSubscribe函数来监听设备连接状态了

上一篇的连接:http://blog.csdn.net/u011740603/article/details/10337153

 

今天呢,则是讲一下打开设备并获取设备基本信息,如:设备名、设备ID、设备类型等。

主要涉及到的函数有:

(其中HANDLE hDevice应传入AMDeviceNotificationSubscribe监听到的结构体中的HANDLE)

int AMDeviceConnect(HANDLE hDevice);//执行正确则返回0

int AMDeviceIsPaired(HANDLE hDevice);//执行正确则返回1

int AMDeviceValidatePairing(HANDLE hDevice);//执行正确则返回0

int AMDeviceStartSession(HANDLE hDevice);//执行正确则返回0

int AMDeviceStopSession(HANDLE hDevice);

int AMDeviceDisconnect(HANDLE hDevice);

int AMDeviceIsPaired(HANDLE hDevice);

 

HANDLE __CFStringMakeConstantString(char* data);

HANDLE AMDeviceCopyValue(HANDLE hDevice,HANDLE cfstring1,HANDLE cfstring2);

BOOL CFStringGetCString(HANDLE cfstring,WCHAR* name,UINT len,UINT encode);

int CFGetTypeID(HANDLE copyRet);

int CFArrayGetTypeID();

int CFDictionaryGetTypeID();

int CFBooleanGetTypeID();

int CFNumberGetTypeID();

int CFStringGetTypeID();

int CFDataGetTypeID();

上面一系列是今天完成功能需要用到的iTunesMobileDevice.dll中的函数,看起来有点多,不过也没办法快哭了,必须遵循苹果提供的DLL中函数的调用规则。。。

上一篇文章中,监听的连接保存在ConnectStateInfomation结构体中,其中就有handle,这个数据及其重要,务必保存好!以后几乎所有的函数调用都需要它做参数,我给它的定义就是一个连接设备的句柄。

 

监听的设备连接之后,下一步的操作就是要打开设备连接,代码如下:

BOOL DeviceOpen()
{
    AMDeviceConnect(hDevice);
    AMDeviceIsPaired(hDevice);
    AMDeviceValidatePairing(hDevice);
    AMDeviceStartSession(hDevice);
    return TRUE;
}
其中,几个函数的返回值我就懒得判断了大笑,一般也不会执行出错,需要的童鞋请自行加入返回值判断代码。

这个就是打开设备的函数,一般的流程是,监听的设备之后,就立即执行此函数打开设备,之后就可以进行各种其他功能的操作了。

 

有打开,肯定就有关闭,代码如下:

BOOL DeviceClose()
{
    AMDeviceStopSession(hDevice);
    AMDeviceDisconnect(hDevice);
    return TRUE;
}

 这个就是关闭设备代码,一般是到程序结束的时候调用,或者重新打开设备的时候调用。

 

设备的打开和关闭现在都完成了,接下来就是要获取设备信息。

1、获取设备名称:

BOOL GetDeviceName(WCHAR* wcName)
{
	HANDLE handleResult;
	char ucData[MAX_PATH];
	strcpy(ucData,"DeviceName");
	HANDLE cfName = __CFStringMakeConstantString(ucData);
	handleResult = AMDeviceCopyValue(hDevice,NULL,cfName);
	if (!handleResult)
	{
		return FALSE;
	}
	WCHAR cNmae[256];
	UINT i;
	if (CFStringGetCString(handleResult,cNmae,256,256))
	{
		UINT pos = 0;
		long asciiWchar = 0;
		while(pos<256)
		{
			asciiWchar = cNmae[pos];
			if (asciiWchar==52224)
			{
				cNmae[pos] = '\0';
				break;
			}
			pos++;
		}
		wcscpy(wcName,cNmae);
	}
	return TRUE;
}

其中函数里面最后的字符串结束判断也偷懒了。。。

对了,请以后也不要问为什么这样调用iTunesMobileDevice.dll的函数。。因为这是iTunesMobileDevice.dll的规则,只有这样做才能达到想要的结果。。。快哭了

 

2、获取设备ID:

BOOL GetDeviceID(char* wcDeviceID)
{
	HANDLE handleResult;
	char ucData[256];
	unsigned char clenth;
	strcpy(ucData,"UniqueDeviceID");
	HANDLE cfName = __CFStringMakeConstantString(ucData);
	handleResult = AMDeviceCopyValue(hDevice,NULL,cfName);
	if (!handleResult)
	{
		return FALSE;
	}
	int ret = CFGetTypeID(handleResult);
	int retarray = CFArrayGetTypeID();
	int retdictionary = CFDictionaryGetTypeID();
	int retbool = CFBooleanGetTypeID();
	int retnumber = CFNumberGetTypeID();
	int retstring = CFStringGetTypeID();
	int retdata = CFDataGetTypeID();
	if (ret == retstring)
	{
		clenth = *(unsigned char *)handleResult;
		strcpy_s(wcDeviceID,clenth,(char*)((long)handleResult+9));
	}
	return TRUE;
}

这里要说一下,函数里面用了很多CF函数,其中ret应该是要和下面的每个ret值都要比较一下的,因为我知道会是哪种值,所以就偷懒了。。

 

3、获取设备类型

BOOL GetDeviceProductType(char* wcProductType)
{
	HANDLE handleResult;
	char ucData[256];
	unsigned char clenth;
	strcpy(ucData,"ProductType");
	HANDLE cfName = __CFStringMakeConstantString(ucData);
	handleResult = AMDeviceCopyValue(hDevice,NULL,cfName);
	if (!handleResult)
	{
		return FALSE;
	}
	int ret = CFGetTypeID(handleResult);
	int retarray = CFArrayGetTypeID();
	int retdictionary = CFDictionaryGetTypeID();
	int retbool = CFBooleanGetTypeID();
	int retnumber = CFNumberGetTypeID();
	int retstring = CFStringGetTypeID();
	int retdata = CFDataGetTypeID();
	if (ret == retstring)
	{
		clenth = *(unsigned char *)handleResult;
		strcpy_s(wcProductType,clenth,(char*)((long)handleResult+9));
	}
	return TRUE;
}

这个函数和上面类似。

 

到这里,设备的打开关闭和获取设备基本信息,就大功告成了!当然设备有很多很多基本信息,这里就不一一列举了!

 

由于本人编程水平有限,代码质量不高,欢迎拍砖!

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐