{"id":3363,"date":"2008-12-09T16:26:00","date_gmt":"2008-12-09T16:26:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vbteam\/2008\/12\/09\/m-g-phone-home-matt-gertz\/"},"modified":"2024-07-05T13:35:58","modified_gmt":"2024-07-05T20:35:58","slug":"m-g-phone-home-matt-gertz","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/vbteam\/m-g-phone-home-matt-gertz\/","title":{"rendered":"M. G. Phone Home (Matt Gertz)"},"content":{"rendered":"<h2><font face=\"Cambria\" color=\"#365f91\" size=\"5\">M. G. Phone Home (Matt Gertz)<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">I have a slight problem with my cell phone.<span>&nbsp; <\/span>It&rsquo;s a smartphone; I love it, I use it all the time, and I don&rsquo;t what I&rsquo;d do without it, but I&rsquo;ve been pretty aggravated with its support of Bluetooth.<span>&nbsp; <\/span>It will fail to pair with the Sync system in the car at random times, which drives me crazy as I&rsquo;ll only notice the problem when I&rsquo;m on the highway or something.<span>&nbsp; <\/span>Here in Washington State, it is (thankfully) illegal to drive and operate a hands-on cell phone at the same time, and so one of the whole points of getting a car with Sync was to be able to easily phone my wife simply by voice command and say &ldquo;No, I&rsquo;m still on the on-ramp for I-405N &ndash; well, I think I&rsquo;ve moved 50 meters since I called you last.&rdquo;<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Toggling the Bluetooth on\/off fixes the problem, so it seems to be an issue with the device itself.<span>&nbsp; <\/span>Unfortunately, toggling Bluetooth is not a good solution while driving, since to do that in Windows Mobile 6.1, I have to get out my stylus, click on a little icon, go to the second tab in the resulting dialog, click a check box to turn it off, dismiss the dialog, and then repeat the process a second later to turn it on.<span>&nbsp; <\/span>And that&rsquo;s if I don&rsquo;t have to unlock the phone first with my PIN!<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">So, for a while, I&rsquo;ve been meaning to create a simple application that would toggle the setting for me, since I don&rsquo;t really want to upgrade my phone at the moment.<span>&nbsp; <\/span>The idea would be to put the application on my Start menu on the phone so I could easily trigger the whole sequence of events.<span>&nbsp; <\/span>I finally got enough free time to look into this late last week, and while searching around on the Web for some pointers into how to code against the Bluetooth stack, I came across a neat little Microsoft library which wraps up common Bluetooth functionality &ndash; the Windows Embedded Source Tools.<span>&nbsp; <\/span>Some of you probably already know about it (it&rsquo;s been out for <\/font><a href=\"http:\/\/www.microsoft.com\/presspass\/press\/2005\/may05\/05-10sourcetoolspr.mspx\"><font face=\"Calibri\" size=\"3\">over three years<\/font><\/a><font face=\"Calibri\" size=\"3\">, which means there&rsquo;s probably lots of other blog posts out there on this, but blithely pressing on&hellip;), but for those who haven&rsquo;t heard about it, it&rsquo;s great for trying out Bluetooth programming on a Windows Mobile 5 device or later.<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">First, you need to download the tools.<span>&nbsp; <\/span>You can get them from this <\/font><a href=\"http:\/\/go.microsoft.com\/?linkid=2913250\"><font face=\"Calibri\" size=\"3\">link<\/font><\/a><font size=\"3\"><font face=\"Calibri\">.<span>&nbsp; <\/span>After you&rsquo;ve installed them, you can use them immediately &ndash; you don&rsquo;t need to reboot Visual Studio or anything like that.<span>&nbsp; <\/span><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">In Visual Studio, create a new project via &ldquo;File-&gt;New Project&rdquo; (or &ldquo;File-&gt;Add New Project&rdquo; if you prefer), and under the Visual Basic node in the resulting window, select &ldquo;Smart Device Project&rdquo; and give it a name (in my case, I chose &ldquo;ResetBluetooth&rdquo;).<span>&nbsp; <\/span>Click &ldquo;OK,&rdquo; and you&rsquo;ll be taken to a dialog where you get to choose the type of SD project you want to create.<span>&nbsp; <\/span>This application won&rsquo;t need any UI, so just choose &ldquo;Console Application&rdquo; and click OK.<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Once the editor has been set up and the solution explorer populated with the application, you&rsquo;ll need to add a reference to the source tools you just downloaded.<span>&nbsp; <\/span>Right-click on the project and choose &ldquo;Add Reference.&rdquo;<span>&nbsp; <\/span>Switch to the &ldquo;Browse&rdquo; tab, and then navigate to &ldquo;C:Program FilesWindows Embedded Source ToolsMicrosoft.WindowsMobile.SharedSource.Bluetooth.dll&rdquo; &ndash; press OK to add the reference.<span>&nbsp; <\/span>(It actually took me a few tries to figure out this step, since I was trying to add services from the .NET tab &ndash; there *are* Bluetooth libraries there, just not the one I needed.)<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Now, back in the editor, let&rsquo;s make it cleaner to access the calls by adding the following line at the top of the file:<\/font><\/p>\n<p class=\"MsoNormal\"><span>Imports<\/span><span> Microsoft.WindowsMobile.SharedSource.Bluetooth<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">The rest of the code is pretty straightforward:<\/font><\/p>\n<p class=\"MsoNormal\"><span>Module<\/span><span> Module1<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>Sub<\/span> Main()<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Dim<\/span> radio <span>As<\/span> <span>New<\/span> BluetoothRadio<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>If<\/span> radio.BluetoothRadioMode = BluetoothRadioMode.On <span>Then<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>radio.BluetoothRadioMode = BluetoothRadioMode.Off<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>End<\/span> <span>If<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>radio.BluetoothRadioMode = BluetoothRadioMode.On<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>End<\/span> <span>Sub<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>End<\/span><span> <span>Module<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">That almost doesn&rsquo;t need any comments, but just for forms&rsquo; sake &ndash; by allocating a BluetoothRadio object, I&rsquo;m connecting to my phone&rsquo;s Bluetooth service.<span>&nbsp; <\/span>I&rsquo;ll check to see if Bluetooth is on &ndash; if it is, I&rsquo;ll turn it off.<span>&nbsp; <\/span>Then, regardless of whether it was on or off to begin with, I&rsquo;ll turn it on.<span>&nbsp; <\/span>(This covers the case where my phone turns off Bluetooth for no good reason, which also happens sometimes.)<\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">At this point, you could press F5 and test it against the emulator.<span>&nbsp; <\/span>Unfortunately, the emulator doesn&rsquo;t support Bluetooth, alas (&ldquo;No Bluetooth hardware is installed on this device&rdquo; is listed in its system settings), and so all you can do is verify that it doesn&rsquo;t crash.<span>&nbsp; <\/span>To really test it, you&rsquo;ll need to deploy it to your bluetooth-supporting Windows Mobile phone when you F5 &ndash; a choice to do this will be offered to you.<span>&nbsp; <\/span>(Ironically, I haven&rsquo;t really had a opportunity to test it &ldquo;in crisis&rdquo; &ndash; my phone is in one of those rare states where it behaves correctly, for a week or so&hellip; <\/font><span><span>J<\/span><\/span><font face=\"Calibri\">)<\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">According the Intellisense for the BluetoothRadio object, you can also enumerate the devices connected to Bluetooth, which is pretty handy, and then presumably interact with them directly.<span>&nbsp; <\/span>I may try some of those out later, as I&rsquo;ve been trying to debug why my GPS receiver isn&rsquo;t connecting to my phone&hellip;<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">&lsquo;Til next time,<\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\"><span>&nbsp; <\/span>&#8211;Matt&#8211;*<\/font><\/font><\/p>\n<\/h2>\n","protected":false},"excerpt":{"rendered":"<p>M. G. Phone Home (Matt Gertz) I have a slight problem with my cell phone.&nbsp; It&rsquo;s a smartphone; I love it, I use it all the time, and I don&rsquo;t what I&rsquo;d do without it, but I&rsquo;ve been pretty aggravated with its support of Bluetooth.&nbsp; It will fail to pair with the Sync system in [&hellip;]<\/p>\n","protected":false},"author":258,"featured_media":8818,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[22,195],"tags":[101,140,166],"class_list":["post-3363","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-matt-gertz","category-visual-basic","tag-matt-gertz","tag-smart-devices","tag-vb2008"],"acf":[],"blog_post_summary":"<p>M. G. Phone Home (Matt Gertz) I have a slight problem with my cell phone.&nbsp; It&rsquo;s a smartphone; I love it, I use it all the time, and I don&rsquo;t what I&rsquo;d do without it, but I&rsquo;ve been pretty aggravated with its support of Bluetooth.&nbsp; It will fail to pair with the Sync system in [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/3363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/users\/258"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/comments?post=3363"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/3363\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media\/8818"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media?parent=3363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/categories?post=3363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/tags?post=3363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}