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都一样。不同平台有细微的差别会下面讲述。
服务端
{
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的实现类似。
客户端
{
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[]),因此可以传输任意类型的数据。客户端在接收回应信息后,不断的往服务端发送数据。
源代码: https://files.cnblogs.com/procoder/Bluetooth.rar
参考文档
32feet.NET — User’s Guide,存放于32feet.NET的安装包。
出处:http://procoder.cnblogs.com
本作品由Jake Lin创作,采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。 任何转载必须保留完整文章,在显要地方显示署名以及原文链接。如您有任何疑问或者授权方面的协商,请给我留言。