您的位置:首页 > 其它

wince 蓝牙 操作 搜索蓝牙设备 建立连接 建立虚拟串口 MFC

2011-11-28 16:43 489 查看
我弄的时候 也看了下网络资料 多少有些缺陷 现在我把我整理好的 贴出来

一共 分为 3部分 bluetooth.H bluetooth.cpp 使用说明 这几部分

1.bluetooth.h

#ifndef BLUETOOTH_H

#define BLUETOOTH_H

#include "winsock2.h"

#include "Ws2bth.h"

typedef struct BluetoothInfo

{

BT_ADDR Addr;

TCHAR Name[32];

int ComNum;

}BTINFO;

//搜索蓝牙设备存放到 btinfo数组中

bool BluetoothSearch(BTINFO btinfo[16],int *DeviceNum);

//根据蓝牙地址 创建 虚拟串口 并返回窗口号

int BluetoothCOMCreate (BT_ADDR ServerAddress);

//销毁 所创建的 蓝牙虚拟串口

void BluetoothCOMDestroy(void);

#endif

2.buletooth.cpp

#include "stdafx.h"

#include "Bt_api.h"

#include "BluetoothCOM.h"

#include "malloc.h"

#include "basetyps.h"

#include "ws2bth.h"

//==================================================================

HANDLE hCom = NULL;

typedef struct _RemoteBthDevInfo

{

_RemoteBthDevInfo ()

{

memset ( szName, 0, sizeof(szName) );

memset ( &RemoteAddr, 0, sizeof(BT_ADDR) );

memset ( &LocalAddr, 0, sizeof(BT_ADDR) );

}

TCHAR szName[64];

BT_ADDR RemoteAddr;

BT_ADDR LocalAddr;

} t_RemoteBthDevInfo;

typedef CArray<t_RemoteBthDevInfo,t_RemoteBthDevInfo&> t_Ary_RemoteBthDevInfo;

//==================================================================

bool BluetoothSearch(BTINFO btinfo[16],int *DeviceNum)

{

t_Ary_RemoteBthDevInfo m_Ary_RemoteBthDevInfo;

m_Ary_RemoteBthDevInfo.RemoveAll ();

WSAQUERYSET wsaq;

WSADATA wsaData;

HANDLE hLookup = NULL;

union

{

CHAR buf[5000];

double __unused; // ensure proper alignment

};

WSAStartup (MAKEWORD(2,2),&wsaData);

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))

{

CString str;

str.Format((L"error is %d!"),GetLastError());

::MessageBox(NULL,str,0,0);

WSACleanup();

return false;

}

ZeroMemory(pwsaResults, sizeof(WSAQUERYSET));

pwsaResults->dwSize = sizeof(WSAQUERYSET);

pwsaResults->dwNameSpace = NS_BTH;

pwsaResults->lpBlob = NULL;

while (ERROR_SUCCESS == WSALookupServiceNext (hLookup, LUP_RETURN_NAME | LUP_RETURN_ADDR, &dwSize, pwsaResults))

{

ASSERT (pwsaResults->dwNumberOfCsAddrs == 1);

BT_ADDR b = ((SOCKADDR_BTH *)pwsaResults->lpcsaBuffer->RemoteAddr.lpSockaddr)->btAddr;

bHaveName = pwsaResults->lpszServiceInstanceName && *(pwsaResults->lpszServiceInstanceName);

t_RemoteBthDevInfo RemoteBthDevInfo;

if ( bHaveName )

{

StringCchPrintf ( RemoteBthDevInfo.szName, sizeof(RemoteBthDevInfo.szName), _T("%s"),

pwsaResults->lpszServiceInstanceName );

StringCchPrintf ( btinfo[*DeviceNum].Name, 32, _T("%s"), pwsaResults->lpszServiceInstanceName );

btinfo[*DeviceNum].Addr = b;

*DeviceNum = *DeviceNum+1;

}

}

WSALookupServiceEnd(hLookup);

WSACleanup();

return true;

}

int BluetoothCOMCreate( BT_ADDR ServerAddress)

{

GUID guid;

CoCreateGuid(&guid);

SOCKADDR_BTH sa;

WSADATA wsaData;

int channel = 1;

if (0 != WSAStartup(MAKEWORD(2,2), &wsaData))

{

MessageBox(NULL,L"Failed to retrive socket version.",0,0);

return false;

}

SOCKET m_socketClient = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);

if ( m_socketClient==INVALID_SOCKET )

{

CString str;

str.Format(L"error of creatsocket is %d",GetLastError());

MessageBox(NULL,str,0,0);

return -1;

}

//密码配对

char* szPIN = "0000";

BTH_SOCKOPT_SECURITY bth_sockopt;

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

bth_sockopt.iLength = strlen(szPIN);

bth_sockopt.btAddr = ServerAddress;//改为0也是一样

char szBuf[32] ;

memset(szBuf, 0, sizeof(szBuf));

strcpy(szBuf, szPIN);

memcpy(bth_sockopt.caData,szBuf,strlen(szBuf)+1);

if(setsockopt(m_socketClient, SOL_RFCOMM, SO_BTH_SET_PIN, (char *)&bth_sockopt, sizeof(BTH_SOCKOPT_SECURITY)) == SOCKET_ERROR )

{

int nError = WSAGetLastError();

MessageBox(NULL,L"配对失败",0,0);

WSACleanup();

}//配对结束

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

sa.btAddr = ServerAddress; //b is a BT_ADDR variable

sa.addressFamily = AF_BT;

sa.port = channel & 0xff;

sa.serviceClassId = guid;

if (connect (m_socketClient, (SOCKADDR *)&sa, sizeof(sa)))

{

closesocket (m_socketClient);

MessageBox(NULL,L"connect Socket error",0,0);

return -1;

}

//=============================================

// 蓝牙虚拟串口

//=============================================

//创建客户端蓝牙串口

PORTEMUPortParams pp;

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

pp.device = ServerAddress;

pp.channel = channel & 0xff;

pp.uiportflags = RFCOMM_PORT_FLAGS_REMOTE_DCB;

int index=0;

hCom = NULL;

for (index=0;index<10;index++)

{

hCom = RegisterDevice (L"COM", index, L"btd.dll", (DWORD)&pp);

if(hCom != NULL)

break;

}

if (index >= 9)

MessageBox(NULL,L"com failed",0,0);

WSACleanup();

return index;

}

void BluetoothCOMDestroy(void)

{

DeregisterDevice(hCom);

}

3.使用说明

int DeviceNum = 0;

BTINFO BtInfoArry[16];

BluetoothSearch(BtInfoArry,&DeviceNum);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: