Smartphone 蓝牙虚拟串口, 注册服务失败

wangxin_ch 2007-09-18 01:20:04
标题: 蓝牙虚拟串口, 注册服务失败

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")) ;
}
}

可是我看网上的类似代码,都说这里会成功,不知什么原因,请各位大大指点一下!
...全文
1489 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
wnqgz 2010-12-13
  • 打赏
  • 举报
回复
我注册成功了,可是打开串口的时候打不开,是怎么回事呀?
macklau 2010-04-01
  • 打赏
  • 举报
回复
感谢lz分享自己的成果!
spenserleetw 2008-11-29
  • 打赏
  • 举报
回复
請問如何解決 我注册了com5,但却打不开,不知道怎么回事,错误代码55意思是指定的网络资源或设备已不可用。各位有见过这种情况的吗?
mayang_117 2008-04-02
  • 打赏
  • 举报
回复
已经解决了,呵呵
mayang_117 2008-04-01
  • 打赏
  • 举报
回复
我注册了com5,但却打不开,不知道怎么回事,错误代码55意思是指定的网络资源或设备已不可用。各位有见过这种情况的吗?
xiangding 2008-03-12
  • 打赏
  • 举报
回复
学习,我也在弄这个。但注册服务一次成功
marszszzl 2007-11-09
  • 打赏
  • 举报
回复
mark
dyw 2007-10-31
  • 打赏
  • 举报
回复
感谢lz分享自己的成果!
wangxin_ch 2007-10-31
  • 打赏
  • 举报
回复
解决代码如下:
在注册之前,我先进行了查找,不知和这个有没有关系!

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 ;
}
wangxin_ch 2007-10-31
  • 打赏
  • 举报
回复
经过多次尝试,终于解决了注册服务的问题.
wangxin_ch 2007-10-14
  • 打赏
  • 举报
回复
还是没有答案!
wangxin_ch 2007-09-21
  • 打赏
  • 举报
回复
ok, 我试一下
wangxin_ch 2007-09-19
  • 打赏
  • 举报
回复
已经试过了,还是不行,没天理呀!
哪位大哥有成功过,进来看看吧!
IMGGTOO 2007-09-19
  • 打赏
  • 举报
回复
查找服务时,加一步这个:
nResult = WSALookupServiceNext(hLookUp,0,&dwSize,pwsaResult);
if(ERROR_SUCCESS == nResult)
{
unsigned char cChannel = 0;
if(ERROR_SUCCESS == FindRFCOMMChannel(pwsaResult->lpBlob->pBlobData,pwsaResult->lpBlob->cbSize,cChannel))
nResult = cChannel;
}

FindRFCOMMChannel,这个函数,网上有实现的代码,你找找看!我就是这么做的,已经实现了蓝牙连接和发送数据。
wangxin_ch 2007-09-18
  • 打赏
  • 举报
回复
有过还有
pp.channel = RFCOMM_CHANNEL_MULTIPLE ;// the next available channel
这个channel 是指下一下可用的通道!
wangxin_ch 2007-09-18
  • 打赏
  • 举报
回复
我用的也是多普达的手机(cht 9000),明天试了给大家答复!
谢谢大家的回答!
IMGGTOO 2007-09-18
  • 打赏
  • 举报
回复
通过虚拟串口连接蓝牙设备需要3个条件:
1.被连接设备的地址;
2.被连接设备提供的连接通道;
3.本机的一个虚拟COM;

就我自己在这方面的经验来说,第二个条件最难得到。看了楼主自己的做法以及你提到的别人的做法,这个连接通道,都是直接指定的,这个,我不敢说有什么问题,但是至少我这样做,没有成功过。

如果有兴趣,你可以做这样一个测试:获取设备地址之后,注册虚拟端口时,循环测试通道和COM口的每一种组合。因为,连接通道是在1-31之间的,而虚拟COM口,也就在0-9之间(实际上,我用的几款多谱达的手机,只支持COM6和COM7)。这样下来,组合情况也没多少:
PORTEMUPortParams pp;
memset(&pp,0,sizeof(pp));
pp.device = btAddr;
pp.uiportflags = RFCOMM_PORT_FLAGS_REMOTE_DCB;
for(int m = 1;m <= 31;m++)
{
pp.channel = m & 0xFF;
BOOL bSuccess = FALSE;
for(int n = 0; n <= 9; n++)
{
hDev = RegisterDevice(_T("COM"),n,_T("btd.dll"),(DWORD)&pp);
if(hDev )
{
bSuccess = TRUE;
BREAK;
}
}
if(bSuccess )
break;
}

看看能否注册成功。
dyw 2007-09-18
  • 打赏
  • 举报
回复
暂无开发环境,只能分析。

Trouble with several conection in bluetooth witn virtual port
https://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1664678&SiteID=1&pageid=0#1664678

另一种做法:通过配置Setting,绕过这个问题
http://www.teksoftco.com/forum/viewtopic.php?t=9
wangxin_ch 2007-09-18
  • 打赏
  • 举报
回复
tstwk:
Bluetooth is integrated in the T-Mobile MDA II ... http://msmobiles.com/article.php/54.html (most of the articles I found were german) It seems to have features like the IPaqs. Addionately it has a fantastic camera feature (and is a cell-phone : ). The IPaq (Bluetooth-PPC) might be an alternative for us, but if the problem persists it wouldn't make much sense.

Is it possible to use the wsa sockets on the IPqaw without using the WidComm stack? The thing I don't understand is - Windows Mobile 2003 has a stack for Virtual COM Ports and the manufacturer doesn't implement it, so that no generic software can use it?!?

I have already contacted the manufacturer, but still no answer. Maybe the MDA II has third party bluetooth stuff, too. If telekom answers, I'll know.

Do you have any further ideas?

The device you are working on might be an alternative, too... Currently we are selling yakumo deltas with leica disto pro 4 lasermeasurement devices. But the cable is disturbing while working with the laser device... http://www.mobilaufmass.com/Mobilaufmass/Das_Mobilaufmass_im_Einsatz/Steuerung.htm (unfortunately the site is german)

Thanks for your endurance.

Thomas


11.07.2003 at 12:41PM PST, ID: 9703896

cindy_k:
Unfortunately I don't know the widcomm stack so I don't know if you can get around buying their SDK.

The device with the embedded bluetooth that I am using comes from Symbol Technologies, Inc. but is currently not for sale. I am doing some evaluation development work on it.

However, Symbol sells this device:
http://www.symbol.com/products/mobile_computers/mobile_ppt8800_ppc2003.html
It has a Socket Bluetooth radio in it and I know Symbol will supply the SDK to their customers, so they will be able to write their own code to use the radio.

It's no problem. I understand such frustration with new product. Documentation? What documentation!?!
Trust me, you don't want to write an Service Discovery App using the Microsoft Stack. It was difficult, because the documentation provided with the APIs was extremely lacking and wrong.

Good Luck!!
Cynthia

Accepted Solution


03.01.2004 at 09:00PM PST, ID: 10492484

cerebralpc:
Hi tstwk,
You wrote..

"Is it possible to use the wsa sockets on the IPaq without using the WidComm stack? The thing I don't understand is - Windows Mobile 2003 has a stack for Virtual COM Ports and the manufacturer doesn't implement it, so that no generic software can use it?!? "


Do you ever resolve this issue?
I'm currently programming an application for an IPaq 2210 and although I've managed to get 1 device connected and transfering data to the IPaq I actaully need to connect 2 Bluetooth devices. Therefore I really need to create VIRTUAL ports however as you suggest VIRTUAL ports don't come with this stack - sigh.

Another issue wsa that I had to set the IPaq as the master device - although the connection would appear to be made if the IPaq was the slave no data would actually be sent.

I've emailed the maker widcomm.com but never got satisfactory replys.


06.03.2004 at 01:52AM PDT, ID: 11220254

Synth2:
cindy_k

Your application needs both Bluetooth devices to be paired or it's not necessary because you implement an SDP query? maybe it's implemented by default?
I have a Dell Axim X3 and i want to make a connection between it and a Pc.
Dell Axim X3 Bluetooth hardware manufacturer is Widcomm.Won't I be able to create VIRTUAL COM from my Pocket PC if i don't get the Widcomm SDK?


06.03.2004 at 02:22AM PDT, ID: 11220417

tstwk:
We found out, that the RFCOMM stuff simply doesn't work on the MDA II. It's a himalaya device, all of these devices are showing the same peculiarity.
They send us 3 different version of their PPC, but without any progress within the implementation of RFCOMM. There's also a logical error in the user interface: You can "find and connect" the device to the pocket pc in Windows Mobile 2003 and you can set bluetooth com-ports (COM4 output, COM5 input), but there is no setting to combine the com-port with the device?!?

Because of this, you can never connect these Pocket PCs to a RFOMM device (GPS etc.). *g*

Dell Axim X3 question: Now we are using the WIDCOMM stack (only MFC-support I think) for RFCOMM, which has its errors, but with some workarounds you can wangle it. In my opinion the WIDCOMM Support is miserable.
I'm relatively sure, that without buying the WIDCOMM stack you won't be able to connect the devices. But maybe it's possible... There are many extra features for searching devices etc. int the stack, so that the users do not need to establish the connection in the widcomm settings.
Because of some errors the newer WIDCOMM PPCs have revised SDK-DLLs in their ROM, maybe you could use them (BtCoreIf.dll BtSdkCE30.dll)?!? Normally you must install them yourself. It is hard enough with the mfc classes. But it's not, that you pay for nothing...




06.13.2004 at 02:57AM PDT, ID: 11299287

myopic:
Having similar trouble on WinCE 4.2. I haven't written my own app, just used the sample ones that came with it. I may be able to shed some light on a little of what was said before.

The registry settings under
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Bluetooth\Transports\BuiltIn\]
Are for bluetooth transports. The transport is the way the Host (WinCE) talks to the bluetooth device. Eg. Serial/USB/PCMCIA etc. They are at a lower level then what is trying to be done here.

I've heard that it's possible to get this working but I can't find any resources on the net anywhere.
wangxin_ch 2007-09-18
  • 打赏
  • 举报
回复
tstwk:
The serial devices return are COM 1,2,3,6,8 and 9. So there are "free" Ports.
BUT.... the key [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Bluetooth\Transports\BuiltIn\2] you mentioned above is absend!

Only [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Bluetooth\Transports\BuiltIn\1] with bthusb.dll and [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Bluetooth\Transports\BuiltIn\3] with bthcsr.dll (concerning COM6).

I think this is a pretty obvious error. But unfortunately I found out nothing about this registry key. Would you be so kind to give me further informations or a list of sources about my absend [...\BuildIn\2] registry key, so i have the chance to find out more about it. Just to reproduce the problem. Is it the btd.dll mentioned in [...BuiltIn\2]?
Am I right that the hardware manufacturer is responsible for the development (DLLs and registry-settings) of its Win CE Platform?

If I have a blade of grass to hang on, I'll close this question with a clear conscience : )



11.06.2003 at 10:21AM PST, ID: 9695700

cindy_k:
>>Am I right that the hardware manufacturer is responsible for the development (DLLs and registry-settings) of its Win CE Platform?
Partially. It depends of whether the Radio is embedded into the device or not. If its embedded than the Device manufacturer would be the one responsible for drivers and registry settings. If you are adding a bluetooth compact flash card, then the card manufacturer would be responsible.

Can I ask what Bluetooth card you are using?
For example, I know that Socket offers an SDK to go with their card but they don't use the Microsoft Stack for their card.

I am currently working on a device that my company is building, we have our own internal Bluetooth chipset and we are using the Microsoft Stack. I got those registry settings from that device.

I have a CE device here with a Socket BT compact flash card in it. I just looked at it's registry settings and they are different.
Because it's in the Compact Flash slot it doesn't have a Serial port assigned to it, like my other device does.
It has this setting but doesn't have a 2 or 3.
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Bluetooth\Transports\BuiltIn\1]

I can register a port with the Socket card, but I am having trouble opening the com port. Of course I can create the connection with the Socket SDK with no problem.
They use the Draker Stack.

I just looked into the IPAQ's I have with Bluetooth in them and they use the WidComm stack. You have to get the SDK to use that stack from them. Looks like they are charging for it. www.widcomm.com
I can't register a com port on my IPAQ for bluetooth either. I'm guessing they do this so you are forced to buy their SDK.

Hope this helps some.
Cynthia



加载更多回复(7)

7,656

社区成员

发帖
与我相关
我的任务
社区描述
Windows Phone是微软发布的一款手机操作系统,它将微软旗下的Xbox LIVE游戏、Zune音乐与独特的视频体验整合至手机中。
社区管理员
  • Windows客户端开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧