一、枚举本地蓝牙设备
/******************************************************************************\
枚举本地所有蓝牙设备到 m_arrLocal,返回本地蓝牙设备数
\******************************************************************************/
int CBlueTooth::EnumLocalDev()
{
RemoveAllLocalDev();
HANDLE hRadio = NULL;
BLUETOOTH_FIND_RADIO_PARAMS bfrp = {sizeof(bfrp)};
HBLUETOOTH_RADIO_FIND hFind = BluetoothFindFirstRadio(&bfrp,&hRadio);
if(hFind)
{
do{
if(hRadio)
{
m_arrLocal.Add(hRadio);
}
}while(BluetoothFindNextRadio(hFind,&hRadio));
BluetoothFindRadioClose(hFind);
}
return (int)m_arrLocal.GetSize();
}
二、搜索远程蓝牙设备
//
// 用蓝牙 APIs 搜索附近的蓝牙设备,成功时返回设备数,否则返回-1
//
int CBlueTooth::Scan(HANDLE hRadio,BOOL fReturnAuthenticated,BOOL fReturnRemembered,BOOL fReturnUnknown
,BOOL fReturnConnected,BOOL fIssueInquiry,UCHAR cTimeoutMultiplier)
{
RemoveAllRemoteDev();
BLUETOOTH_DEVICE_INFO bdi = { sizeof(BLUETOOTH_DEVICE_INFO) };
BLUETOOTH_DEVICE_SEARCH_PARAMS bdsp;
ZeroMemory(&bdsp, sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS));
bdsp.dwSize = sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS);
bdsp.hRadio = hRadio;
bdsp.fReturnAuthenticated = fReturnAuthenticated;
bdsp.fReturnRemembered = fReturnRemembered;
bdsp.fReturnUnknown = fReturnUnknown;
bdsp.fReturnConnected = fReturnConnected;
bdsp.fIssueInquiry = fIssueInquiry;
bdsp.cTimeoutMultiplier = cTimeoutMultiplier;
HBLUETOOTH_DEVICE_FIND hbf = BluetoothFindFirstDevice(&bdsp, &bdi);
if(hbf == NULL)
{
return -1;
}
do
{
TRACE ( _T("%s ( %s )\n"), bdi.szName, AddrToStr(bdi.Address.rgBytes) );
RemoteDev dev;
dev.Address = bdi.Address.ullLong;
dev.sName = bdi.szName;
m_arrRemote.Add(dev);
}while(BluetoothFindNextDevice(hbf,&bdi));
BluetoothFindDeviceClose(hbf);
return m_arrRemote.GetSize();
}
三、与指定的蓝牙设备配对
int nSelLocal = 0;
int nSelRemote = 0;
BLUETOOTH_DEVICE_INFO bdi;
if(!GetSomeInfo(&nSelLocal,&nSelRemote,&bdi))
{
return;
}
CDlgPairSend dlg;
dlg.m_sLocal = m_ListLocal. GetItemText(nSelLocal ,0) + _T(" - ") + m_ListLocal .GetItemText(nSelLocal ,1);
dlg.m_sRemote = m_ListRemote.GetItemText(nSelRemote,0) + _T(" - ") + m_ListRemote.GetItemText(nSelRemote,1);
if(dlg.DoModal() == IDOK)
{
int nLenW = MultiByteToWideChar(CP_ACP,0,dlg.m_sPwd,dlg.m_sPwd.GetLength(),NULL,0);
wchar_t*wzPwd = new wchar_t[nLenW + 1];
MultiByteToWideChar(CP_ACP,0,dlg.m_sPwd,dlg.m_sPwd.GetLength(),wzPwd,nLenW);
t_RemoteBthDevInfo RemoteBthDevInfo;
RemoteBthDevInfo.Address.ullLong = bdi.Address.ullLong;
m_BlueTooth.EnumerateInstalledServices ( RemoteBthDevInfo );
if(BluetoothAuthenticateDevice(m_hWnd,m_BlueTooth.m_arrLocal[nSelLocal],&bdi,wzPwd,nLenW)==ERROR_SUCCESS)
{
MessageBox(_T("配对成功"));
}
else
{
MessageBox(_T("配对失败"));
}
delete[] wzPwd;
四、连接蓝牙设备,枚举串口,寻找蓝牙虚拟串口,发送握手指令 ,
HKEY hKey;
LPCTSTR lpSubKey="HARDWARE\\DEVICEMAP\\SERIALCOMM\\";
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpSubKey, 0, KEY_READ, &hKey)!= ERROR_SUCCESS)
{
return ;
}
char szValueName[NAME_LEN];
BYTE szPortName[NAME_LEN];
LONG status;
DWORD dwIndex = 0;
DWORD dwSizeValueName=100;
DWORD dwSizeofPortName=100;
DWORD Type;
dwSizeValueName = NAME_LEN;
dwSizeofPortName = NAME_LEN;
do
{
status = RegEnumValue(hKey, dwIndex++, szValueName, &dwSizeValueName, NULL, &Type,
szPortName, &dwSizeofPortName);
if((status == ERROR_SUCCESS))
{
CString str;
str=(char *)szPortName;
m_SerialPort.SetCom(str);
if (m_SerialPort.OpenConnection())
{
//发送指令,判断串口是否有回复,有回复即为蓝牙虚拟串口
unsigned char data[2]={0xa5,0x5a}; //握手
m_SerialPort.WriteComm(data,2);
char *Buf=new char[10];
memset(Buf,0,10);
int nCount=0;
nCount=m_SerialPort.ReadComm(Buf,1);
byte bt=Buf[0];
if (bt==0xa5)
{
MessageBox(_T("连接成功"));
Start();
break;
}
}
}
dwSizeValueName = NAME_LEN;
dwSizeofPortName = NAME_LEN;
} while((status!= ERROR_NO_MORE_ITEMS));
RegCloseKey(hKey);
五、通过串口与远程蓝牙设备通信
源码参考:https://download.csdn.net/download/qq_23565865/10789156