首 页 | 新 闻 | Symbian | Android| Windows Mobile | J2ME | 下载中心 | 游戏策划招聘与求职 | 购书指南 | 视频教程
您现在的位置: 开发视界 >> Windows Mobile >> Windows Mobile 入门 >> 正文
Smartphone 蓝牙虚拟串口, 注册服务失败的解决办法
作者:佚名    文章来源:csdn论坛    更新时间:2008-1-1 17:22:07
问题:
蓝牙虚拟串口,   注册服务失败

void   CBlueTestDlg::OnBnClickedButton1()
{
        //   注册服务
        PORTEMUPortParams   pp   ;
        ZeroMemory(&pp,   sizeof(pp));
        pp.channel   =   RFCOMM_CHANNEL_MULTIPLE   ;//   the   next   available   channel
        pp.flocal   =   TRUE;        
GetBA(_T("000b244ada77"),   &pp.device);

        int   nIndex   =     1;
        HANDLE   m_hDev   =   RegisterDevice(_T("COM"),   nIndex,   _T("btd.dll"),   (DWORD)&pp);
        //这里会返回错误,   错误代码为2404,   意思为"   设备正由活动进程使用,无法断开。"  
        CString   sErr   ;
        sErr.Format(_T("Error   %d,RegisterDevice!"),   GetLastError())   ;
        if   (m_hDev   ==   0)
        {
                MessageBox(sErr,   _T("ERROR"))   ;  
        }
}

解决代码:
在注册之前,我先进行了查找,不知和这个有没有关系!

void   BtFind(CListCtrl   &list)
{
int   iRet   =   0   ;
WSAQUERYSET   wsaq   ;
HANDLE   hLookup   =   0   ;

HCURSOR hCurs;
hCurs   =   GetCursor()   ;
SetCursor(LoadCursor(NULL,   IDC_WAIT))   ;

ZeroMemory(&wsaq,   sizeof(wsaq))   ;
wsaq.dwSize   =   sizeof(wsaq)   ;
wsaq.dwNameSpace   =   NS_BTH   ;
wsaq.lpcsaBuffer   =   NULL   ;

iRet   =   WSALookupServiceBegin(&wsaq,   LUP_CONTAINERS,   &hLookup)   ;
if(iRet   !=   0)
{
TCHAR   tszErr[32]   ;
iRet   =   WSAGetLastError()   ;
StringCchPrintf(tszErr,   32,   _T("Socket   Err:   %d"),   iRet)   ;
AfxMessageBox(tszErr)   ;
}

LPWSAQUERYSET   pwsaRet   =   0;
CHAR   buf[5000]   ;
DWORD   dwSize   =   0   ;
for(;;)
{
pwsaRet   =   (LPWSAQUERYSET)   buf   ;
dwSize   =   sizeof(buf)   ;
ZeroMemory(pwsaRet,sizeof(WSAQUERYSET))   ;
pwsaRet-> dwNameSpace   =   NS_BTH   ;
pwsaRet-> lpBlob   =   NULL   ;

iRet   =   WSALookupServiceNext(hLookup,   LUP_RETURN_NAME   ¦   LUP_RETURN_ADDR,  
&dwSize,   pwsaRet)   ;

if(iRet   !=   0)
{
iRet   =   WSAGetLastError()   ;
if(iRet   !=   WSA_E_NO_MORE)
{
TCHAR   tszErr[32]   ;
iRet   =   WSAGetLastError()   ;
StringCchPrintf(tszErr,   32,   _T("Err:   %d"),   iRet)   ;
AfxMessageBox(tszErr)   ;

}
//   finished
break   ;
}
//   add   the   name   and   address   to   the   list
list.InsertItem(0,   _T(""));
list.SetItem(0,   0,   LVIF_TEXT,   pwsaRet-> lpszServiceInstanceName,   0,   0,   0,   0   )   ;  
BT_ADDR   b   =   ((SOCKADDR_BTH   *)   pwsaRet-> lpcsaBuffer-> RemoteAddr.lpSockaddr)-> btAddr   ;
ULONG   channal   =   ((SOCKADDR_BTH   *)   pwsaRet-> lpcsaBuffer-> RemoteAddr.lpSockaddr)-> port   ;
CString   szItem   ;
szItem.Format(_T("%x"),   b)   ;
list.SetItem(0,   1,   LVIF_TEXT,   szItem,   0,   0,   0,   0   )   ;
szItem.Format(_T("%d"),   channal)   ;
list.SetItem(0,   2,   LVIF_TEXT,   szItem,   0,   0,   0,   0   )   ;
}
WSALookupServiceEnd(hLookup)   ;
SetCursor(hCurs)   ;
}

/***********************************************************************
FUNCTION:GetBA
PROTOTYPE:int   GetBA   (WCHAR   *pp,   BT_ADDR   *pba)  
PURPOSE:Form   the   BDADDR   in   a   way   the   PORTEMUPortParams   structure   understands.
***********************************************************************/
int   GetBA   (WCHAR   *pp,   BT_ADDR   *pba)  
{
        //   Bump   pointer   through   any   leading   spaces
int   i   =   0   ;
        while   (*pp   ==   '   ')
                ++pp;
        for   (i   =   0   ;   i   <   4   ;   ++i,   ++pp)  
        {
                if   (!iswxdigit   (*pp))
                return(FALSE);
                int   c   =   *pp;
                if   (c   > =   'a')
                c   =   c   -   'a'   +   0xa;
                else   if   (c   > =   'A')
                c   =   c   -   'A'   +   0xa;
                else   c   =   c   -   '0';
                if   ((c   <   0)   ¦ ¦   (c   >   16))
                return(FALSE);
                *pba   =   *pba   *   16   +   c;
        }
        for   (i   =   0;   i   <   8   ;   ++i,   ++pp)  
        {
                if   (!iswxdigit   (*pp))
                return(FALSE);
                int   c   =   *pp;
                if   (c   > =   'a')
                c   =   c   -   'a'   +   0xa;
                else   if   (c   > =   'A')
                c   =   c   -   'A'   +   0xa;
                else   c   =   c   -   '0';
                if   ((c   <   0)   ¦ ¦   (c   >   16))
                return(FALSE);
                *pba   =   *pba   *   16   +   c;
        }
        if   ((*pp   !=   '   ')   &&   (*pp   !=   '\0'))
        return(FALSE);
        else
        return(TRUE);
}

HANDLE   RegisterCom(int   channel,   BT_ADDR   ba,   int   n)
{
//   注册服务
PORTEMUPortParams   pp;  

memset   (&pp,   0,   sizeof(pp));
pp.flocal   =   TRUE;
pp.channel   =   channel   &   0xff;
pp.device   =   ba   ;
pp.uiportflags   =   RFCOMM_PORT_FLAGS_AUTHENTICATE   ;

HANDLE   hDev   ;
hDev   =   RegisterDevice(_T("COM"),n,_T("btd.dll"),(DWORD)&pp);  
if(hDev   )  
AfxMessageBox(_T("Register   Success!"))   ;
else
AfxMessageBox(_T("Register   Fail!"))   ;
return   hDev   ;
}
相关文章:
没有相关文章
 

站点地图 | 加入收藏 | 联系站长 | 广告服务 |
QQ:280529124  Tel:0592-8271361 辽ICP备05021703号