Anil Dhawan - Look at Bluetooth programming on Windows Mobile
- Posted: Feb 17, 2005 at 5:29 PM
- 140,703 Views
- 50 Comments
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Right click “Save as…”
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums,
or
Contact Us and let us know.
Follow the Discussion
Oops, something didn't work.
What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in. You need to be signed in to Channel 9 to use this feature.What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in and view them all on your notifications page.sign up for email notifications?
I'd love to play around with that code-- is there somewhere you can post it?
John
Nice video, is it possible to transform my smartphone to a Remote Car Starter?
First, is there many Pocket PC devices in the wild that use Microsoft's bluetooth stack? Last time I checked most devices used Widcomm's software as yours was unfinished.
Second, how do the two compare on the desktop and devices? Is there anwhere I can see a comparison of supported profiles or can you just tell me?
I would love to develop using Microsoft's stack as Widcomm's locked me into eVC++ and was damn expensive. And don't get me started on the documentation.
Cheers,
Andrew
Cool video.
I've got a very similar application over at http://mtaulty.com/blog/archive/2004/12/07/947.aspx
which controls PowerPoint or any other "foreground" application from the SmartPhone.
The difference would be that the code I've got is C# .NET code rather than embedded C++ although there's a little bit of managed C++ in there as well.
The application's there for download as is the source code.
Thanks,
Mike.
The MS stack is more common on Smartphones and a number of the newer Pocket PC Phone Edition devices. However the big players in the traditional Pocket PC space - HP, Dell etc use Widcomm.
On Windows Mobile the MS stack supports SerialPort, ObjectPush, Dun, Headset/Handsfree profiles
On Windows XP:- SerialPort, ObjectPush, Dun, Panu, HardCopyCableReplacement (Printing), HID (Keyboard/Mouse)
Peter
I should have been clearer ObjectPush is OBEX. There are two OBEX profiles in the Bluetooth specification - ObjectPush which is used to send a file to another device - used to send a contact for example. The other is OBEX FTP where the device can host an FTP style filestore which other devices can browse and download from. This is available in the Bluetooth modules for CE.NET 4.2 but not included in Windows Mobile. You can host your own OBEX FTP service if you process the appropriate OBEX headers e.g. using a BluetoothListener. The Widcomm stack has support for this profile and offers both the ability to share files in this way and a browser application to view files on a remote device.
Peter
Anyone know any (cheap ;p, yeah I'm a student) windows enables phones in Holland?
It is possible to implement an OBEX FTP service in managed code on the Microsoft stack. In testing I was able to receive and respond to connection requests, what I haven't done though is wrapped all the OBEX commands, but they are documented in the Bluetooth spec.
One of my Blog readers put together a working sample on the OBEX Push side but it illustrates how OBEX packets are used - http://www.gatefold.co.uk/obex/
Peter
I played around with building this sort of an app, but I can't get the bluetooth to work on the desktop side.
I think the main problem is that I can't get the MS bluetooth stack to run on my adapter (Zonet ZUB6100) and the APIs don't seem to work with the WIDCOMM stack provided by zonet. (BluetoothEnableDiscovery always returns false.)
Does anyone want to recommend a good usb bluetooth adapter compatible with XP SP2's bluetooth stack?
John
Can you please post C++ code you were showing in your demo?
Thanks
MSx86
Hi,
I am interested in getting the c++ code.. Can somebody please post the code which Anil showed in his demo (c++) version?
Thanks in advance...
By the way, looking at Anil's code: - is anyone out there who would know how to simulate a LONG press of the "Hook Up" (VK_F3) key on the Smartphone, so that the Speakerphone gets started? keybd_event() is OK to take the hook off, but it won't launch speakerphone mode.
Is there some other way to programmatically activate Speakerphone mode?
I'm writing an app that enables the physically handicapped to operate a smartphone.
Thanks for a hint
Phil
Peter
Which seems strange. Looking at the way the Keyboard drivers are implemented, even they rely on keybd_event.
If one starts a KeyTracker program, the VK_F3 gives you nothing on pressing the key, but fires a KEYUP and a KEYDOWN on releasing it. If one holds it down LONG, it won't give you anything at all, unless a call is active. In that case, it launches Speakerphone mode.
Strange but true...
I have asked this question so many time. But this forum is very slow. I will repeat my request again. Can somebody post the VC++ code and the c# version of the same code?
Thanks
MSx86
Hi
Did you manage to get the code? If so can I have it?
Thanks
Nandu,
I haven't received the code and I am still waiting.
MSx86
Nandu
hey I am also interested in the code please if anybody recives it post it to me
my email is sultan.alsharfi@gmail.com
I'm afraid I can't help with the eVC++ code mentioned in the video, but a close equivalent in C# is available here:-
http://www.peterfoot.net/BluetoothRemoteControl.aspx
Peter
can you please point me some online tutorial/manual or some literature that can help me with bluetooth programming ?
I'm a newbie to Windows Mobile programming so any hints or recommendations will be appreciated.
Thanks
I have a working library built around sockets which works on both desktop and Windows CE (Microsoft stack only) you can download the source and a sample chat application for each platform from this blog entry:-
http://www.peterfoot.net/Bluetoothv14.aspx
Peter
Assuming you are talking about .NETCF development you'll find everything required in this package:-
http://www.peterfoot.net/BluetoothV14.aspx
Including source (C#), sample projects, class documentation. It also closely follows the IrDa classes in .NETCF so should be easy to pickup. It'll work on XP or Windows CE/ Windows Mobile so long as you are using the Microsoft stack (not from a third-party such as Broadcom).
Peter
If you are interested in learning more about Bluetooth on Windows Mobile, swing by my blog at: http://blogs.msdn.com/anil_dhawan .
Where is a good source of information for a developer who wants to develop at a lower level using Bluetooth on WM5?
I am actually working on a project that detects when a paired BT device is NOT in range and I am getting seriously stuck and having a difficult time finding viable resources.
I saw this video for creating bluetooth applications. It's very nice.
I have couple of questions which are different from your sample.
I have a simple requirement in Windows Mobile(Pocket PC 4.x, 5.x) Bluetooth.
The aim is to find if a paired bluetooth device is nearby(within range).
I thought I could write a simple Winsock application to do it. Basically do a service discovery. But the problem is the Querry set gives me only the name in "lpszServiceInstanceName". I would like to get the Mac id (uniqure bluetooth id) for the device I discovered.
1. Could you please let me know how I can get it?.
Else
2. If I know the mach id of the paired device (I can get it from the registry), How can I directly inquire if this device is deiscoverable or not? This is to avoid SDP.
Note : I don't have a connection to this device. I just have a pairing. So i won't be able to use winsock "connect" calls since they require a port (channel 1 -32) or Service specific UUID. I don't have both since I am just trying to find if a paired device is within range.
3. Another "MAJOR" problem is Some(lot
I hope you can give me some suggestions.
Thanks very much
Ganesan
That was a fantastic demo... I was just trying to know that does Windows CE has any AT command handler so that you can connect any headset to ur notebook or PC... in the sense an Audio Gateway
regards
Soumya
1.- The "Server side" or the laptop side of the app is a service waiting for signals sent by the bt smartphone? or a simple wile(true) kind of code?
2.- I dont have a smart phone, but i do have a Pocket PC, BUT it has windows Mobile 2003 se, i guess Serial ports programming should work aswell on my device...that wrapper for bluetooth programming that you made its ONLY for WM 5.0?
3.- is there a bluetooth api for windows mobile 2003 that i could use, or i should just stay with com ports?, plus i dont think my Dell Axim X50 runs windows bluetooth stack to use openetCF.
thanks
However, I don't know how the classes work together to actually create a bluetooth application. Can anyone show me how I can put together a very simple app to pair and connect with a headset, and capture the incoming sound to a file in VS2005 (any part of the app would be helpful)
btw the link to download Microsoft Embedded Source Tools For Bluetooth Technology is http://msdn.microsoft.com/embedded/usewinemb/ce/sharedsrccode/west/default.aspx
it was built on:
eVC++ 4.0 SP2 + windows mobile 2003 sdk running on Pocket PC Dell Axim X50.
Visual Studio 2005 (c++) running on my laptop with a bluetooth dongle.
-----------------------PPC portion:
HANDLE fileHandle;
int OpenSerialPort();
int CloseSerialPort();
int ProcessKey(const int key);
void ConfigureSerialPort()
{
COMMPROP commProp;
memset(&commProp, 0, sizeof(COMMPROP));
GetCommProperties(fileHandle, &commProp);
DCB commDCB;
BOOL fSuccess = FALSE;
memset(&commDCB, 0, sizeof(DCB));
if(GetCommState(fileHandle, &commDCB)) {
if(commProp.dwSettableParams && SP_BAUD) {
commDCB.BaudRate = BAUD_9600;
commDCB.fParity = PARITY_NONE;
commDCB.StopBits = ONESTOPBIT;
commDCB.ByteSize = 8;
fSuccess = SetCommState(fileHandle, &commDCB);
}
}
if(!fSuccess)
MessageBox(NULL, TEXT("Could not modify the port's settings"),
TEXT("Error!"), MB_OK);
}
int OpenSerialPort()
{
if (fileHandle == NULL){
fileHandle = CreateFile(_T("COM7:"), GENERIC_WRITE ,0,NULL,OPEN_EXISTING,0,NULL);
if (fileHandle == INVALID_HANDLE_VALUE){
MessageBox(NULL,L"No se pudo crear el COM 7",L"Aviso",MB_OK);
fileHandle = NULL;
return 0;
}
}
ConfigureSerialPort();
return 1;
}
int CloseSerialPort()
{
if (fileHandle != NULL){
CloseHandle(fileHandle);
fileHandle = NULL;
return 1;
}
return 0;
}
int ProcessKey(const int key)
{
DWORD numwritten = 0;
if ((key >= 37 && key <= 40) || (key == 13))
{
WriteFile(fileHandle,&key,sizeof(key),&numwritten,NULL);
}
return 1;
}
//WINAPI STUFF..... in this next method
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
//add the this code.
case WM_KEYUP:
lResult = ProcessKey(wParam);
break;
----------------------------------------PC portion:
#include <stdio.h>
#include <windows.h>
//conection to Serial PORT
HANDLE fileHandle;
/* Methods */
int OpenSerialPort();
int ConfigureSerialPort();
int CloseSerialPort();
void SendKey(const BYTE vKey);
int ReadMsg();
//Open it up
int OpenSerialPort()
{
WCHAR comPort[30];
wsprintf(comPort,L"COM%d",8);
if (fileHandle == NULL)
{
fileHandle = CreateFile(comPort, GENERIC_READ,0,0,OPEN_EXISTING,0,0);
if (fileHandle == INVALID_HANDLE_VALUE)
{
printf("ERROR: Cant open port [%s]\n",comPort);
fileHandle = NULL;
return 0;
}
}
printf("PUERTO [%s] CREATED!\n",comPort);
//configure port
ConfigureSerialPort();
return 1;
}
//Configurar el Puerto Serial
int ConfigureSerialPort()
{
DCB dcb;
FillMemory(&dcb,sizeof(dcb),0);
dcb.DCBlength = sizeof(dcb);
WCHAR buffer[100];
wsprintf(buffer,L"baud=9600 parity=N data=8 stop=1");
if (!BuildCommDCB(buffer,&dcb)){
printf("\tERROR: No se pudo crear DCB!\n");
return 0;
}
printf("\tDCB HECHO!\n");
if (!SetCommState(fileHandle,&dcb)){
printf("\tERROR: No se pudo cambiar el estado del Puerto COM\n");
return 0;
}
printf("\tESTADO del COM SET!\n");
if (!SetupComm(fileHandle,1024,1024)){
printf("\tERROR: No se pudo SET la cola a 1024\n");
return 0;
}
printf("\tBT Stack Ready!\n");
COMMTIMEOUTS cmt;
cmt.ReadIntervalTimeout = 1000;
cmt.ReadTotalTimeoutMultiplier = 1000;
cmt.ReadTotalTimeoutConstant = 1000;
cmt.WriteTotalTimeoutConstant = 1000;
cmt.WriteTotalTimeoutMultiplier = 1000;
if (!SetCommTimeouts(fileHandle,&cmt)){
printf("\tERROR: No se pudo SET los timeouts al COM");
return 0;
}
printf("\tTimeouts SET!\n");
printf("............................................................\n");
return 1;
}
//Cierre del Puerto COM
int CloseSerialPort()
{
if (fileHandle != NULL){
CloseHandle(fileHandle);
fileHandle = NULL;
printf("COM PORT CERRADO\n");
return 1;
}
return 0;
}
//Enviar las teclas al computador
void SendKey(const BYTE vKey)
{
keybd_event(vKey,0,0,0);
keybd_event(vKey,0,KEYEVENTF_KEYUP,0);
}
//Lectura de los mensajes que llegan por el puerto COM
int ReadMsg()
{
unsigned int * dataBuff = new unsigned int[100];
DWORD numRead = 0;
//lee 4 bytes desde COM8 al buffer
ReadFile(fileHandle, dataBuff, 4, &numRead, NULL);
//obtengo el valor real desdeel buffer
int val = dataBuff[0];
printf("key: %d",val);
switch(val)
{
case 13: //enter
SendKey(0xA8);
break;
case 37: //izq
SendKey(VK_LEFT);
break;
case 38: //arriba
SendKey(VK_UP);
break;
case 39: //der
SendKey(VK_RIGHT);
break;
case 40: //abajo
SendKey(VK_DOWN);
break;
default:
break;
}
return 0;
}
int main (int argc, char** argv)
{
if (!OpenSerialPort())
return -1;
//replace it with a nice thread
while (!ReadMsg());
CloseSerialPort();
system("pause");
return 1;
}
I Hope it works and that you all can use it, send me an email if you want the whole proyects,, (hoofmen@hotmail.com)
cheers!
I'd also like to know how all the classes work together. I have been looking at the code for days and got nothing.
In that video, Anil creates an object called btsCore of type Core. He uses the namespace "BluetoothSystem" which I cannot find anywhere. The methods he uses are similar to those at the Windows Embedded Source Tools for Bluetooth technology (http://msdn.microsoft.com/embedded/usewinemb/ce/sharedsrccode/west/default.aspx ) but they aren't the same.
All I want to be able to do is establish a simple connection using the SPP to transfer some data using C#, thats all, nothing more. Everyone seems to ask for help on forums and nobody ever seems to get any. Anil, if you can read this, please point us in the right direction...you seem to be the only soul out there who knows anything about this topic. I would GREATLY appreciate it.
P.S. I am a design engineer at Rockford Fosgate and maybe we can barder some tech support for audio equipment
I totally recommend you to use 32feet.net wrapper for programming over bluetooth, i have been trying the other libraries and none of them are as simple as this one, oh also opennetcf its good.
i am also frustrated about finding bluetooth programming help, Anil seems to be too busy to answer questions even when in most videos he advertises channel9 to be a place in where you get to talk to him.
What really gets me is that the Embedded Shared Library looks pretty simple but has fundamental flaws like requesting an 8-byte Bluetooth address!! I mean if they need and extra 2 bytes for interoperability with some other function, then they need to document the format of those 8 byts in their code like...
bt_addr[0 ~ 5] Bluetooth address bytes
bt_addr[6] 0xNN
bt_addr[7] 0xNN where 0xNN describes XXXXXX
I'd assume that they do realize the BT address is only 6 bytes, so those extra 2 must do something!
It's frusterating to troubleshoot this type of stuff when there's not enough documentaion.
maybe you know, In the BluetoothEndpoint class, when you create a socket and you pass the BT address and GUID, does it need to use the serialize method and join the data? If so, the serialize function is screwed up too because of the 8 byte address. I tried looking for the documentation of the socketaddress to see if it was correct but I can't find anything.
Looking over the info at 32feeet.net makes me want to cry...it looks sooooo easy with their tools. Why can't they work with VS2005???? [C]
[edit] After looking deeper into that site, there may be some hope in using 32feet with VS2005 as long as I only write PPC applications (which is all I need anyways)
both apps using 32 feet lib, when you download the library there are many examples that come with the visual studio solution file (version 8, that is VS 2005).
I al so have no idea why those 2 extra bytes, maybe is a hamming code for validation
if you have more advanced questions talk to peter foot, he is the author for the 32feet lib, and he does replies to emails.
good luck
These tools are wonderful, thanks for the info, which led me to continue this avenue!
I've come up with a software for all of this, while I was looking last year for some ways for using bluetooth to remote control applications
After all, bluetooth is like any other transport
Mostly in C# on Mobile side, full C# on server side.
See:
http://jaylee.org/remotecontrol
To check it out...
Hello,
I’am a student and I need information very fast
Can you help me with some bluetooth questions?
What is the difference between RFCOMM and SPP?
How can you describe a bluetooth stack?
If I program my application with widcomm I need to install widcomm, I can’t use a Microsoft stack.This is not a problem on a desktop because I can install MS of Widcomm but not on a PDA. Is this true? And why ?
Ok that’s all, I hope you can help me with it
Grtz Glenn
I am in a big mess and hope to get some help from here.The scenario is i need to develop a mobile 5.0 application in visual studio.net which will enumerate list of paired bluetooth devices.It have a bluetooth dongle connected to my desktop it uses widcomm stack.I need to test the application on the emulator as i dont have a pocket pc at the moment.I downloaded the Windows Embedded Source Tools for Bluetooth Technology tried the Tutorial # 1 available on the following URL http://channel9.msdn.com/wiki/default.aspx/Channel9.BluetoothDevelopment
When i deploy the application on the pocket pc emulator i get the following error - 'Error closing Key : registry exception was unhandled
A first chance exception of type 'Microsoft.WindowsMobile.SharedSource.Utilities.Registry.RegistryException' occurred in Microsoft.WindowsMobile.SharedSource.Bluetooth.dll'
I really dont know what to do,because there is absolutely no information like code samples to develop bluetooth application for mobile 5.0 using vb.net/c#.
The code samples on msdn and other websites are in C++ and uses windows CE which is definnitely not what i am looking for.I have researched left,right and centre for three days but in vain. [C][C][C][C][C]
My name is Adam and I am the one who posted the Tutorial 1 after the heading Tutorial 1 (probably created by microsoft) said coming soon for, I would say well over 6 months now.
The reason this code does not work for you is as you mentioned you have the WIDCOMM stack and you are using it on Windows Desktop.
The MicrosoftSharedSource for Bluetooth is designed for Windows Mobile 5.0 + and with a microsoft bluetooth stack.
Firstly you can switch to the microsoft bluetooth stack on your desktop by uninstalling any WIDCOMM bluetooth stacks. (you should see it in the control panel add/remove programs) and ensuring you have XP SP2 or higher.
Next you should download the code I ported across to Windows XP
http://www.dotnetcore.com/tutorial.aspx?id=9
Also does anyone know what happened to Anil, he seems to have dropped of the planet? (april 1st 2006 was his last blog entry)
I have to develop an application on Windows Mobile 2003 SE on a Dell PPC using CF.NET and Microsoft.WindowsMobile.SharedSource.Bluetooth.
When a try to list the paired BT devices, I get the same problem than yumnakhan, Registry.ClosingKey() throw an exception.
I am not sure of which BT stack is present (the BT controller is provided by Broadcom, maybe the stack too).
It's strange because all the information I found on internet says the 'Microsoft.WindowsMobile.SharedSource' works for Windows Mobile. It is not the case for 2003 SE?
What can I do to solve the problem?
Regards
math
I get the same CloseKey error.
Using a Mobile2003 with Widcomm.
Any way to fix it? Anyone gave a solution?
Thanks ofr an answer.
JMJ
Remove this comment
Remove this thread
close