首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

基于winsock的蓝牙搜索、通信程序

2013-07-08 
基于winsock的蓝牙搜寻、通信程序本帖最后由 VisualEleven 于 2011-01-04 11:18:45 编辑我的电脑是客户端,

基于winsock的蓝牙搜寻、通信程序
本帖最后由 VisualEleven 于 2011-01-04 11:18:45 编辑 我的电脑是客户端,远程蓝牙模块是服务端,用socket 进行通信,为什么单步调试是能过真确得到recv()值,全速运行就有问题,程序长时间没反应。

ULONG              ulRetCode = 0;
int                iCxnCount = 0;
SOCKET             LocalSocket = INVALID_SOCKET;
SOCKADDR_BTH       SockAddrBthServer = {0};

#defineCXN_TRANSFER_DATA_LENGTH100
#defineCXN_RECEIVE_DATA_LENGTH100

CHAR               szSendData[CXN_TRANSFER_DATA_LENGTH] = {0};
//我定义的
CHAR   szRecBuf[CXN_RECEIVE_DATA_LENGTH];


// Setting address family to AF_BTH indicates winsock2 to use Bluetooth sockets
// Port should be set to 0 if ServiceClassId is spesified.

SockAddrBthServer.addressFamily = AF_BTH;
SockAddrBthServer.btAddr = (BTH_ADDR) SelectionRemoteBtAddr;
SockAddrBthServer.serviceClassId = g_guidServiceClass;
SockAddrBthServer.port = 1;// Valid ports are 1 - 31

if ( ( LocalSocket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM) ) == INVALID_SOCKET )
{//创建套接字

//printf("=CRITICAL= | socket() call failed. Error = [%d]\n", WSAGetLastError());

//AfxMessageBox( (LPCTSTR) WSAGetLastError(), MB_OK, 0 );
AfxMessageBox( _T("create socket for the bluetooth fialed !") );

}

// Connect the socket (pSocket) to a given remote socket represented by address (pServerAddr)

if ( connect(LocalSocket, (struct sockaddr *) &SockAddrBthServer, sizeof(SOCKADDR_BTH)) == SOCKET_ERROR )
{//连接套接字

AfxMessageBox( _T("connect socket for the bluetooth fialed !") );

}

strcpy(szSendData, "7XY");//待发送的字符命令

if ( send(LocalSocket, szSendData, 3, 0) == SOCKET_ERROR )
{//发送数据

AfxMessageBox( _T("send data for the bluetooth fialed !") );

}

#definesTIMEOUT2
#defineuTIMEOUT1000

fd_setfdRead;
timeval TimeOut;
TimeOut.tv_sec= sTIMEOUT;
TimeOut.tv_usec= uTIMEOUT;

FD_ZERO(&fdRead);
FD_SET(LocalSocket,&fdRead);
int Ret = select( 0,&fdRead, NULL, NULL,&TimeOut );
//这样select返回的时候可以根据ret的值判断时候有数据可读。ret>0可读,==0则是SOCKET_ERROR

if ( Ret > 0 )
{
if ( SOCKET_ERROR == recv(LocalSocket, szRecBuf, 1, 0) )
{
AfxMessageBox( _T("receive data for the bluetooth fialed !") );

}
else
AfxMessageBox( _T("receive data for the bluetooth success !") );

}
else if ( Ret == 0 )
{
AfxMessageBox( _T("select for the bluetooth timeout !") );

}else if ( Ret == SOCKET_ERROR )


{
AfxMessageBox( _T("select for the bluetooth fialed !") );

}


[解决办法]
那问题应该出在你的timeout的时间上吧,
没有反应?你不是通过超时值可以上select返回的吗?

[解决办法]
蓝牙的没做近。。很感兴趣。 MARK.

另。。楼主你安装蓝牙驱动了吗?

热点排行