M. G. Phone Home (Matt Gertz)
I have a slight problem with my cell phone. It’s a smartphone; I love it, I use it all the time, and I don’t what I’d do without it, but I’ve been pretty aggravated with its support of Bluetooth. It will fail to pair with the Sync system in the car at random times, which drives me crazy as I’ll only notice the problem when I’m on the highway or something. 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 “No, I’m still on the on-ramp for I-405N – well, I think I’ve moved 50 meters since I called you last.”
Toggling the Bluetooth on/off fixes the problem, so it seems to be an issue with the device itself. 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. And that’s if I don’t have to unlock the phone first with my PIN!
So, for a while, I’ve been meaning to create a simple application that would toggle the setting for me, since I don’t really want to upgrade my phone at the moment. 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. 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 – the Windows Embedded Source Tools. Some of you probably already know about it (it’s been out for over three years, which means there’s probably lots of other blog posts out there on this, but blithely pressing on…), but for those who haven’t heard about it, it’s great for trying out Bluetooth programming on a Windows Mobile 5 device or later.
First, you need to download the tools. You can get them from this link. After you’ve installed them, you can use them immediately – you don’t need to reboot Visual Studio or anything like that.
In Visual Studio, create a new project via “File->New Project” (or “File->Add New Project” if you prefer), and under the Visual Basic node in the resulting window, select “Smart Device Project” and give it a name (in my case, I chose “ResetBluetooth”). Click “OK,” and you’ll be taken to a dialog where you get to choose the type of SD project you want to create. This application won’t need any UI, so just choose “Console Application” and click OK.
Once the editor has been set up and the solution explorer populated with the application, you’ll need to add a reference to the source tools you just downloaded. Right-click on the project and choose “Add Reference.” Switch to the “Browse” tab, and then navigate to “C:Program FilesWindows Embedded Source ToolsMicrosoft.WindowsMobile.SharedSource.Bluetooth.dll” – press OK to add the reference. (It actually took me a few tries to figure out this step, since I was trying to add services from the .NET tab – there *are* Bluetooth libraries there, just not the one I needed.)
Now, back in the editor, let’s make it cleaner to access the calls by adding the following line at the top of the file:
Imports Microsoft.WindowsMobile.SharedSource.Bluetooth
The rest of the code is pretty straightforward:
Module Module1
Sub Main()
Dim radio As New BluetoothRadio
If radio.BluetoothRadioMode = BluetoothRadioMode.On Then
radio.BluetoothRadioMode = BluetoothRadioMode.Off
End If
radio.BluetoothRadioMode = BluetoothRadioMode.On
End Sub
End Module
That almost doesn’t need any comments, but just for forms’ sake – by allocating a BluetoothRadio object, I’m connecting to my phone’s Bluetooth service. I’ll check to see if Bluetooth is on – if it is, I’ll turn it off. Then, regardless of whether it was on or off to begin with, I’ll turn it on. (This covers the case where my phone turns off Bluetooth for no good reason, which also happens sometimes.)
At this point, you could press F5 and test it against the emulator. Unfortunately, the emulator doesn’t support Bluetooth, alas (“No Bluetooth hardware is installed on this device” is listed in its system settings), and so all you can do is verify that it doesn’t crash. To really test it, you’ll need to deploy it to your bluetooth-supporting Windows Mobile phone when you F5 – a choice to do this will be offered to you. (Ironically, I haven’t really had a opportunity to test it “in crisis” – my phone is in one of those rare states where it behaves correctly, for a week or so… J)
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. I may try some of those out later, as I’ve been trying to debug why my GPS receiver isn’t connecting to my phone…
‘Til next time,
–Matt–*
0 comments
Be the first to start the discussion.