Windows Moible, Wince 使用.NET Compact Framework进行蓝牙(Bluetooth)开发 之 32feet.NET

简介:

上篇文章 .NET Compact Framework下的Bluetooth开发 之 Windows Embedded Source Tools for Bluetooth 讲述了Windows Embedded Source Tools for Bluetooth的bluetooth开发,这篇文章讲述32feet.NET。
32feet.NET是shared-source的项目,支持CF.net 2.0以及桌面版本.NET framework,提供短距离领域(personal area networking technologie)的通信功能,支持bluetooth,Infrared(IrDA)红外等,在这篇文章,主要介绍bluetooth部分的开发。

32feet.NET 项目主页
http://inthehand.com/content/32feet.aspx

32feet.NET 安装包及例子
http://32feet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=7373

32feet.NET 源代码
http://32feet.codeplex.com/SourceControl/ListDownloadableCommits.aspx

32feet.NET 安装包成功安装后,会安装两个Assemblies,CF.net版本存放在
C:\Program Files\In The Hand Ltd\32feet.NET 2.3\Assemblies\CE2\InTheHand.Net.Personal.dll
而桌面版本存放在
C:\Program Files\In The Hand Ltd\32feet.NET 2.3\Assemblies\XP2\InTheHand.Net.Personal.dll
两个版本共享统一的API,也就是说不管CF.net还是完整的.NET framework对32feet.NET都一样。不同平台有细微的差别会下面讲述。

服务端

复制代码
public   static   void  DisplayBluetoothRadio()
{
    BluetoothRadio myRadio 
=  BluetoothRadio.PrimaryRadio;
    
if  (myRadio  ==   null )
    {
        WriteMessage(
" No radio hardware or unsupported software stack " );
        
return ;
    }
    
//  Warning: LocalAddress is null if the radio is powered-off.
    WriteMessage(String.Format( " * Radio, address: {0:C} " , myRadio.LocalAddress));
    WriteMessage(
" Mode:  "   +  myRadio.Mode.ToString());
    WriteMessage(
" Name:  "   +  myRadio.Name  +   " , LmpSubversion:  "   +  myRadio.LmpSubversion);
    WriteMessage(
" ClassOfDevice:  "   +  myRadio.ClassOfDevice.ToString()  +   " , device:  "   +  myRadio.ClassOfDevice.Device.ToString()  +   "  / service:  "   +  myRadio.ClassOfDevice.Service.ToString());

    
//  Enable discoverable mode
    myRadio.Mode  =  RadioMode.Discoverable;
    WriteMessage(
" Radio Mode now:  "   +  myRadio.Mode.ToString());
}

private   static   void  StartService()
{
    BluetoothListener listener 
=   new  BluetoothListener(BluetoothService.SerialPort);
    listener.Start();
    WriteMessage(
" Service started! " );
    BluetoothClient client 
=  listener.AcceptBluetoothClient();
    WriteMessage(
" Got a request! " );

    Stream peerStream 
=  client.GetStream();

    
string  dataToSend  =   " Hello from service! " ;

    
//  Convert dataToSend into a byte array
     byte [] dataBuffer  =  System.Text.ASCIIEncoding.ASCII.GetBytes(dataToSend);

    
//  Output data to stream
    peerStream.Write(dataBuffer,  0 , dataBuffer.Length);

    
byte [] buffer  =   new   byte [ 2000 ];
    
while  ( true )
    {
        
if  (peerStream.CanRead)
        {
            peerStream.Read(buffer, 
0 50 );
            
string  data  =  System.Text.ASCIIEncoding.ASCII.GetString(buffer,  0 50 );
            WriteMessage(
" Receiving data:  "   +  data);
        }
    }
}
复制代码

DisplayBluetoothRadio用来展现本端设备的信息,以及把本端bluetooth设备设置为可发现。如果使用32feet.NET 2.3,也就是当前最新的release版本,wince不支持读取和设置radio mode。看过32feet.NET初期版本的源码会发现32feet.NET 开始修改自Windows Embedded Source Tools for Bluetooth,最初的版本连命名空间都是使用了Microsoft.WindowsMobile.SharedSource.Bluetooth;因此继承了Microsoft.WindowsMobile.SharedSource.Bluetooth的缺点,wince下不支持读写radio mode,因为bthutil.dll不存在。最新的源码(见上面链接),也就是还没有release的版本,这个问题已经修改过来了,新版本使用btdrt.dll的BthReadScanEnableMask和BthWriteScanEnableMask来读写radio mode。同时需要说明,在windows mobile里的microsoft windows stack支持读写radio mode,但是在broadcom stack,只是支持读取,不支持写入radio mode。在桌面版本,无论microsoft windows stack或者broadcom stack的都支持radio mode的读写。

服务端的侦听BluetoothListener还是使用winsock实现和Windows Embedded Source Tools for Bluetooth的实现类似。

客户端

复制代码
private   static   void  ConnectService()
{
    BluetoothClient client 
=   new  BluetoothClient();
    BluetoothDeviceInfo[] devices 
=  client.DiscoverDevices();
    BluetoothDeviceInfo device 
=   null ;
    
foreach  (BluetoothDeviceInfo d  in  devices)
    {
        
if  (d.DeviceName  ==   " BLUETOOTH_DEVICE " )
        {
            device 
=  d;
            
break ;
        }
    }
    
if  (device  !=   null )
    {
        WriteMessage(String.Format(
" Name:{0} Address:{1:C} " , device.DeviceName, device.DeviceAddress));
        client.Connect(device.DeviceAddress, BluetoothService.SerialPort);
        Stream peerStream 
=  client.GetStream();

        
//  Create storage for receiving data
         byte [] buffer  =   new   byte [ 2000 ];

        
//  Read Data
        peerStream.Read(buffer,  0 50 );

        
//  Convert Data to String
         string  data  =  System.Text.ASCIIEncoding.ASCII.GetString(buffer,  0 50 );
        WriteMessage(
" Receiving data:  "   +  data);

        
int  i  =   0 ;
        
while  ( true )
        {
            WriteMessage(
" Writing:  "   +  i.ToString());
            
byte [] dataBuffer  =  System.Text.ASCIIEncoding.ASCII.GetBytes(i.ToString());

            peerStream.Write(dataBuffer, 
0 , dataBuffer.Length);
            
++ i;
            
if  (i  >=   int .MaxValue)
            {
                i 
=   0 ;
            }
            System.Threading.Thread.Sleep(
500 );
        }
        
//  Close network stream
        peerStream.Close();
    }
}
复制代码

和Windows Embedded Source Tools for Bluetooth最大的区别是32feet.NET 2.3提供了自发现功能,通过client.DiscoverDevices()寻找附近的bluetooth设备。在上述例子中指定连接名字为"BLUETOOTH_DEVICE"的设备。Winsock通信和Windows Embedded Source Tools for Bluetooth类似。client.Connect(device.DeviceAddress, BluetoothService.SerialPort)用于连接名字为"BLUETOOTH_DEVICE"的设备,同样指定串口服务。传输的数据为字节流(byte[]),因此可以传输任意类型的数据。客户端在接收回应信息后,不断的往服务端发送数据。

 

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

参考文档

32feet.NET — User’s Guide,存放于32feet.NET的安装包。

 

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


相关文章
|
2月前
|
API C++ Windows
Visual C++运行库、.NET Framework和DirectX运行库的作用及常见问题解决方案,涵盖MSVCP140.dll丢失、0xc000007b错误等典型故障的修复方法
本文介绍Visual C++运行库、.NET Framework和DirectX运行库的作用及常见问题解决方案,涵盖MSVCP140.dll丢失、0xc000007b错误等典型故障的修复方法,提供官方下载链接与系统修复工具使用指南。
540 2
|
5月前
|
C++ Windows
.NET Framework安装不成功,下载`NET Framework 3.5`文件,Microsoft Visual C++
.NET Framework常见问题及解决方案汇总,涵盖缺失组件、安装失败、错误代码等,提供多种修复方法,包括全能王DLL修复工具、微软官方运行库及命令行安装等,适用于Windows系统,解决应用程序无法运行问题。
357 3
|
2月前
|
开发框架 安全 .NET
Microsoft .NET Framework 3.5、4.5.2、4.8.1,适用于 Windows 版本的 .NET,Microsoft C Runtime等下载
.NET Framework是Windows平台的开发框架,包含CLR和FCL,支持多种语言开发桌面、Web应用。常用版本有3.5、4.5.2、4.8.1,系统可同时安装多个版本,确保软件兼容运行。
583 0
Microsoft .NET Framework 3.5、4.5.2、4.8.1,适用于 Windows 版本的 .NET,Microsoft C Runtime等下载
|
3月前
|
C++
提示缺少.NET Framework 3.5 安装错误:0x80070002、0x800F0950\0x80004002
.NET Framework常见问题及解决方法汇总,
475 0
|
5月前
|
C++ Windows
WindowsDLL修复专家,MSVCP**、DLL修复vcruntime**、DLL修复、`.Net Framework`缺失、DirectX类DLL修复、VC运行库修复
Windows DLL修复专家是一款专为解决因DLL文件缺失、版本错误导致的软件或游戏无法运行问题的系统工具。它支持一键扫描和修复各类DLL异常,涵盖MSVCP、vcruntime、.NET Framework、DirectX等多种常见问题。具备自动检测、备份还原功能,确保修复过程安全可靠。适用于软件报错、系统异常及新系统适配场景,降低用户手动修复门槛,提升系统稳定性与兼容性。
206 3
|
4月前
.NET Framework 3.5离线安装包合集下载
本文介绍了如何获取和安装.NET Framework运行库离线合集包。用户可通过提供的链接下载安装包,安装过程简单,按提示逐步操作即可完成。安装时可选择所需版本,工具会自动适配架构,无需手动判断,方便高效。
1648 0
|
12月前
|
区块链 C# Windows
PasteEx:一款.NET开源的Windows快捷粘贴神器
PasteEx:一款.NET开源的Windows快捷粘贴神器
183 17
|
12月前
|
Web App开发 C# Windows
一款.NET开源的Windows资源管理器标签页工具
一款.NET开源的Windows资源管理器标签页工具
176 5
|
2月前
|
安全 数据安全/隐私保护 虚拟化
Windows Server 2022 中文版、英文版下载 (2025 年 10 月更新)
Windows Server 2022 中文版、英文版下载 (2025 年 10 月更新)
399 2
Windows Server 2022 中文版、英文版下载 (2025 年 10 月更新)

热门文章

最新文章