Today, Page Brooks was telling to me about a limitation with his Windows Mobile device. His device's battery isn't powerful enough for him to be able to leave his Bluetooth radio on all day. But he really wants to be able to use his Bluetooth headset while he is driving to work in the morning, and on the way back home in the afternoon. One requirement he has is he doesn't want to have to look at the screen to accomplish this.
It would have been real easy to create a Bluetooth Toggle application that simply takes the current state of the Bluetooth radio and toggles it from on to off, and vice versa. Instead, I've decided to build a simple Bluetooth Manager application that will provide our readers with the code needed to create the Bluetooth Toggle application. My hope is that someone from the community will take this code and help Page out.
Technically I make some special "modifications" to the application that should help Page get by for the time being. When the application first starts, it toggles the Bluetooth radio mode. And I added a timer that will begin an auto shutdown after the program has been running for 1 minute. All Page needs to do is assign the application to a spare hardware key and then when he press that key it will toggle the Bluetooth radio on and off, and after 1 minute it will auto shutdown.
So according to Minutes to Midnight I have 2 hours, 21 minutes, and 6 seconds left to make today's application. So let's get on with the show.
Bluetooth Manager
This application came about due to a reader's request. So be sure to submit your ideas for applications. I strongly favor working on user submitted ideas over my own.
I always like an application to have a little eye candy, and that explains the interactive Bluetooth logo at the top of the screen. It is in color when the Bluetooth is enabled, and grayscale when Bluetooth is off. Clicking the Bluetooth logo toggles the Bluetooth radio from on to off.
I added a multiline textbox so users could see a history of Bluetooth radio state changes. There are buttons to allow users to directly turn the Bluetooth radio on and off.
One caveat is that this uses P/Invoke to access the Microsoft Bluetooth stack dlls.
Here are the calls:
[DllImport("BthUtil.dll")] private static extern int BthGetMode(out RadioMode dwMode);
[DllImport("BthUtil.dll")] private static extern int BthSetMode(RadioMode dwMode);
Also, we had to take advantage of State and Notification Broker API. Here's why: if the state of the Bluetooth radio were to change due to an action outside of our program we wouldn't know about it. That's where SNB API comes in. It allows us to subscribe to notifications for almost any event that we are interested in. The code is below.
SystemState bluetoothStatePowerOn = new SystemState(SystemProperty.BluetoothStatePowerOn); bluetoothStatePowerOn.Changed += new ChangeEventHandler(bluetoothStatePowerOn_Changed);
void bluetoothStatePowerOn_Changed(object sender, ChangeEventArgs args) { UpdateScreen(); }
The last thing to check out is the auto shutdown code: it's simple but works great. There is a timer that waits for 1 minute to pass, and then there is a loop for the count of ten, which uses a Thread.Sleep(1000) to pause the application for 1 second each iteration of the loop. This creates a great effect that keeps the user from thinking the application has crashed.
Download executable: bluetoothManager.cab
Download Source Code: bluetoothManager.zip
[Be sure to check out the C++ Edition of 30 Days of Windows Mobile Applications]
Feedback
28 days left to go, and so far we had one idea submitted for an application. Keep them coming. So what do you guys think? Is the format working for you? Any ideas on what we can improve?
Theme design by Jelle Druyts
Pick a theme: BlogXP BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves The Right Stuff 2.0 Tricoleur useit.com Voidclass2 BlogXP BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves The Right Stuff 2.0 Tricoleur useit.com Voidclass2
Powered by: newtelligence dasBlog 2.0.7226.0
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2009, Chris Craft
E-mail