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,如需转载请自行联系原作者


相关文章
|
20天前
|
C# 开发者 Windows
4款.NET开源、功能强大的Windows桌面工具箱
4款.NET开源、功能强大的Windows桌面工具箱
|
24天前
|
存储 数据库 数据安全/隐私保护
Windows系统部署AnyTXT Searcher并实现远程搜索本地内网设备中文件
Windows系统部署AnyTXT Searcher并实现远程搜索本地内网设备中文件
|
25天前
|
C# 开发者 Windows
WPF在.NET9中的重大更新:Windows 11 主题
这也是一个很好的学习WPF的项目,可以通过看源代码提升自己的WPF水平。 WPF Gallery演示如何在标记中指定 XAML 控件,因为每个控件页都显示用于创建每个示例的标记。它还将显示您的应用程序的所有可能的布局选项。 WPF Gallery应用还包含有关使用颜色、排版和图标开发特殊应用程序的设计指南。它还包括一个示例页,用于演示如何使用不同的控件在 WPF 应用程序中创建用户界面。 WPF在.NET 9中可以使用Win11主题的控件了,有助于WPF开发者们开发出更符合现在设计风格、更美观的界面。 希望WPF越来越好。
26 0
|
1月前
|
Windows
windows系统bat批处理 打开设备管理器
windows系统bat批处理 打开设备管理器
37 1
|
29天前
|
存储 IDE C#
C#入门:在JetBrains Rider中创建.Net Framework控制台应用程序,输出“Hello, World!”
C#入门:在JetBrains Rider中创建.Net Framework控制台应用程序,输出“Hello, World!”
74 0
|
2月前
|
开发框架 .NET API
在Windows Server 2008 R2上运行.Net 8应用
在Windows Server 2008 R2上成功运行.Net 8程序,需安装三个补丁:Windows Server 2008 R2 SP1 (KB976932)是基础更新;VC_redist.x64提供MSVC库支持;KB3063858解决.NET运行时加载`kernel.dll`的路径问题。KB3063858可能需要KB2533623。详细信息和下载链接在文中给出。
129 4
|
2月前
|
Shell 开发工具 Android开发
|
1月前
|
开发工具 开发者
Jetbrains Rider:缺少.NET Framework 4.5.2
该文主要针对开发者,指出需下载SDK而非Runtime以进行应用程序开发。当使用Rider打开旧项目出现错误提示缺少.NET Framework 4.5.2时,需从微软官网下载相应版本的SDK(推荐开发版)。安装完成后,可能需要重启Rider以消除波浪线提示。对于.NET Core项目,若提示CLI路径未找到,同样需前往微软官网下载缺失的SDK版本,如.NET Core 3.1。安装完毕后,可考虑配置环境变量。
27 0
|
2月前
|
数据挖掘 定位技术
.NET Compact Framework下的GPS NMEA data数据分析(二)转
.NET Compact Framework下的GPS NMEA data数据分析(二)转
28 0
|
2月前
|
SQL Windows
保姆级:Windows Server 2012上安装.NET Framework 3.5
保姆级:Windows Server 2012上安装.NET Framework 3.5
275 0