windows 蓝牙开源工具包分析及C#实例
<buletooh-tool-v2.1.zip>中是使用“Windows Embedded Source Tools for Bluetooth”开发的实例程序。
Bluetooth的应用十分广泛,基于Bluetooth的通信程序开发主要有以下几个步骤:
服务端
* 设置本设备为可发现。
* 公开服务给其他Bluetooth设备访问。
* 接受其他Bluetooth设备的链接。
* 与链接上的Bluetooth设备进行通信。
客户端
* 发现周边Bluetooth设备。
* 主动与被发现的设备发起连接。
* 与链接上的Bluetooth设备进行通信。
在.NET Compact Framework下进行Bluetooth开发有几个可选解决方案
* 可以P/Invoke直接调用Bluetooth的API(btdrt.dll)
* 使用MS的Windows Embedded Source Tools for Bluetooth
* 使用32feet.NET库
这篇文章讲述基于Windows Embedded Source Tools for Bluetooth的开发,点击 链接 下载Windows Embedded Source Tools for Bluetooth,安装后在目录 C:\Program Files\Microsoft\Windows Embedded Source Tools会找到源码以及编译后的DLL。
Windows Embedded Source Tools for Bluetooth提供的API总结如下:
BluetoothRadio.cs
分析:
public enum BluetoothRadioMode : int
{
Off, On, Discoverable
};
API:
bool Dispose() 设置蓝牙设备运行被发现或读取当前蓝牙设备是否允许被发现
BluetoothRadioMode BluetoothRadioMode() 设置或去读当前蓝牙状态
BluetoothDeviceCollection PairedDevices() 获取配对设备集
class BluetoothRadio :BluetoothRadio() 初始化蓝牙系统构造类
BluetoothEndPoint.cs
分析:
class BluetoothEndPoint 蓝牙端点构造类。
构造时包含deviceAddress ,serviceGuid,port三个值。
BluetoothService.cs
分析:
class BluetoothService:BluetoothService(Guid serviceGuid) 蓝牙服务端构造类,执行该构造类的应该是蓝牙通信中的主设备。
void Start() 启动服务
void Stop() 停止服务
bool Started 获取服务状态,启动或停止
bool ConnectionPending 指示是否有一个客户端等待连接到本服务上。
NetworkStream AcceptConnection() 接受一个连接,执行该函数后,该函数会挂起直到一个客户端连接上来。
Guid ServiceGuid 返回本服务的GUID
StandardServices.cs
分析:
class StandardServices:StandardServices()
标准的蓝牙服务列表的GUID
Guid SerialPortServiceGuid 返回串口服务对应的GUID
Registry.cs
分析:
class Registry 封装的注册表操作类。
SafeNativeMethods.cs
分析:
class SafeNativeMethods 构造类。
导入以下类库的API:
const string BTHUTIL_DLL = "bthutil.dll";
const string BTDRT_DLL = "btdrt.dll";
const string WINSOCK_DLL = "Ws2.dll";
const string CORE_DLL =