Windows Moible, Wince 使用.NET Compact Framework的进行蓝牙(Bluetooth)设备配对的开发

简介:

.NET Compact Framework下的Bluetooth开发 之 32feet.NET 里讲述了如何使用32feet.net库来进行Bluetooth的开发,天机 同学在使用过程发现设备配对问题,本文讲述如何进行Bluetooth设备配对的开发。

以下是一个Bluetooth配对程序。在配对之前先扫描附近的Bluetooth设备。

bt_pair1

复制代码
        BluetoothClient client  =   new  BluetoothClient();
        Dictionary
< string , BluetoothAddress >  deviceAddresses  =   new  Dictionary < string , BluetoothAddress > ();
        
private   void  menuItem2_Click( object  sender, EventArgs e)
        {
            
// Turn on the bluetooth
            BluetoothRadio radio  =  BluetoothRadio.PrimaryRadio;
            radio.Mode 
=  RadioMode.Connectable;

            
// Scan the nearby devices
            BluetoothDeviceInfo[] devices  =  client.DiscoverDevices();
            listBoxDevices.Items.Clear();
            deviceAddresses.Clear();
            
foreach (BluetoothDeviceInfo device  in  devices)
            {
                listBoxDevices.Items.Add(device.DeviceName);
                deviceAddresses[device.DeviceName] 
=  device.DeviceAddress;
            }
        } 
复制代码

deviceAddresses 用于保存设备名字和设备地址的映射。当点击 Discover 菜单,先修改BluetoothRadio 的 property Mode 来打开Bluetooth设备,然后通过DiscoverDevices() 函数来扫描附近的所有Bluetooth设备。

 bt_pair2

这是扫描的结果, Jake为我的手机, AV890为Bluetooth 耳机。点击AV890 然后点击 Pair 菜单进行配对。AV890的默认配对密码为 0000。

bt_pair3

配对的代码如下:

复制代码
         private   void  menuItem3_Click( object  sender, EventArgs e)
        {
            
try
            {
                BluetoothAddress deviceAddress 
=  deviceAddresses[listBoxDevices.SelectedItem.ToString()];
                client.SetPin(deviceAddress, textBoxPin.Text.Trim());
                client.Connect(deviceAddress, BluetoothService.Handsfree); 
// if connect ot Hands free.
                
// client.Connect(deviceAddress, BluetoothService.SerialPort);  // if connect to cell phone and so forth.
                MessageBox.Show( " Pair successful. " );

                
// transfer data..
            }
            
catch  (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        } 
复制代码

由于设备有密码,所以配对的时候需要设置密码,使用 SetPin()函数设置对端设备的密码。SetPin() 函数使用了下面的API

[DllImport( " btdrt.dll " , SetLastError = true )]
public   static   extern   int  BthSetPIN( byte [] pba,  int  cPinLength,  byte [] ppin); 

 

Connect() 函数的第二个参数十分重要,天机 同学的程序配对失败可能是因为在这里没有设置正确。参数二是服务类型,通信双方必须使用同样的服务类型。如果通信双方的程序都是由我们负责开发,可以使用通用的服务类型,例如BluetoothService.SerialPort。但是如果要与第三方设备进行通信,需要查出该设备的服务类型,在设备端的通信服务类型一般编写在Firmware(固件)里面,而且会按照规范编写,例如Bluetooth耳机会使用BluetoothService.Handsfree。具体的规范可以参考下面文章。http://en.wikipedia.org/wiki/Bluetooth_profile

在32feet.net里使用BluetoothService来定义服务类型,如下:


ContractedBlock.gif Code

配对成功后的界面。

bt_pair4

可以在配对成功(connection()函数)后加入相应的通信处理代码。

 

源代码:http://files.cnblogs.com/procoder/BluetoothPairing.rar

运行环境:VS2008 + Windows Mobile 5 Pocket PC SDK  + CF.NET 2.0



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


相关文章
|
1月前
|
SQL 开发框架 数据可视化
企业应用开发中.NET EF常用哪种模式?
企业应用开发中.NET EF常用哪种模式?
|
10天前
|
开发框架 前端开发 JavaScript
采用C#.Net +JavaScript 开发的云LIS系统源码 二级医院应用案例有演示
技术架构:Asp.NET CORE 3.1 MVC + SQLserver + Redis等 开发语言:C# 6.0、JavaScript 前端框架:JQuery、EasyUI、Bootstrap 后端框架:MVC、SQLSugar等 数 据 库:SQLserver 2012
|
1月前
|
数据安全/隐私保护 Windows
.net三层架构开发步骤
.net三层架构开发步骤
13 0
|
1月前
深入.net平台的分层开发
深入.net平台的分层开发
59 0
|
1月前
|
数据可视化 数据库 C++
Qt 5.14.2揭秘高效开发:如何用VS2022快速部署Qt 5.14.2,打造无与伦比的Windows应用
Qt 5.14.2揭秘高效开发:如何用VS2022快速部署Qt 5.14.2,打造无与伦比的Windows应用
|
2月前
|
开发框架 前端开发 .NET
福利来袭,.NET Core开发5大案例,30w字PDF文档大放送!!!
为了便于大家查找,特将之前开发的.Net Core相关的五大案例整理成文,共计440页,32w字,免费提供给大家,文章底部有PDF下载链接。
33 1
福利来袭,.NET Core开发5大案例,30w字PDF文档大放送!!!
|
1月前
|
安全 数据安全/隐私保护 Windows
解锁安全之门,Windows Server 2019密码修改攻略大揭秘
解锁安全之门,Windows Server 2019密码修改攻略大揭秘
|
1月前
|
存储 安全 网络安全
铁壁如墙-WINDOWS SERVER 2019勒索病毒终极防御指南
铁壁如墙-WINDOWS SERVER 2019勒索病毒终极防御指南
|
1月前
|
网络协议 数据安全/隐私保护 Windows
Windows Server 各版本搭建域控制器实现通过域管理用户(03~19)
Windows Server 各版本搭建域控制器实现通过域管理用户(03~19)
46 1
|
1月前
|
存储 数据安全/隐私保护 索引
Windows Server 各版本搭建文件服务器实现共享文件(03~19)
Windows Server 各版本搭建文件服务器实现共享文件(03~19)
162 1