使用C#更改Smartphone(WM5)的蓝牙状态

来源:互联网 发布:模拟城市5 mac 破解版 编辑:程序博客网 时间:2024/04/29 19:18

    最近在学.net CF,写一个GPS的应用程序,手机是多普达585,系统为WM5 Smartphone,.NET CF 2,手机与GPS通过蓝牙连接,连接后以虚拟串口的形进行通讯。 

手机平时的蓝牙都是关着的,要不然太耗电了。每次使用GPS前,都得手动开启蓝牙,用完GPS之后,还得手机关闭蓝牙,有些麻烦。通过搜索,终于找到开启和关闭蓝牙的API。写成一个简单的类,要用时引用一下就可以。

 

using System;
using System.Runtime.InteropServices;

namespace GPS
{
 
public class Bluetooth
 {
  
//TODO THIS IS NOT TESTED!!!
        public enum RadioMode {
            BTH_POWER_OFF 
= 0,
            BTH_CONNECTABLE 
= 1,
            BTH_DISCOVERABLE 
= 2
        }

        [DllImport(
"BthUtil.dll", SetLastError = true)]
        
public static extern int BthGetMode(out RadioMode dwMode);  

        [DllImport(
"BthUtil.dll", SetLastError = true)]
        
public static extern int BthSetMode(RadioMode dwMode); }
}

 

 

使用:

using GPS;

 

打开串口前调用:

 

            Bluetooth.RadioMode mode = Bluetooth.RadioMode.BTH_POWER_OFF;
            
int ret = 0;

            
//检查蓝牙状态,如果不是"连接"状态,则设为"连接"状态
            ret = Bluetooth.BthGetMode(out mode);
            
if(mode!=Bluetooth.RadioMode.BTH_CONNECTABLE)  Bluetooth.BthSetMode(Bluetooth.RadioMode.BTH_CONNECTABLE);         

 

 

退出程序时调用:

 

            //关闭蓝牙
            Bluetooth.BthSetMode(Bluetooth.RadioMode.BTH_POWER_OFF);  

 

          

 

原创粉丝点击