首页>
技术资讯>
详情

NET Compact Framework进行蓝牙开发

2016-05-14 来源:佚名 阅读量: 0
关键词: WINCE

    在之前的两篇文章分别讲述了在。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()<

热门推荐 查看更多