天天看点

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

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

32feet.NET 项目主页

<a href="http://inthehand.com/content/32feet.aspx" target="_blank">http://inthehand.com/content/32feet.aspx</a>

32feet.NET 安装包及例子

<a href="http://32feet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=7373" target="_blank">http://32feet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=7373</a>

32feet.NET 源代码

<a href="http://32feet.codeplex.com/SourceControl/ListDownloadableCommits.aspx" target="_blank">http://32feet.codeplex.com/SourceControl/ListDownloadableCommits.aspx</a>

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都一样。不同平台有细微的差别会下面讲述。

Windows Moible, Wince 使用.NET Compact Framework进行蓝牙(Bluetooth)开发 之 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);

        }

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

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的实现类似。

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

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 &gt;= int.MaxValue)

            {

                i = 0;

            }

            System.Threading.Thread.Sleep(500);

        // Close network stream

        peerStream.Close();

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

和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[]),因此可以传输任意类型的数据。客户端在接收回应信息后,不断的往服务端发送数据。

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

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

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

继续阅读