文章目錄
上篇文章 .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的安裝包。