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

简介:

原文见 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} secondsdot.gif "   +  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 博客园 -- 精简开发 无线生活


    本文转自Jake Lin博客园博客,原文链接:http://www.cnblogs.com/procoder/archive/2009/05/21/1471659.html,如需转载请自行联系原作者



相关文章
|
6天前
|
C# Windows
.NET开源免费的Windows快速文件搜索和应用程序启动器
今天大姚给大家分享一款.NET开源(MIT License)、免费、功能强大的Windows快速文件搜索和应用程序启动器:Flow Launcher。
|
6天前
|
存储 文字识别 C#
.NET开源免费、功能强大的 Windows 截图录屏神器
今天大姚给大家分享一款.NET开源免费(基于GPL3.0开源协议)、功能强大、简洁灵活的 Windows 截图、录屏、Gif动图制作神器:ShareX。
|
6天前
|
Linux C# 开发工具
C#开源的一款友好的.NET SDK管理器
C#开源的一款友好的.NET SDK管理器
|
6天前
|
SQL Windows
保姆级:Windows Server 2012上安装.NET Framework 3.5
保姆级:Windows Server 2012上安装.NET Framework 3.5
|
6天前
|
C# Windows
一款.NET开源、简洁易用的Windows桌面小说阅读应用
一款.NET开源、简洁易用的Windows桌面小说阅读应用
|
6天前
|
Windows
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
273 0
|
6天前
|
C# Windows
.NET开源的一个小而快并且功能强大的 Windows 动态桌面软件
.NET开源的一个小而快并且功能强大的 Windows 动态桌面软件
|
6天前
|
JavaScript Linux C#
【傻瓜级JS-DLL-WINCC-PLC交互】1.C#用windows窗体控件创建.net控件
【傻瓜级JS-DLL-WINCC-PLC交互】1.C#用windows窗体控件创建.net控件
74 0
|
6天前
|
人工智能 机器人 C#
Windows编程课设(C#)——基于WPF和.net的即时通讯系统(仿微信)
一款参考QQ、微信的即时通讯软件。采用CS结构,客户端基于.Net与WPF开发,服务端使用Java开发。
|
6天前
|
C# Windows
C#安装“Windows 窗体应用(.NET Framework)”
C#安装“Windows 窗体应用(.NET Framework)”
57 0