pc机上VC++6.0写的蓝牙服务,手机搜索不到解决思路
发布时间:2010-05-30 00:34:26 文章来源:www.iduyao.cn 采编人员:星星草
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;
}
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。
其他相似内容:
-
高手,求个S60v3 FP2 320*240分辨率的SDK
求个S60v3 FP2 320*240分辨率的SDK,
240*320的SDK虽然可以调分辨率的,但我想要一个和E72一...
-
请教关于smybian上使用sqlite的问题
请问如何在symbian 9.1版本上使用sqlite啊?
------解决方案--------------------
直接去sql...
-
symbian 可以内嵌浏览器吗
谁有内嵌浏览器的简单代码吗?或者如何实现内嵌浏览器。谢谢
------解决方案--------------------
ht...
-
symbian:如何判断一个文件是否系统支持的手机铃声格式
现在需要扫描一些固定后缀的文件如MP3,aac等显示出来供用户选择设置为手机铃...
-
数据库求助!!
我创建了一个RDbStoreDatabase,想把通讯录的数据库内容复制过来,该如何实现?实在是不想一条条的insert。。。
------解...
-
Specified file '\Symbian\9.1\S60_3rd_MR\EPOC32\RELEASE\WINSCW\UDEB\.lib' not found 什么意思?
请大家帮我解决一下,...
-
关于Symbian的express signed
我想给一个Symbian程序做一个express signed,在得到了publisheID后,还需要一个Content ID才能完成,可...
-
CPbkContactEngine::NewL()真机崩溃
如题,在模拟器上正常,在真机上崩溃
哪位遇到过?
------解决方案--------------------
我也遇...
-
请问在symbian如何获取最近手机基站的lac和cid?
请问在symbian如何获取最近手机基站的lac和cid?
------解决方案---------------...
-
求救rvct2.2问题:Cannot obtain license for Compiler (feature compiler) with license version >= 2.2
在carbide用rvct2.2编译...