Description:
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.