Windows Moible, Wince 使用.NET Compact Framework的进行蓝牙(Bluetooth)开发 之 蓝牙虚拟串口 (Bluetooth Virtual Serial Port)

简介:

在之前的两篇文章分别讲述了在.NET Compact Framework下使用Windows Embedded Source Tools for Bluetooth和32feet.NET进行Bluetooth的开发,链接如下:
.NET Compact Framework下的Bluetooth开发 之 Windows Embedded Source Tools for Bluetooth 
.NET Compact Framework下的Bluetooth开发 之 32feet.NET 
在这篇文章讲述Bluetooth Virtual Serial Port的开发,所谓Bluetooth Virtual Serial Port,其实是从软件的角度看,把Bluetooth的通信转化成Serial Port(串口)。经过这样的转换后,使用Bluetooth的Client程序可以像使用串口一样操作Bluetooth。这个应用方式的出现是为了支持现有应用(Legacy system,遗产应用)。举个例子,在Bluetooth出现以前,大部分移动设备都是通过串口连接的GPS receiver的,基于GPS应用程序的开发也就通过的串口通信取出NMEA data。关于GPS NMEA data的开发可以参考 .NET Compact Framework下的GPS NMEA data数据分析 。串口的开发可以参考.NET Compact Framework下的串口通信。随着Bluetooth的普及,移动设备可以通过Bluetooth来连接GPS receiver了,那么原先基于GPS的应用程序需要重新开发通信部分去读取NMEA data,这为现有应用带来很多麻烦,所有的现有应用都需要重写通信部分,因此人们想出解决方法,把Bluetooth的通信转化成Serial Port(串口)。硬件上使用Bluetooth来进行通信,在软件上虚拟一个串口给应用程序,应用程序不需要任何的修改就可以支持Bluetooth的GPS Receiver了。这就像设计模式里面的Adapter模式,但是这里是为新设备提供原有的接口,使得原先的Client不需要更改。

由于Bluetooth Virtual Serial Port的出现基于对现有系统(Legacy System)支持的需求,所以对于新的系统,MS不推荐使用Bluetooth Virtual Serial Port,而是直接使用Winsock进行通信。在使用Winsock进行Bluetooth通信需要指定服务,因此可以指定使用串口服务进行通信。Bluetooth Virtual Serial Port和Winsock的Bluetooth通信都是使用RFCOMM协议,所以两者等同。使用Winsock的Bluetooth通信比Bluetooth Virtual Serial Port更简单,不需要配置。而且更强壮(robust),因为使用Winsock的Bluetooth通信可以直接监听到蓝牙设备关闭或者离开通信范围,而Bluetooth Virtual Serial Port只能通过Timeout来检查。

由于支持现有系统(Legacy System),Bluetooth Virtual Serial Port还是有存在的价值,下面讲述Bluetooth Virtual Serial Port的开发。在Windows Mobile下有两种方法可以建立Bluetooth Virtual Serial Port:调用API建立Bluetooth Virtual Serial Port和修改注册表建立Bluetooth Virtual Serial Port

调用API建立Bluetooth Virtual Serial Port

调用API建立Bluetooth Virtual Serial Port可以调用RegisterDevice

HANDLE h  =  RegisterDevice (L " COM " , index, L " btd.dll " , (DWORD) & pp);

 

建立服务端口,需要配置以下参数

PORTEMUPortParams pp;
memset (
& pp,  0 sizeof (pp));
pp.flocal 
=  TRUE;
pp.channel 
=  channel  &   0xff ;

服务端口flocal为true。channel可以使用RFCOMM_CHANNEL_MULTIPLE (0xfe),这样RFCOMM 会自动分配可用的信道。

建立客户端口,需要配置以下参数

 

PORTEMUPortParams pp;
memset (
& pp,  0 sizeof (pp));
pp.device 
=  ba;
pp.channel 
=  channel  &   0xff ;

服务端口flocal为false。device为服务端地址。

反注册端口使用以下API

DeregisterDevice (h);

详细可以参考MSDN文章 Creating a Connection to a Remote Device Using a Virtual COM Port,链接见参考文献。

在32feet.net里面,这些API封装在InTheHand.Net.Ports.BluetoothSerialPort,可以直接使用。但是32feet.net的作者提醒这些API是不是可信赖的(unreliable),所以在使用之前请要谨慎考虑和详细测试。我在wince 5下测试过,不能成功建立Bluetooth Virtual Serial Port。

使用32feet.net建立服务端口

复制代码
public   static   void  CreateIncomingPort()
{
    BluetoothSerialPort port 
=  BluetoothSerialPort.CreateServer(BluetoothService.SerialPort);
    Console.WriteLine(port.PortName);
}
复制代码

 

使用32feet.net建立客户端口

复制代码
public   static   void  CreateIncomingPort()
{
    BluetoothClient client 
=   new  BluetoothClient();
    BluetoothDeviceInfo[] devices 
=  client.DiscoverDevices();
    BluetoothDeviceInfo device 
=   null ;
    
foreach  (BluetoothDeviceInfo d  in  devices)
    {
        
if  (d.DeviceName  ==   " BLUETOOTH_DEVICE " )
        {
            device 
=  d;
            
break ;
        }
    }
    BluetoothEndPoint  endPoint 
=   new  BluetoothEndPoint(device.DeviceAddress, BluetoothService.SerialPort);
    BluetoothSerialPort port 
=  BluetoothSerialPort.CreateClient(endPoint);
    Console.WriteLine(port.PortName);
}
复制代码

 

修改注册表建立Bluetooth Virtual Serial Port

由于第一种方法不是很可靠,所以可以选择第二种方法,第二种方法其实就是修改注册表,把
HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Bluetooth\\Serial\\Ports的项进行修改。可是这个方法只是支持windows mobile5以上的系统,不支持wince5。同时,如果使用这个方法,需要重启系统。

这是Windows Mobile的注册表,可以通过程序修改注册表的项目,通过程序修改后需要重启Windows Mobile。

上图为Wince 5的注册表,结构不一样,在Wince5不能通过修改注册表的方式实现Bluetooth Virtual Serial Port。

下面的代码来源自32feet.net的BluetoothDeviceInfo类里面的SetServiceState()方法。这里演示了如何修改注册表。

复制代码
if  (state)
{
    
// write registry settings for WM5 Serial Port support

    
// get available ports
    Microsoft.Win32.RegistryKey rkPorts  =  Microsoft.Win32.Registry.LocalMachine.OpenSubKey( " SOFTWARE\\Microsoft\\Bluetooth\\Serial\\Ports " true );
    
string [] supportedPorts  =  ( string [])rkPorts.GetValue( " SupportedPorts " );
    System.Collections.ArrayList alPorts 
=   new  System.Collections.ArrayList(supportedPorts);

    
// check availability
     foreach  ( string  deviceid  in  rkPorts.GetSubKeyNames())
    {
        Microsoft.Win32.RegistryKey rkDevice 
=  rkPorts.OpenSubKey(deviceid);
        
// remove port from arraylist if unavailable
         string  port  =  rkDevice.GetValue( " Port " ).ToString();
        
int  nullPos  =  port.IndexOf( ' \0 ' );
        
if  (nullPos  >   - 1 )
        {
            port 
=  port.Substring( 0 , nullPos);
        }
        
if  (alPorts.Contains(port))
        {
            alPorts.Remove(port);
        }
        rkDevice.Close();
    }

    
if  (alPorts.Count  ==   0 )
    {
        
throw   new  InvalidOperationException( " No ports available " );
    }
    
// write port details to registry
    Microsoft.Win32.RegistryKey rkNewPort  =  rkPorts.CreateSubKey( this .DeviceAddress.ToString( " 8 " ));
    rkNewPort.SetValue(
" KeepDCB " 0 );
    rkNewPort.SetValue(
" RemoteDCB " 0 );
    rkNewPort.SetValue(
" Encryption " 0 );
    rkNewPort.SetValue(
" Authentication " 0 );
    rkNewPort.SetValue(
" Port " , alPorts[ 0 ]);
    rkNewPort.SetValue(
" Server " 0 );
    rkNewPort.Close();

    rkPorts.Close();

    
// try open port now
     try
    {
        InTheHand.Net.Ports.BluetoothSerialPort.CreateClient(alPorts[
0 ].ToString(),  new  BluetoothEndPoint( this .DeviceAddress, BluetoothService.SerialPort));
    }
    
catch
    {
    }
}
else
{
    
// find and remove registry entries
    Microsoft.Win32.RegistryKey rkPorts  =  Microsoft.Win32.Registry.LocalMachine.OpenSubKey( " SOFTWARE\\Microsoft\\Bluetooth\\Serial\\Ports " true );
    
foreach  ( string  deviceAddress  in  rkPorts.GetSubKeyNames())
    {
        
if  (deviceAddress  ==   this .DeviceAddress.ToString( " 8 " ))
        {
            rkPorts.DeleteSubKeyTree(deviceAddress);
            
break ;
        }
    }
    rkPorts.Close();
}
复制代码
当state为true时为注册,当state为false时为反注册。

 

参考文献

Bluetooth COM Ports On Windows CE 
Windows Mobile 5.0 Bluetooth Virtual Serial Ports 
Any Port in a Storm 
MSDN:Creating a Connection to a Remote Device Using a Virtual COM Port

 


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



相关文章
|
1月前
|
数据可视化 数据库 C++
Qt 5.14.2揭秘高效开发:如何用VS2022快速部署Qt 5.14.2,打造无与伦比的Windows应用
Qt 5.14.2揭秘高效开发:如何用VS2022快速部署Qt 5.14.2,打造无与伦比的Windows应用
|
18天前
|
监控 安全 API
7.3 Windows驱动开发:内核监视LoadImage映像回调
在笔者上一篇文章`《内核注册并监控对象回调》`介绍了如何运用`ObRegisterCallbacks`注册`进程与线程`回调,并通过该回调实现了`拦截`指定进行运行的效果,本章`LyShark`将带大家继续探索一个新的回调注册函数,`PsSetLoadImageNotifyRoutine`常用于注册`LoadImage`映像监视,当有模块被系统加载时则可以第一时间获取到加载模块信息,需要注意的是该回调函数内无法进行拦截,如需要拦截则需写入返回指令这部分内容将在下一章进行讲解,本章将主要实现对模块的监视功能。
36 0
7.3 Windows驱动开发:内核监视LoadImage映像回调
|
4月前
|
监控 安全 API
7.2 Windows驱动开发:内核注册并监控对象回调
在笔者上一篇文章`《内核枚举进程与线程ObCall回调》`简单介绍了如何枚举系统中已经存在的`进程与线程`回调,本章`LyShark`将通过对象回调实现对进程线程的`句柄`监控,在内核中提供了`ObRegisterCallbacks`回调,使用这个内核`回调`函数,可注册一个`对象`回调,不过目前该函数`只能`监控进程与线程句柄操作,通过监控进程或线程句柄,可实现保护指定进程线程不被终止的目的。
30 0
7.2 Windows驱动开发:内核注册并监控对象回调
|
4月前
|
监控 安全 API
7.6 Windows驱动开发:内核监控FileObject文件回调
本篇文章与上一篇文章`《内核注册并监控对象回调》`所使用的方式是一样的都是使用`ObRegisterCallbacks`注册回调事件,只不过上一篇博文中`LyShark`将回调结构体`OB_OPERATION_REGISTRATION`中的`ObjectType`填充为了`PsProcessType`和`PsThreadType`格式从而实现监控进程与线程,本章我们需要将该结构填充为`IoFileObjectType`以此来实现对文件的监控,文件过滤驱动不仅仅可以用来监控文件的打开,还可以用它实现对文件的保护,一旦驱动加载则文件是不可被删除和改动的。
29 1
7.6 Windows驱动开发:内核监控FileObject文件回调
|
3月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
42 0
|
1月前
|
开发框架 前端开发 .NET
进入ASP .net mvc的世界
进入ASP .net mvc的世界
29 0
|
1月前
mvc.net分页查询案例——mvc-paper.css
mvc.net分页查询案例——mvc-paper.css
5 0
|
1月前
|
开发框架 前端开发 .NET
C# .NET面试系列六:ASP.NET MVC
<h2>ASP.NET MVC #### 1. MVC 中的 TempData\ViewBag\ViewData 区别? 在ASP.NET MVC中,TempData、ViewBag 和 ViewData 都是用于在控制器和视图之间传递数据的机制,但它们有一些区别。 <b>TempData:</b> 1、生命周期 ```c# TempData 的生命周期是短暂的,数据只在当前请求和下一次请求之间有效。一旦数据被读取,它就会被标记为已读,下一次请求时就会被清除。 ``` 2、用途 ```c# 主要用于在两个动作之间传递数据,例如在一个动作中设置 TempData,然后在重定向到另
99 5