Capturing Bluetooth events in Windows Mobile

I wrote on Sunday about writing Bluetooth code for Windows Mobile for a location-based reminder app I hacked together.

At the time, I wrote about two main approaches for finding nearby Bluetooth devices:

  • searching for discoverable devices
  • registering for notifications for when paired devices connect and

For the location-based reminder app, I went with the first approach, and played with how to search for discoverable devices.

This afternoon, I tried out the other way – writing an app that registers with the OS for connect and disconnect notifications from paired devices.

An app – locking a phone based on the proximity of the Bluetooth handset
A number of Linux-using friends at work use BlueProximity – which is a Linux app that you can use to lock your computer when your phone goes out of Bluetooth range.

I thought it’d be useful to write something similar for my mobile.

T-Mobile Ameo with LG Style-IMy needs are perhaps not very typical, so it’s probably worth adding a quick reminder about the gadgets I carry with me most often.

My mobiles-of-choice at the moment are the HTC Universal and HTC Advantage – neither of which are small, particularly the Advantage!

And I use a small and uber-light Bluetooth handset to make and receive calls.

When I’m at work, the tiny Bluetooth handset is normally clipped to the name-badge I wear around my neck. The rest of the time, it’s normally clipped to a shirt or in a pocket. So it’s pretty much always with me.

My actual phone is rarely in a pocket. Partly because I’m almost constantly either referring to or jotting notes in it, and partly because it’s too big to fit in a pocket unless I’m wearing combats! 🙂 As the phone is normally in my hands, I do have a habit of leaving it in places… particularly at other people’s desks at work.

Something that would lock it when I walk away (with my Bluetooth handset) would actually be pretty useful. And a good excuse to try out capturing Bluetooth events.

Registering for Bluetooth notifications
The code for this is actually pretty simple – there is a Windows API called RequestBluetoothNotifications which lets you register for Bluetooth events.

I found a good MSDN article for Windows Mobile: “Handling Events from the Bluetooth Stack” which provides a sample and walks through the steps provided. In short, you just need to create a message queue, then register for notifications giving a handle to your queue. Then you spawn off a new background thread that sits blocked waiting for Bluetooth notification messages to start arriving on the queue.

As I’m getting quite into the whole p/invoking thing, I thought I’d have a go at wrapping it up in a C# class for a .NET application. It’s fairly simple – there aren’t any strings in the Bluetooth structures I’m using here, so the mapping was all straightforward. If it is any use to anyone else, the interesting bit of the source is here. (Usual disclaimers apply… that is, don’t blame me if it turns out to be gibberish. I was only having a quick play with this stuff.)

After a quick bit of hacking, it seems to work (download the CAB here). It’s not quite finished… I’ve not worked out how to get much useful information out of the Bluetooth event messages other than the event type (e.g. connect or disconnect). I haven’t worked out how to find out which device has connected or disconnected! (It’s not a problem for my immediate purposes – my phone is only paired with one Bluetooth device anyway, but it’s something I want to come back to.)

Other Bluetooth app ideas
There are other things I want to try with Bluetooth code… ideally, I’d love to buy a few dozen Bluetooth beacons and scatter them around the site where I work, Hursley Park. Then I could use them as a sort of internal GPS around work. If people’s phones would report their location to an internal server, you could even use this to find people at work. Or use it in conjunction with the intranet map site to provide directions when you can’t find a meeting room.

Or I could use it to detect when I get in my car and when I get home… not sure what I’d do with those events, but it might be useful.

The key bit will be what sort of power drain these apps – location-based reminder and this auto-locking – will have. It will be interesting to see whether it has a noticeable effect on the battery life of my phones over the next few days.

Tags: , , , ,

6 Responses to “Capturing Bluetooth events in Windows Mobile”

  1. Benoist says:


    Do you have this source code in C++? If so, would you mind to share it?

    Thank you for your help.

  2. dale says:

    @Benoist – I’ll contact you offline.

  3. Marino van der Heijden [NL] says:

    Cool stuff. This really inspires me to do something with bluetooth and mobile. Hope to find some time soon! 🙂 Thanks

  4. […] location sharing is something that I keep coming back to: from finding where my phone is using GPS, Bluetooth, WiFi Access Points, GSM Cell Ids, using my own hand-rolled systems or newer services like Google […]

  5. Dan says:


    Looks like this is almost exactly what I’ve been looking for. I have been looking for a program that can launch a program (in my case, Mortscripts) to setup my phone for various profiles when it detects something around it. That is, at home, it will pickup a BT dongle there…which will set my “home” profile. When At work, the same. When I get to my car, the same, as in my car, I have a GPS unit with Handsfree dialing.

    I have poked through the registry, but these things do not seem to be presented there. So, with a program that can “see” what has registered with my phone (your option 2) I think this can be pulled off!

    What do you think?

  6. Tony says:

    Please give a C# example of how I can use this class to detect the Bluetooth connection event.