{"id":274,"date":"2008-05-30T17:05:17","date_gmt":"2008-05-30T17:05:17","guid":{"rendered":"http:\/\/dalelane.co.uk\/blog\/?p=274"},"modified":"2008-05-31T07:31:59","modified_gmt":"2008-05-31T07:31:59","slug":"capturing-bluetooth-events-in-windows-mobile","status":"publish","type":"post","link":"https:\/\/dalelane.co.uk\/blog\/?p=274","title":{"rendered":"Capturing Bluetooth events in Windows Mobile"},"content":{"rendered":"<p>I wrote on Sunday about <a href=\"http:\/\/dalelane.co.uk\/blog\/?p=270\" target=\"_blank\">writing Bluetooth code for Windows Mobile<\/a> for a <a href=\"http:\/\/dalelane.co.uk\/page.php?id=1049\" target=\"_blank\">location-based reminder app<\/a> I hacked together.  <\/p>\n<p>At the time, I <a href=\"http:\/\/dalelane.co.uk\/blog\/?p=270#more-270\" target=\"_blank\">wrote about two main approaches<\/a> for finding nearby Bluetooth devices: <\/p>\n<ul>\n<li> searching for discoverable devices\n<\/li>\n<li> registering for notifications for when paired devices connect and<br \/>\n    disconnect <\/li>\n<\/ul>\n<p>For the <a href=\"http:\/\/dalelane.co.uk\/page.php?id=1049\" target=\"_blank\">location-based reminder app<\/a>, I went with the first approach, and played with how to search for discoverable devices.  <\/p>\n<p>This afternoon, I tried out the other way &#8211; writing an app that registers with the OS for connect and disconnect notifications from paired devices.  <\/p>\n<p><!--more--><strong>An app &#8211; locking a phone based on the proximity of the Bluetooth handset<\/strong><br \/>\nA number of <a href=\"http:\/\/blog.garethj.com\/2008\/02\/16\/passwords-passwords-passwords\/#comment-12\" target=\"_blank\">Linux-using friends at work<\/a> use <a href=\"http:\/\/blueproximity.sourceforge.net\/\" target=\"_blank\">BlueProximity<\/a> &#8211; which is a Linux app that you can use to lock your computer when your phone goes out of Bluetooth range.  <\/p>\n<p>I thought it&#8217;d be useful to write something similar for my mobile.  <\/p>\n<p><a href=\"http:\/\/www.flickr.com\/photos\/dalelane\/490427507\/\" title=\"T-Mobile Ameo with LG Style-I by dalelane, on Flickr\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/farm1.static.flickr.com\/228\/490427507_bf6ce7ccee_m.jpg\" align=\"left\" width=\"240\" height=\"180\" vspace=\"10\" hspace=\"10\" style=\"border: thin black solid\" alt=\"T-Mobile Ameo with LG Style-I\" \/><\/a>My needs are perhaps not very typical, so it&#8217;s probably worth adding a quick reminder about the gadgets I carry with me most often. <\/p>\n<p>My mobiles-of-choice at the moment are the <a href=\"http:\/\/www.flickr.com\/photos\/dalelane\/1448282201\/\" target=\"_blank\">HTC Universal and HTC Advantage<\/a> &#8211; neither of which are small, particularly the Advantage! <\/p>\n<p>And I use a <a href=\"http:\/\/www.flickr.com\/photos\/dalelane\/490427501\/\" target=\"_blank\">small and uber-light Bluetooth handset<\/a> to make and receive calls.  <\/p>\n<p>When I&#8217;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&#8217;s normally clipped to a shirt or in a pocket. So it&#8217;s pretty much always with me.  <\/p>\n<p>My actual phone is rarely in a pocket. Partly because I&#8217;m almost constantly either referring to or jotting notes in it, and partly because it&#8217;s too big to fit in a pocket unless I&#8217;m wearing combats! \ud83d\ude42 As the phone is normally in my hands, I do have a habit of leaving it in places&#8230; particularly at other people&#8217;s desks at work.  <\/p>\n<p>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. <\/p>\n<p><strong>Registering for Bluetooth notifications<\/strong><br \/>\nThe code for this is actually pretty simple &#8211; there is a Windows API called <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa916492.aspx\" target=\"_blank\">RequestBluetoothNotifications<\/a> which lets you register for Bluetooth events.  <\/p>\n<p>I found a good MSDN article for Windows Mobile: &#8220;<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa916855.aspx\" target=\"_blank\">Handling Events from the Bluetooth Stack<\/a>&#8221; 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.  <\/p>\n<p>As I&#8217;m <a href=\"http:\/\/dalelane.co.uk\/blog\/?p=271\" target=\"_blank\">getting quite into the whole p\/invoking thing<\/a>, I thought I&#8217;d have a go at wrapping it up in a C# class for a .NET application. It&#8217;s fairly simple &#8211; there aren&#8217;t any strings in the Bluetooth structures I&#8217;m using here, so the mapping was all straightforward. If it is any use to anyone else, the  <a href=\"http:\/\/dalelane.co.uk\/files\/BTSecure.cs.txt\" target=\"_blank\">interesting bit of the source<\/a> is here. (<em>Usual disclaimers apply&#8230; that is, don&#8217;t blame me if it turns out to be gibberish. I was only having a quick play with this stuff.<\/em>) <\/p>\n<p>After a quick bit of hacking, it <a href=\"http:\/\/dalelane.co.uk\/page.php?id=1050\" target=\"_blank\">seems to work<\/a> (<a href=\"http:\/\/dalelane.co.uk\/files\/bLADESecure.cab\" target=\"_blank\">download the CAB here<\/a>). It&#8217;s not quite finished&#8230; I&#8217;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&#8217;t worked out how to find out which device has connected or disconnected! (It&#8217;s not a problem for my immediate purposes &#8211; my phone is only paired with one Bluetooth device anyway, but it&#8217;s something I want to come back to.) <\/p>\n<p><strong>Other Bluetooth app ideas<\/strong><br \/>\nThere are other things I want to try with Bluetooth code&#8230; ideally, I&#8217;d love to buy a few dozen <a href=\"http:\/\/www.blipsystems.com\/Default.aspx?ID=684\" target=\"_blank\">Bluetooth<\/a> <a href=\"http:\/\/www.lesswire.com\/products\/net_comp\/BluePing.htm\" target=\"_blank\">beacons<\/a> and scatter them around the site where I work, <a href=\"http:\/\/www.ibm.com\/uk\/hursley\/\" target=\"_blank\">Hursley Park<\/a>. Then I could use them as a sort of internal GPS around work. If people&#8217;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&#8217;t find a meeting room.  <\/p>\n<p>Or I could use it to detect when I get in my car and when I get home&#8230; not sure what I&#8217;d do with those events, but it might be useful.  <\/p>\n<p>The key bit will be what sort of power drain these apps &#8211; <a href=\"http:\/\/dalelane.co.uk\/page.php?id=1049\" target=\"_blank\">location-based reminder<\/a> and this <a href=\"http:\/\/dalelane.co.uk\/page.php?id=1050\" target=\"_blank\">auto-locking<\/a> &#8211; 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. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 disconnect For the location-based reminder app, I went with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[202,571,215,19,43],"class_list":["post-274","post","type-post","status-publish","format-standard","hentry","category-code","tag-bluetooth","tag-code","tag-lg-style-i","tag-windows-mobile","tag-windowsmobile"],"_links":{"self":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=274"}],"version-history":[{"count":0,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/274\/revisions"}],"wp:attachment":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}