您的位置:首页 > 移动开发

30 Days of .NET [Windows Mobile Applications] - Day 02: Bluetooth Manager(蓝牙管理器)

2009-05-21 17:17 501 查看
原文见 Day 02: Bluetooth
Manager



需求

Page Brooks为了省电,想一步完成Bluetooth开关的操作。

实现

使用的技术有P/Invoke蓝牙API, PictureBox, State and Notification Broker API.

看过我之前的文章会知道,在Windows Mobile下打开关闭Bluetooth,就是P/Invoke BthSetMode().

.NET
Compact Framework下的Bluetooth开发 之 Windows Embedded Source Tools for Bluetooth

.NET
Compact Framework下的Bluetooth开发 之 32feet.NET

.NET
Compact Framework下的Bluetooth开发 之 Bluetooth Virtual Serial Port

[DllImport("BthUtil.dll")]
private static extern int BthGetMode(out RadioMode dwMode);
[DllImport("BthUtil.dll")]
private static extern int BthSetMode(RadioMode dwMode);

状态变更功能,如果外部程序变更了Bluetooth的状态,当前程序需要被通知并处理变更。

using Microsoft.WindowsMobile.Status;
SystemState bluetoothStatePowerOn = new SystemState(SystemProperty.BluetoothStatePowerOn);
bluetoothStatePowerOn.Changed += new ChangeEventHandler(bluetoothStatePowerOn_Changed);

void bluetoothStatePowerOn_Changed(object sender, ChangeEventArgs args)
{
UpdateScreen();
}

这里使用了State and Notifications Broker API,需要引用Microsoft.WindowsMobile.Status库。SystemState(SystemProperty.BluetoothStatePowerOn)指定了状态监控的类型,生成Bluetooth开关的系统状态对象,bluetoothStatePowerOn.Changed += new ChangeEventHandler(bluetoothStatePowerOn_Changed)订阅Bluetooth开关系统状态的变更消息,并使用bluetoothStatePowerOn_Changed进行处理该消息。

State and Notifications Brokerz API是一个很重要的API,这API可以监控注册表的变化状况。总所周知,在Windowns里面注册表就是保持系统信息和应用程序信息的小型数据库。State and Notifications Brokerz API提供监控注册表的功能,表示他能监控系统信息以及应用程序信息的变化。这些信息包括摄像头状态,ActiveSync,电源状态,SMS,计划任务,呼叫信息,Bluetooth状态,网络链接状态,modem状态等等。所以这API广泛运用于系统信息相关事件触发的开发,参考链接见下面。

增加自动关闭程序功能。

private void timer_Tick(object sender, EventArgs e)
{
textBox.Text = string.Empty;

for (int i = 10; i > 0; i--)
{
textBox.Text += string.Format("Auto shutdown in {0} seconds

" + Environment.NewLine, i);
Thread.Sleep(1000);
}

this.Close();
}

this.timer.Interval = 60000;
这个程序运行1分钟后,自动关闭自己。在关闭前,有10秒钟的倒数,目的使得用户知道这个程序不是Crash,而是自动关闭了,这是用户友好性设计的表现。

Emulator下调试

由于Windows Mobile的Emulator不直接支持Bluetooth,所以源代码需要在真实设备上进行调试,为了方便,可以尝试在Emulator调试。可以参考
施炯 同学的文章 在Windows Mobile模拟器上使用蓝牙以及 Dmitry Klionsky的Bluetooth for Microsoft Device Emulator

安装程序: bluetoothManager.cab

源代码: bluetoothManager.zip




参考文献:
MSDN:State and
Notifications Broker

.NET Compact Framework, WinCE, Windows Mobile开发系列

Jake's Blog in 博客园 -- 精简开发 无线生活
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐