⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 btpair.cxx

📁 Windows CE操作系统中适用的蓝牙驱动程序
💻 CXX
📖 第 1 页 / 共 2 页
字号:
	}

	g_pState->Unlock ();

	FillAuthInfo (FALSE);

	SetWindowText (hWnd, L"Resolving Names");
	g_pState->Lock ();
	CHECK_STOP;

	g_pState->fState = DO_NAMERES;

	g_pState->Unlock ();

    // Reset iterator and beginn querying for names.
    DWORD dwUnused;
    iErr = BthNsLookupServiceNext (hLookup, BTHNS_LUP_RESET_ITERATOR, &dwUnused, NULL);

	g_pState->Lock ();
	CHECK_STOP;

	while ((iErr == ERROR_SUCCESS) && g_pState->hWnd && (g_pState->fState != DO_STOP)) {
		union {
			CHAR buf[5000];
			SOCKADDR_BTH	__unused;	// properly align buffer to BT_ADDR requirements
		};

		LPWSAQUERYSET pwsaResults = (LPWSAQUERYSET) buf;
		DWORD dwSize  = sizeof(buf);

		memset(pwsaResults,0,sizeof(WSAQUERYSET));
		pwsaResults->dwSize      = sizeof(WSAQUERYSET);
		pwsaResults->dwNameSpace = NS_BTH;
		pwsaResults->lpBlob      = NULL;

		g_pState->Unlock ();
		SVSUTIL_ASSERT(hLookup);

		iErr = BthNsLookupServiceNext (hLookup, LUP_RETURN_NAME | LUP_RETURN_ADDR, &dwSize, pwsaResults);

		g_pState->Lock ();

		if ((iErr == ERROR_SUCCESS) && g_pState->hWnd && (g_pState->fState != DO_STOP) &&
			pwsaResults->lpszServiceInstanceName && *(pwsaResults->lpszServiceInstanceName)) {
			InquiryResult *pRes = g_pState->pDev;
			while (pRes && pRes->b != ((SOCKADDR_BTH *)pwsaResults->lpcsaBuffer->RemoteAddr.lpSockaddr)->btAddr)
				pRes = pRes->pNext;

			if (pRes) {
				if (wcslen (pwsaResults->lpszServiceInstanceName) < (sizeof(pRes->szName)/sizeof(pRes->szName[0])))
					wcscpy (pRes->szName, pwsaResults->lpszServiceInstanceName);

				WCHAR szTitle[300];
				if (pRes->szName[0])
					wsprintf (szTitle, L"%s (%04x%08x)", pRes->szName, GET_NAP(pRes->b), GET_SAP(pRes->b));
				else
					wsprintf (szTitle, L"%04x%08x", GET_NAP(pRes->b), GET_SAP(pRes->b));

				g_pState->Unlock ();

				LV_FINDINFO	lfi;
				memset (&lfi, 0, sizeof(lfi));
				lfi.flags = LVFI_PARAM;
				lfi.lParam = (LPARAM)pRes;

				int iData = ListView_FindItem (hWndDevList, 0, &lfi);
				if (iData >= 0) {
					ListView_SetItemText (hWndDevList, iData, 0, szTitle);
					ListView_SetColumnWidth(hWndDevList, 0, LVSCW_AUTOSIZE);
				}

				g_pState->Lock ();
			}
		}
	}

	BthNsLookupServiceEnd(hLookup);

	g_pState->fState = DO_NOTHING;
	g_pState->Unlock ();

	SetWindowText (hWndButton, L"Inquiry");
	SetWindowText (hWnd, L"Bluetooth Pairing");
	return 0;
}

static BOOL CALLBACK DlgProc2 (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
#if defined (SDK_BUILD)
	if (gpSHInputDialog)
		gpSHInputDialog (hWnd, uMsg, wParam);
#endif

	switch (uMsg) {
	case WM_INITDIALOG:
		SetWindowText (GetDlgItem (hWnd, IDC_PIN), L"");
		SetFocus (GetDlgItem (hWnd, IDC_PIN));

		return 0;

	case WM_COMMAND:
		{
			int wID = LOWORD(wParam);
			switch (wID)
			{
			case IDOK:
				{
					WCHAR szPIN[64];
					GetWindowText (GetDlgItem (hWnd, IDC_PIN), szPIN, 64);
					EndDialog (hWnd, (int)_wcsdup (szPIN));
				}
				return 0;

			case IDCANCEL:
				EndDialog (hWnd, 0);
				return 0;
			}
		}
		break;
	}

	return 0;
}

static void Pair (void) {
	g_pState->Lock ();
	HWND hWnd = g_pState->hWnd;
	HINSTANCE hInst = g_pState->hInst;
	g_pState->Unlock ();

	HWND hWndDevList   = GetDlgItem (hWnd, IDC_DEVICELIST);

	int iNdx = ListView_GetNextItem (hWndDevList, -1, LVNI_SELECTED);

	if (iNdx < 0) {
		MessageBox (hWnd, L"Nothing selected", L"Error", MB_OK | MB_TOPMOST);
		return;
	}

	LV_ITEM lvi;
	memset (&lvi, 0, sizeof(lvi));
	lvi.mask = LVIF_PARAM;
	lvi.iItem = iNdx;

	ListView_GetItem(hWndDevList, &lvi);

	LPARAM lpItemData = (LPARAM)lvi.lParam;

	g_pState->Lock ();
	InquiryResult *pRes = g_pState->pDev;
	while (pRes && (pRes != (InquiryResult *)lpItemData))
		pRes = pRes->pNext;

	if (! pRes) {
		g_pState->Unlock ();
		MessageBox (hWnd, L"Please requery. This record is obsolete.", L"Error", MB_OK | MB_TOPMOST);
		return;
	}


	BT_ADDR b = pRes->b;
	WCHAR	*pszPIN = NULL;

	if (g_pState->fState != DO_NOTHING) {
		g_pState->fState = DO_STOP;

		SetWindowText (hWnd, L"Stopping...");
	}

	g_pState->Unlock ();

	pszPIN = (WCHAR *)DialogBox (hInst, MAKEINTRESOURCE (IDD_ENTERPIN), NULL, DlgProc2);

	if (! pszPIN)
		return;

	unsigned char pin[16];
	int cPin = 0;

	while ((*pszPIN) && (cPin < 16))
		pin[cPin++] = (unsigned char)*(pszPIN++);

	// Set PIN first
	BthSetPIN (&b, cPin, pin);

	for (int i = 0 ; (i < 10) && (g_pState->fState != DO_NOTHING); ++i)
		Sleep (1000);

	int iRes = ERROR_SUCCESS;
	unsigned short h = 0;
	
	if (ERROR_SUCCESS == BthCreateACLConnection (&b, &h)) {
		iRes = BthAuthenticate (&b);

		BthCloseConnection (h);
	}
	else {
		iRes = GetLastError ();
		MessageBox (hWnd, L"Connection failed", L"Error", MB_OK | MB_TOPMOST);
	}

	if (iRes == ERROR_SUCCESS)
		MessageBox (hWnd, L"Pairing succeeded!", L"Success", MB_OK | MB_TOPMOST);
	else
		MessageBox (hWnd, L"Pairing failed!", L"Error", MB_OK | MB_TOPMOST);

	FillAuthInfo (FALSE);
}

static void Unpair (void) {
	g_pState->Lock ();
	HWND hWnd = g_pState->hWnd;
	HINSTANCE hInst = g_pState->hInst;
	g_pState->Unlock ();

	HWND hWndDevList   = GetDlgItem (hWnd, IDC_DEVICELIST);

	int iNdx = ListView_GetNextItem (hWndDevList, -1, LVNI_SELECTED);

	if (iNdx < 0) {
		MessageBox (hWnd, L"Nothing selected", L"Error", MB_OK | MB_TOPMOST);
		return;
	}

	LV_ITEM lvi;
	memset (&lvi, 0, sizeof(lvi));
	lvi.mask = LVIF_PARAM;
	lvi.iItem = iNdx;

	ListView_GetItem(hWndDevList, &lvi);

	LPARAM lpItemData = (LPARAM)lvi.lParam;

	g_pState->Lock ();
	InquiryResult *pRes = g_pState->pDev;

	while (pRes && (pRes != (InquiryResult *)lpItemData))
		pRes = pRes->pNext;

	if (! pRes) {
		g_pState->Unlock ();
		MessageBox (hWnd, L"Please requery. This record is obsolete.", L"Error", MB_OK | MB_TOPMOST);
		return;
	}


	BT_ADDR b = pRes->b;

	g_pState->Unlock ();

	BthRevokePIN (&b);
	BthRevokeLinkKey (&b);

	FillAuthInfo (FALSE);
}

static HIMAGELIST LoadIcons(HINSTANCE hInst) {
	HIMAGELIST	hIconList = ImageList_Create(16, 16, ILC_COLOR, 0, 5);
	HICON hIcon;
	hIcon = (HICON)LoadImage(hInst, MAKEINTRESOURCE(IDI_TRUSTED), IMAGE_ICON, 16, 16, 0);		
	if( hIcon ) {
		ImageList_AddIcon(hIconList, hIcon);
    		DestroyIcon(hIcon);
	}

	hIcon = (HICON)LoadImage(hInst, MAKEINTRESOURCE(IDI_UNTRUSTED), IMAGE_ICON, 16, 16, 0);	
	if( hIcon ) {
   		ImageList_AddIcon(hIconList, hIcon);
	    	DestroyIcon(hIcon);
	}

	hIcon = (HICON)LoadImage(hInst, MAKEINTRESOURCE(IDI_EMPTY), IMAGE_ICON, 16, 16, 0);	
	if( hIcon ) {
   		ImageList_AddIcon(hIconList, hIcon);
	    	DestroyIcon(hIcon);
	}

	return hIconList;
}

static BOOL CALLBACK DlgProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	switch (uMsg) {
	case WM_INITDIALOG:
		{
		g_pState->hWnd = hWnd;
		CloseHandle (CreateThread (NULL, 0, FillAuthInfo, (LPVOID)TRUE, 0, NULL));
		HWND hWndDevList = GetDlgItem (hWnd, IDC_DEVICELIST);
		HIMAGELIST hIconList = LoadIcons(g_pState->hInst);
		ListView_SetImageList(hWndDevList, hIconList, LVSIL_SMALL);

		LV_COLUMN lvc;
	
		lvc.mask = LVCF_FMT | LVCF_SUBITEM;
		lvc.iSubItem = 0;
		lvc.fmt = LVCFMT_IMAGE ;
	
		RECT rect;
		GetWindowRect(hWndDevList, &rect);
		lvc.cx = 0;		

		ListView_InsertColumn(hWndDevList, 0, &lvc);
		}
		return 0;

	case WM_COMMAND:
		{
			int wID = LOWORD(wParam);
			switch (wID)
			{
			case IDOK:
				Pair ();
				return 0;

			case IDC_UNPAIR:
				Unpair ();
				return 0;

			case IDCANCEL:
				EndDialog (hWnd, TRUE);
				return 0;

			case IDC_INQUIRY:	// Inquiry
				CloseHandle (CreateThread (NULL, 0, DoInquiry, NULL, 0, NULL));
				break;
			}
		}
		break;
	}

	return 0;
}

int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPWSTR lpszCmdLine, int nCmdShow) {
#if defined (SDK_BUILD)
	HMODULE hMod = LoadLibrary (L"aygshell.dll");
	if (hMod)
		gpSHInputDialog = (tSHInputDialog)GetProcAddress (hMod, L"SHInputDialog");
#endif

	InitCommonControls ();

	g_pState = new Global;

	if (! g_pState)
		return 0;

	g_pState->hInst = hInst;

	DialogBox (g_pState->hInst, MAKEINTRESOURCE (IDD_MAINBOX), NULL, DlgProc);

#if defined (SDK_BUILD)
	gpSHInputDialog = NULL;
	FreeLibrary (hMod);
#endif

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -