pc机上VC++6.0写的蓝牙服务,手机搜索不到
最近参照教程在我的电脑上用VC++写了一个关于蓝牙的程序程序。当我在电脑上插上蓝牙适配器后运行程序时,我的手机没法搜索到电脑蓝牙,不知道为什么,请高手指点。谢谢!代码如下:
#include "stdafx.h"
#include <initguid.h>
#include <winsock2.h>
#include <ws2bth.h>
#pragma comment (lib,"ws2_32.lib")
#pragma comment (lib,"msvcrt.lib")
DEFINE_GUID(SAMPLE_UUID,0x31b44148,0x041f,0x42f5,0x8e,0x73,0x18,0x6d,0x5a,0x47,0x9f,0xc9);
int _tmain(int argc, _TCHAR* argv[ ])
{
SOCKET server;
SOCKADDR_BTH sa;
int sa_len = sizeof(sa );
//initialize windows sockets
printf("initialize windows sockets...\n");
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD( 2,0 );
if( WSAStartup( wVersionRequested, &wsaData) !=NO_ERROR ) {
ExitProcess ( 2 ) ;
}
// create the server socket
printf("create the server socket...\n");
server = socket (AF_BTH , SOCK_STREAM , BTHPROTO_RFCOMM ) ;
if( SOCKET_ERROR ==server) {
ExitProcess ( 2 );
}
// bind the server socket to an arbitrary RFCOMM port
printf("bind the server socket to an arbitrary RFCOMM port...\n");
sa.addressFamily=AF_BTH;
sa.btAddr =0;
sa.port =BT_PORT_ANY ;
if(SOCKET_ERROR == bind (server ,(const sockaddr*)&sa , sizeof(SOCKADDR_BTH )))
{
ExitProcess ( 2 ) ;
}
listen( server, 1 );
// check which port we’re listening on
printf("check which port we’re listening on...\n");
if(SOCKET_ERROR==getsockname (server, (SOCKADDR*)&sa, &sa_len )) {
ExitProcess ( 2 ) ;
}
printf("listening on RFCOMM port: %d\n",sa.port);
// advertise the service
printf("advertise the service...\n");
CSADDR_INFO sockInfo ;
sockInfo.iProtocol=BTHPROTO_RFCOMM;
sockInfo.iSocketType=SOCK_STREAM;
sockInfo.LocalAddr.lpSockaddr=(LPSOCKADDR) &sa;
sockInfo.LocalAddr.iSockaddrLength=sizeof(sa );
sockInfo.RemoteAddr.lpSockaddr=(LPSOCKADDR) &sa;
sockInfo.RemoteAddr.iSockaddrLength=sizeof(sa );
WSAQUERYSET svcInfo={0};
svcInfo.dwSize=sizeof(svcInfo);
svcInfo.dwNameSpace=NS_BTH;
svcInfo.lpszServiceInstanceName="Win32 Sample Bluetooth Service";
svcInfo.lpszComment="Description of service..." ;
svcInfo.lpServiceClassId=(LPGUID)&SAMPLE_UUID;
svcInfo.dwNumberOfCsAddrs =1;
svcInfo.lpcsaBuffer=&sockInfo;
if( SOCKET_ERROR ==WSASetService(&svcInfo,RNRSERVICE_REGISTER,0))
{
printf("setService goes wrong...\n");
ExitProcess(2);
}
printf("setService goes ok...\n");//程序能够运行到这里来!
SOCKADDR_BTH ca;
int ca_len = sizeof(ca);
SOCKET client;
char buf[1024] = {0};
DWORD buf_len = sizeof(buf);
client=accept(server,(LPSOCKADDR) &ca, &ca_len );
if( SOCKET_ERROR == client ) {
printf("accept goes wrong...\n");
ExitProcess ( 2 ) ;
}
printf("accept goes ok...\n");
WSAAddressToString ( (LPSOCKADDR)&ca, (DWORD) ca_len,NULL,buf,&buf_len ) ;
printf( "Accepted connection from %s\n" , buf ) ;
int received =0;
received = recv (client,buf,sizeof(buf),0);
if(received>0) {
printf("received: %s\n",buf) ;
}
closesocket (client);
closesocket (server);
return 0;
}
[解决办法]
问题解决啦,你联系一下我吧。