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

📄 bluetoothdlg.cpp

📁 一个简单的蓝牙发现程序
💻 CPP
字号:
// bluetoothDlg.cpp : implementation file
//

#include "stdafx.h"
#include "bluetooth.h"
#include "bluetoothDlg.h"

#include ".\bluetoothdlg.h"

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

CListBox* devices = NULL  ;
CButton* buttonScan = NULL ;
CButton* buttonPropertySheet = NULL ;
CButton* buttonAddr = NULL ;


/**********************************************************************************************************************/
#include <bthsdpdef.h>
#include <BluetoothAPIs.h>
#pragma comment ( lib, "Irprops.lib")
	
#define MAX_DEVICE_COUNT 32 
BLUETOOTH_DEVICE_INFO device_array[MAX_DEVICE_COUNT] ;
static int device_array_pointer = 0 ;


void WINAPI ScanThread (LPVOID lpParameter)
{
	buttonScan->EnableWindow(SW_HIDE);
	buttonScan->SetWindowText("Scan in progress");
	buttonPropertySheet->EnableWindow(SW_HIDE);
	buttonAddr->EnableWindow(SW_HIDE);

	for(int i =  0 ; i<devices->GetCount();i++)
			devices->DeleteString(0);
		

		BLUETOOTH_DEVICE_SEARCH_PARAMS bdsp;
		BLUETOOTH_DEVICE_INFO bdi;
		HBLUETOOTH_DEVICE_FIND hbf;
		ZeroMemory(&bdsp, sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS));

		bdsp.dwSize = sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS);
		bdsp.fReturnAuthenticated = TRUE;
		bdsp.fReturnRemembered = TRUE;
		bdsp.fReturnUnknown = TRUE;
		bdsp.fReturnConnected = TRUE;
		bdsp.fIssueInquiry = TRUE;
		bdsp.cTimeoutMultiplier = 10;
		bdsp.hRadio = NULL;
		bdi.dwSize = sizeof(bdi);

		hbf = BluetoothFindFirstDevice(&bdsp, &bdi);
		DWORD dwErr = GetLastError();

		if (hbf != NULL)
		{
		TCHAR szDevName[MAX_PATH];

		device_array_pointer = 0 ;

				while(1)
				{
				memset(szDevName, 0x00, sizeof(szDevName));
				  
				sprintf(szDevName, _T("Name:%S"), bdi.szName);
				 

				devices->AddString(szDevName);

				device_array[device_array_pointer] = bdi ;

				  
				device_array_pointer++;
				 

				if (BluetoothFindNextDevice(hbf, &bdi) == FALSE)
				break;
				}

		BluetoothFindDeviceClose(hbf);
		}

	buttonScan->EnableWindow(SW_SHOW);
	buttonScan->SetWindowText("Scan for devices");
	buttonPropertySheet->EnableWindow(SW_SHOW);
	buttonAddr->EnableWindow(SW_SHOW);

	return ;
}

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

#ifdef _DEBUG
#define new DEBUG_NEW
#endif



// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CbluetoothDlg dialog



CbluetoothDlg::CbluetoothDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CbluetoothDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CbluetoothDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CbluetoothDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP

	ON_BN_CLICKED(ID_BUTTON_PROPERTY_SHEET, OnBnClickedButtonPropertySheet)
	ON_BN_CLICKED(ID_BUTTON_SCAN, OnBnClickedButtonScan)
	ON_BN_CLICKED(ID_BUTTON_SHOW_MAC, OnBnClickedButtonShowMac)

END_MESSAGE_MAP()


// CbluetoothDlg message handlers

BOOL CbluetoothDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	devices = (CListBox *) GetDlgItem(ID_LIST_DEVICES);
	buttonScan = (CButton *) GetDlgItem(ID_BUTTON_SCAN);
	buttonPropertySheet = (CButton *) GetDlgItem(ID_BUTTON_PROPERTY_SHEET);
	buttonAddr = (CButton *) GetDlgItem(ID_BUTTON_SHOW_MAC);
	
	


	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CbluetoothDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CbluetoothDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CbluetoothDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}




void CbluetoothDlg::OnBnClickedButtonPropertySheet()
{
	CListBox* Devices = (CListBox *) GetDlgItem( ID_LIST_DEVICES );
	unsigned int index = Devices->GetCurSel();

	if(index == -1 ) {MessageBox("Please select a device");return;}
	
	BluetoothDisplayDeviceProperties(NULL,&device_array[index]);
	
}

void CbluetoothDlg::OnBnClickedButtonScan()
{
	DWORD dwThreadID ; 
	CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ScanThread,NULL,0,&dwThreadID);
}

void CbluetoothDlg::OnBnClickedButtonShowMac()
{
	CListBox* Devices = (CListBox *) GetDlgItem( ID_LIST_DEVICES );
	unsigned int index = Devices->GetCurSel();

	if(index == -1 ) {MessageBox("Please select a device");return;}

	char addr[7];
	for(int i = 0 ; i<6;i++)
		addr[i] = device_array[index].Address.rgBytes[i] ;
    addr[6] = '\0';


	CString s ;
	s.Format("%x",addr);
	AfxMessageBox(s);

}

⌨️ 快捷键说明

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