Tuesday, June 03, 2008

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.

 

image

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?

 


Tuesday, June 03, 2008 07:42:53 (Eastern Standard Time, UTC-05:00)
Three suggestions - I've already written the code in VB.NET, all you gotta do is convert them to C# :)

1) Tip Calculator
2) Basic SMS Sending
3) Call Blocking

Code samples for SMS Sending and Call Blocker are already on my blog, I'll upload the Tip Calculator solution :)
Tuesday, June 03, 2008 07:54:51 (Eastern Standard Time, UTC-05:00)
Hi,
I may have an idea that you might be interested in. I have a GPS receiver in my device and use the GPS Sample as provided with the Windows Mobile 5 SDK. I would be interested in a 'snail trail' kind of application which would draw your current location and keep track of your movements, a simple line showing your movements would be great. Also maybe a compass type application which would calculate your bearing.
I have had some success with these ideas however I just don't get the time to explore them further.
Thanks
Paul Diston
Paul Diston
Friday, June 06, 2008 00:38:04 (Eastern Standard Time, UTC-05:00)
Hi,

I would really like an app to quickly change between 2G (GSM) and 3G (UMTS) on my HTC Universal, running WM6.1

thanks, Alex
Alex
Friday, June 06, 2008 08:27:08 (Eastern Standard Time, UTC-05:00)
Would it be possible to take the BlueTooth manager a little further and have it toggle on when a call arrives? I would undertake this myself if I had the tools.
Hugh Belton
Friday, June 06, 2008 21:48:36 (Eastern Standard Time, UTC-05:00)
Alex,

Your brilliant man. I can't believe we didn't think about taking that approach it is so simple and clean. I love it. I'm not sure it will accomplish what we are trying to pull off here, but I have to know if it will work.

I might try to work this in this weekend. It's going to be a busy one. But I will do it, unless one of our enterprising readers, ;), beats me too it. Go for it guys.
Wednesday, June 11, 2008 06:39:45 (Eastern Standard Time, UTC-05:00)
Lou,

Thanks for the ideas man. I'll definitely do something with SMS soon. And I ended up writing a tip calculator of my own: Day 09: Mobile Tipper. Thanks!
Comments are closed.

Theme design by Jelle Druyts

Pick a theme: