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

📄 btsearch.cpp

📁 在PDA上应用的EVC环境中蓝牙设备处理应用程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// btsearch.cpp : Defines the entry point for the application.
//



#include "stdafx.h"
#include "resource.h"
#include "Afx.h"
#include "Winuser.h"
#include "Afxwin.h"
#include "bthutil.h"
#include "Bt_api.h"
#include "bthapi.h"
#include "bt_sdp.h"
#include "Objbase.h"


#define RFCOMM_CHANNAL_MULTIPLE 0xfe

HINSTANCE g_hInst;

BOOL WINAPI MainDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam );
void PopulateBtDevList(HWND hDlg );
void BthDiscover(HWND hDlg);
void BthVCom(HWND hDlg);
void BthConn(HWND hDlg);
extern "C" int BthGetMode(DWORD * pdwMode);
extern "C" int BthSetMode(DWORD pdwMode);
/*************************************************************

  Function: WinMain
  
  Purpose: Standard Win32 Entry point
	
**************************************************************/
int WINAPI WinMain(	HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   LPTSTR    lpCmdLine,
				   int       nCmdShow)
{
	// TODO: Place code here.
	
	WORD wVersionRequested;
	WSADATA wsaData;
	
	g_hInst = hInstance;
	// Initialize Winsock
	wVersionRequested = MAKEWORD( 2, 2 );
	
	if (WSAStartup( wVersionRequested, &wsaData ) == 0)
	{
		DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, MainDlgProc);
	}
	else
	{
		MessageBox(NULL, _T("Could not initialize winsock!"), _T("Fatal Error"), MB_OK);
		
	}
	
	return 0;
}
/*************************************************************

  Function: MainDlgProc
  
  Purpose: Main Dialog Window message handler
	
**************************************************************/
BOOL WINAPI MainDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
{
	
	switch (msg)
	{
	case WM_INITDIALOG:
		{
			
			SHINITDLGINFO sid;
			SHMENUBARINFO smb;
			RECT rc;
			
			sid.dwMask   = SHIDIM_FLAGS;
			sid.dwFlags = SHIDIF_SIZEDLGFULLSCREEN;
			sid.hDlg   = hDlg;
			
			// size dialog to full screen
			SHInitDialog(&sid);
			
			smb.cbSize = sizeof(SHMENUBARINFO);
			smb.hInstRes = g_hInst;
			smb.hwndParent = hDlg;
			smb.nToolBarId = IDR_MENUBAR;
			
			// create the Options menubar
			SHCreateMenuBar(&smb);
			
			// size the listbox control
			
			GetClientRect(hDlg, &rc);
			
			// Size the listbox control to the size of the window
			SetWindowPos(GetDlgItem(hDlg, IDC_DEVLIST), 
				0, 
				rc.left, 
				rc.top, 
				rc.right - rc.left+1, 
				rc.bottom - rc.top+1, 
				0);
			
			
			SendMessage(GetDlgItem(hDlg, IDC_DEVLIST),
				LB_ADDSTRING,
				0,
				(LPARAM) _T("Select \'Search\' below")); 
			
			
			
		}
		break;
	case WM_COMMAND:
		
		switch(LOWORD(wParam))
		{
		case IDM_QUIT:
			// 
			EndDialog(hDlg, IDOK);
			break;
			
		case IDM_SEARCH:
			
			PopulateBtDevList(hDlg);
			
			break;
		case IDM_BTHDISCOVER:

			BthDiscover(hDlg);

			break;
		case ID_BTHVCOM:

			BthVCom(hDlg);

			break;
		case ID_BTHCONN:
			
			BthConn(hDlg);

			break;
//		case ID_BTHSERQUERY:
//			
//			PerformServiceSearch (BT_ADDR *pb);
//
//			break;	
		}
		
		
		break;
	}
	
	
	return FALSE;
}

/*************************************************************

  Function: PoplulateBtDevList
  
  Purpose: Searches for available bluetooth devices and displays
			them in the listbox
	
**************************************************************/
void PopulateBtDevList(HWND hDlg )
{
	
	INT				iResult = 0;
	LPWSAQUERYSET	pwsaResults;
	DWORD			dwSize = 0;
	WSAQUERYSET		wsaq;
	HCURSOR			hCurs;
	HANDLE			hLookup = 0;
	
	WORD wVersionRequested;
	WSADATA wsaData;
	wVersionRequested = MAKEWORD( 2, 2 );
	WSAStartup( wVersionRequested, &wsaData );

	// empty the list box
	SendMessage(GetDlgItem(hDlg, IDC_DEVLIST),
		LB_RESETCONTENT,
		0,
		0);
	
	SendMessage(GetDlgItem(hDlg, IDC_DEVLIST),
		LB_ADDSTRING,
		0,
		(LPARAM) _T("Results...")); 
	
	
	memset (&wsaq, 0, sizeof(wsaq));
	wsaq.dwSize      = sizeof(wsaq);
	wsaq.dwNameSpace = NS_BTH;
	wsaq.lpcsaBuffer = NULL;
	
	// initialize searching procedure
	iResult = WSALookupServiceBegin(&wsaq, 
		LUP_CONTAINERS, 
		&hLookup);
	
	if (iResult != 0)
	{
		TCHAR tszErr[32];
		iResult = WSAGetLastError();
		_sntprintf(tszErr, 32, _T("Socket Error: %d"), iResult);
		MessageBox(hDlg, tszErr, _T("Error"), MB_OK);
		
		
	}
	
	union {
		CHAR buf[5000];				// returned struct can be quite large 
		SOCKADDR_BTH	__unused;	// properly align buffer to BT_ADDR requirements
	};
	
	// save the current cursor
	hCurs = GetCursor();
	
	for (; ;)
	{
		// set the wait cursor while searching
//		SetCursor(LoadCursor(NULL, IDC_WAIT));
		pwsaResults = (LPWSAQUERYSET) buf;
		
		dwSize  = sizeof(buf);
		
		memset(pwsaResults,0,sizeof(WSAQUERYSET));
		pwsaResults->dwSize      = sizeof(WSAQUERYSET);
		// namespace MUST be NS_BTH for bluetooth queries
		pwsaResults->dwNameSpace = NS_BTH;
		pwsaResults->lpBlob      = NULL;
		
		// iterate through all found devices, returning name and address
		// (this sample only uses the name, but address could be used for
		// further queries)
		iResult = WSALookupServiceNext (hLookup, 
			LUP_RETURN_NAME | LUP_RETURN_ADDR, 
			&dwSize, 
			pwsaResults);
		
		
		if (iResult != 0)
		{
			iResult = WSAGetLastError();
			if (iResult != WSA_E_NO_MORE)
			{
				TCHAR tszErr[32];
				iResult = WSAGetLastError();
				_sntprintf(tszErr, 32, _T("Socket Error: %d"), iResult);
				MessageBox(hDlg, tszErr, _T("Error"), MB_OK);
				
			}
			// we're finished

			break;
		}
		
		// add the name to the listbox
		if (pwsaResults->lpszServiceInstanceName)
		{
			SendMessage(GetDlgItem(hDlg, IDC_DEVLIST), 
				LB_ADDSTRING, 
				0, 
				(LPARAM)pwsaResults->lpszServiceInstanceName);
			
		}
		
	}
	
	WSALookupServiceEnd(hLookup);
	
	// restore cursor
	SetCursor(hCurs);
	
}
static int PerformServiceSearch(BT_ADDR pb)
{

}
/******************************************************

*******************************************************/



/*************************************************************

  Function: 
  
  Purpose: 
**************************************************************/
void BthDiscover(HWND hDlg)
{
	WSAQUERYSET wsaq;
	CString BthName="";
	unsigned __int64  addr = 0;
	BT_ADDR pba;

	int channel=0x08;
	int rc;
	DWORD cBytes;
	BYTE ch=NULL;
	DWORD index=3;
	HANDLE hLookup;
	SOCKADDR_BTH sa;
	DWORD port = 0;
	DWORD dwSizeOut = 0;
//	HANDLE hFile;

	WORD wVersionRequested;
	WSADATA wsaData;
	wVersionRequested = MAKEWORD( 2, 2 );
	WSAStartup( wVersionRequested, &wsaData );

	union {
		CHAR buf[5000];
		double __unused; // ensure proper alignment
	};
	LPWSAQUERYSET pwsaResults = (LPWSAQUERYSET) buf;
	DWORD dwSize  = sizeof(buf);
	BOOL bHaveName;
	ZeroMemory(&wsaq, sizeof(wsaq));
	wsaq.dwSize = sizeof(wsaq);
	wsaq.dwNameSpace = NS_BTH;
	wsaq.lpcsaBuffer = NULL;
	if (ERROR_SUCCESS != WSALookupServiceBegin (&wsaq, LUP_CONTAINERS, &hLookup))
	{
		wprintf(L"WSALookupServiceBegin failed %d\r\n", GetLastError());
//		return FALSE;

⌨️ 快捷键说明

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