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

    最近在学.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);  

 

          

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值