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

📄 btdialer.cxx

📁 Windows CE操作系统中适用的蓝牙驱动程序
💻 CXX
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
/**
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.


Abstract:
	Windows CE Bluetooth application sample

**/
#include <windows.h>
#include <stdio.h>
#include <bt_api.h>

#include "resource.h"

#include "../btenum/btenum.hxx"

#define DIAL_STRING_SIZE	32

WCHAR gszNumber[DIAL_STRING_SIZE];

static void DisplayNumber (HWND hWnd) {
	int c = wcslen (gszNumber);
	if ((c > 3) && (c <= 7)) { // 123-4567
		WCHAR szString[32];
		szString[0] = gszNumber[0];
		szString[1] = gszNumber[1];
		szString[2] = gszNumber[2];
		szString[3] = L'-';
		wcscpy (szString + 4, gszNumber + 3);
		SetWindowText (GetDlgItem(hWnd, IDC_NUM), szString);
	} else if ((c > 7) && (c <= 10)) { // (123) 456-7890
		WCHAR szString[32];
		szString[0] = L'(';
		szString[1] = gszNumber[0];
		szString[2] = gszNumber[1];
		szString[3] = gszNumber[2];
		szString[4] = L')';
		szString[5] = L' ';
		szString[6] = gszNumber[3];
		szString[7] = gszNumber[4];
		szString[8] = gszNumber[5];
		szString[9] = L'-';
		wcscpy (szString + 10, gszNumber + 6);
		SetWindowText (GetDlgItem(hWnd, IDC_NUM), szString);
	} else if ((c == 11) && (gszNumber[0] == '1')) { // 1 (234) 567-8901
		WCHAR szString[32];
		szString[0] = gszNumber[0];
		szString[1] = L' ';
		szString[2] = L'(';
		szString[3] = gszNumber[1];
		szString[4] = gszNumber[2];
		szString[5] = gszNumber[3];
		szString[6] = L')';
		szString[7] = L' ';
		szString[8] = gszNumber[4];
		szString[9] = gszNumber[5];
		szString[10] = gszNumber[6];
		szString[11] = L'-';
		wcscpy (szString + 12, gszNumber + 7);
		SetWindowText (GetDlgItem(hWnd, IDC_NUM), szString);
	} else 
		SetWindowText (GetDlgItem(hWnd, IDC_NUM), gszNumber);
}

static void AddDigit (HWND hWnd, WCHAR *sz) {
	int c = wcslen (gszNumber);
	if (c >= DIAL_STRING_SIZE-1)
		return;

	gszNumber[c] = *sz;
	gszNumber[c+1] = L'\0';
	DisplayNumber (hWnd);
}

static BOOL CALLBACK AboutDlg (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	switch (uMsg) {
	case WM_INITDIALOG:
		return 0;
	}

	return 0;
}

static BOOL CALLBACK DialerDlgProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	switch (uMsg) {
	case WM_INITDIALOG:
		{
			HFONT hFont = NULL;

			LOGFONT logFont;

			memset (&logFont, 0, sizeof(logFont));

			wcscpy (logFont.lfFaceName, L"Courier New");
			logFont.lfHeight = 16;

			HDC hDC = GetDC(hWnd);
			logFont.lfHeight = -(logFont.lfHeight * GetDeviceCaps(hDC, LOGPIXELSY) + 36) / 72;
			ReleaseDC(hWnd, hDC);

			logFont.lfPitchAndFamily = FIXED_PITCH;
			logFont.lfCharSet = OEM_CHARSET;


			hFont = CreateFontIndirect(&logFont);

			SetWindowText (GetDlgItem(hWnd, IDC_NUM), L"");
			SetForegroundWindow (hWnd);

			if (hFont)
				SendMessage (GetDlgItem (hWnd, IDC_NUM), WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));

			gszNumber[0] = '\0';
		}

		return 0;

	case WM_CHAR:
		{
			char ch = (char)wParam;
			if ((ch >= '0') && (ch <= '9')) {
				static int KeyClick[10] = {
					IDC_DIG_0,
					IDC_DIG_1,
					IDC_DIG_2,
					IDC_DIG_3,
					IDC_DIG_4,
					IDC_DIG_5,
					IDC_DIG_6,
					IDC_DIG_7,
					IDC_DIG_8,
					IDC_DIG_9
				};

				HWND h = GetDlgItem (hWnd, KeyClick[ch - '0']);
				SendMessage (h, BM_CLICK, 0, 0);
				SetFocus (hWnd);
			} else if (ch == '\r') {
				HWND h = GetDlgItem (hWnd, IDC_DIAL);
				SendMessage (h, BM_CLICK, 0, 0);
				SetFocus (hWnd);
			} else if (ch == '\b') {
				HWND h = GetDlgItem (hWnd, IDC_DIG_C);
				SendMessage (h, BM_CLICK, 0, 0);
				SetFocus (hWnd);
			} else if (ch == 27) {
				EndDialog (hWnd, (int)NULL);
			}

			return 0;
		}

	case WM_COMMAND:
		{
			int wID = LOWORD(wParam);
			switch (wID)
			{
			case IDC_DIAL:
				if (gszNumber[0] != L'\0')
					EndDialog (hWnd, (int)gszNumber);
				return 0;

			case IDCANCEL:
				EndDialog (hWnd, (int)NULL);
				return 0;

			case IDC_DIG_1:
				AddDigit (hWnd, L"1");
				return 0;

			case IDC_DIG_2:
				AddDigit (hWnd, L"2");
				return 0;

			case IDC_DIG_3:
				AddDigit (hWnd, L"3");
				return 0;

			case IDC_DIG_4:
				AddDigit (hWnd, L"4");
				return 0;

			case IDC_DIG_5:
				AddDigit (hWnd, L"5");
				return 0;

			case IDC_DIG_6:
				AddDigit (hWnd, L"6");
				return 0;

			case IDC_DIG_7:
				AddDigit (hWnd, L"7");
				return 0;

			case IDC_DIG_8:
				AddDigit (hWnd, L"8");
				return 0;

			case IDC_DIG_9:
				AddDigit (hWnd, L"9");
				return 0;

			case IDC_DIG_0:
				AddDigit (hWnd, L"0");
				return 0;

			case IDC_DIG_C:
				{
					int c = wcslen(gszNumber);
					if (c > 0) {
						--c;
						gszNumber[c] = '\0';
						DisplayNumber (hWnd);
					}
				return 0;
				}
			}
		}
		break;
	}

	return 0;
}

static WCHAR gszPortName[_MAX_PATH];

static int EnumCallback (void *pContext, BTDEV *pb) {
	if ((pb->iDeviceClass == BTENUM_DEVICE_MODEM) && pb->fActive)
		wcsncpy (gszPortName, pb->szPortName, _MAX_PATH);

	return TRUE;
}

int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPWSTR lpszCmdLine, int nCmdShow) {

	gszPortName[0] = '\0';
	BthEnumDevices (NULL, EnumCallback);

	if (! gszPortName[0]) {
		MessageBox (NULL, L"Bluetooth phone is not configured. Please use Bluetooth control panel to pair to phone (use dial-up networking profile)", L"Phone not found", MB_OK | MB_TOPMOST);
		return 0;
	}

	WCHAR *lpszPhone = lpszCmdLine;
	while ((*lpszPhone != '\0') && (iswspace (*lpszPhone)))
		++lpszPhone;

	WCHAR szPhoneBuffer[DIAL_STRING_SIZE];

	if (lpszPhone[0] == '\0')
		lpszPhone = (WCHAR *)DialogBox (hInst, MAKEINTRESOURCE (IDD_DIALOG1), NULL, DialerDlgProc);
	else {
		WCHAR *p = szPhoneBuffer;
		while ((*lpszPhone != '\0') && (p - szPhoneBuffer < DIAL_STRING_SIZE - 1)) {
			switch (*lpszPhone) {
			case '-':
			case ' ':
			case '+':
			case '(':
			case ')':
				++lpszPhone;
				continue;
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
				*p++ = *lpszPhone++;
				continue;
			default:
				MessageBox (NULL, L"Unrecognized phone format", L"Dialer Error", MB_OK | MB_TOPMOST);
				return 0;
			}
		}

		*p = '\0';

		lpszPhone = szPhoneBuffer;
	}

	if ((! lpszPhone) || (lpszPhone[0] == '\0'))
		return 0;

	HWND hDlg = CreateDialog (hInst, MAKEINTRESOURCE (IDD_ABOUTBOX), NULL, AboutDlg);
	WCHAR szMsg[256];

	wsprintf (szMsg, L"Dialing %s", lpszPhone);

	SetWindowText (GetDlgItem (hDlg, IDC_INFO_1), szMsg);
	SetWindowText (GetDlgItem (hDlg, IDC_INFO_2), L"Searching for phone");

	SetWindowPos (hDlg, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE);
    UpdateWindow (hDlg);

	char szDialerString[DIAL_STRING_SIZE + 10];
	strcpy (szDialerString, "ATD");
	WideCharToMultiByte (CP_ACP, 0, lpszPhone, -1, szDialerString + 3, sizeof(szDialerString)-3-1-1, NULL, NULL);
	strcat (szDialerString, ";\r");

	for (int i = 0 ; i < 3 ; ++i) {
		HANDLE hFile = CreateFile (gszPortName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);


		if (hFile != INVALID_HANDLE_VALUE) {
			wsprintf (szMsg, L"Dialing...");
			SetWindowText (GetDlgItem (hDlg, IDC_INFO_2), szMsg);
			UpdateWindow (hDlg);

			DWORD dwWrit = 0;
			Sleep(1000);
			WriteFile (hFile, szDialerString, strlen(szDialerString), &dwWrit, NULL);
			Sleep(1000);
			CloseHandle (hFile);
			Sleep(100);
			BthTerminateIdleConnections ();		// Let the phone get to the headset
			break;
		} else if (i == 1) {
			wsprintf (szMsg, L"Phone not found...");
			SetWindowText (GetDlgItem (hDlg, IDC_INFO_2), szMsg);
			UpdateWindow (hDlg);
			Sleep (3000);
			break;
		}

		wsprintf (szMsg, L"Searching, attempt %d", i+2);
		SetWindowText (GetDlgItem (hDlg, IDC_INFO_2), szMsg);
		UpdateWindow (hDlg);
	}

	DestroyWindow (hDlg);

	return 0;
}

⌨️ 快捷键说明

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