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都一樣。不同平台有細微的差别會下面講述。

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[]),是以可以傳輸任意類型的資料。用戶端在接收回應資訊後,不斷的往服務端發送資料。
32feet.NET — User’s Guide,存放于32feet.NET的安裝包。
本文轉自Jake Lin部落格園部落格,原文連結:http://www.cnblogs.com/procoder/archive/2009/05/14/Windows_Mobile_Bluetooth_32feet.html,如需轉載請自行聯系原作者