天天看點

在Windows mobile 6.0模拟器中實作藍牙資料采集

在Windows mobile 6.0模拟器中實作藍牙資料采集

一、Windows mobile 6.0中藍牙驅動的加載

大家知道,Windows Mobile 6.0 SDK自帶了cellular emulator和fake GPS,使得我們可以在模拟器上調試radio級的phone call和基于GPS的地理位置應用。

我們來看看實作Bluetooth的架構,參考下圖(來源于Bluetooth for Microsoft Device Emulator),主要包括emulator端和PC端:

在Windows mobile 6.0模拟器中實作藍牙資料采集

1.1 軟硬體條件

1. Emulator端

Bluetooth HCI Transport Driver

Serial Port Driver

Microsoft Remote Tools Framework remote agent

2.  PC端

Microsoft Remote Tools Framework desktop plugin

FreeBT USB Driver runtime

FreeBT USB Driver

Connected Bluetooth USB device

    注意:作者采用了FreeBT這個開源的工程來控制Bluetooth的接入。

    在Windows Mobile模拟器上使用藍牙的必要條件有:

1. Visual studio 2005 with SP1 或者 Visual studio 2008

2. Windows Mobile 5.0 Pocket PC/Smartphone SDK and/or Windows Mobile 6 Standard/Professional SDK emulator images

3. Microsoft Remote Tools Framework 1.00

5. 一個具有USB接口的Bluetooth dongle

1.2 模拟環境搭建   

(一)安裝FreeBT藍牙驅動

在Windows Mobile模拟器上使用藍牙,即在PC端安裝下載下傳的FreeBT的藍牙驅動,方法自然是通過"windows裝置管理器",為新的硬體裝置(也就是連接配接的Bluetooth USB device)添加下載下傳過來的驅動(主要是找到fbtusb.inf檔案)。具體如下:

1、打開Windows裝置管理器。

2、在裝置管理器,找到藍牙裝置将裝置用作FreeBT USB接口。

3、點選滑鼠右鍵并選擇"更新驅動程式..."在彈出的菜單。

4、選擇"否,暫時不",然後點選"下一步>"。

5、選擇"安裝從清單或指定位置",然後點選"下一步>"。

6、選擇"不要搜尋。我将選擇要安裝的驅動",然後點選"下一步>"。

7、選擇裝置驅動程式,然後點選"從磁盤安裝... ".

8、在查找檔案對話框中,浏覽到fbtusb.inf檔案,然後點選"下一步>"。

9、當一個"硬體安裝警告"出現後,點選"仍然繼續"。

10、一旦安裝完畢後,點選"完成"。

11、裝置管理器現在應顯示"FreeBT USB驅動"在名單的USB控制器。

(二)安裝加載Bluetooth for Microsoft Device Emulator

1、安裝FreeBT USB驅動中描述了藍牙USB驅動安裝部分。

2、安裝要求如描述的安裝步驟。

3、 在Remote Tools Framework plugin中運作BthEmulManager.cetool,将其和模拟器建立連接配接。

4、從清單中選擇一個仿真圖像。

5、等待模拟器連接配接。

6、選擇"Bluetooth for Microsoft Device Emulator"節點。

7、如果你有一個藍牙裝置連接配接成功,那麼藍牙裝置資訊(位址,制造商,人機互動版本和LMP版本)将被顯示。現在,藍牙應該運作在你的模拟器上。如果有一個錯誤代碼,則将被顯示。如圖9所示。

在Windows mobile 6.0模拟器中實作藍牙資料采集
在Windows mobile 6.0模拟器中實作藍牙資料采集

8、要清除通訊記錄,點選滑鼠右鍵,選擇"清除"。

9、目前裝置資訊複制到剪貼闆,選擇"藍牙為微軟裝置模拟器"節點,點選滑鼠右鍵,選擇"拷貝到剪貼闆"。

10、關掉藍牙的模拟器,點選"連接配接"菜單,選擇"脫離…模拟器"。

11、微軟遠端工具結構允許你在同一時間啟動兩個裝置模拟器。如果您已經安裝了兩個或兩個以上FreeBT USB裝置,點選"視窗"菜單并選擇"切分視窗檢視"。重複步驟3-6。你将得到兩個同時運作的藍牙驅動仿真器。

12、很可能要啟用/禁用裝置端日志記錄。如果您啟用"裝置日志記錄"複選框,然後将啟用遠端登入在模拟器上。關注\ \ Temp的模拟器目錄。應該有建立btd_bthemul_0.txt,btd_BthEmulAgent_0.txt,btd_bthemulcom_0.txt檔案。

13、很可能要啟用/禁用桌面端日志記錄。如果您啟用"桌面日志記錄"複選框,那麼就将啟用本地記錄。檢視你的安裝目錄,應該有建立BthEmulManager.txt檔案。

14、很可能要啟用/禁用通訊記錄。通訊記錄可以看到仿真器和藍牙裝置之間的通信活動。

(三)連通測試

連接配接建立以後,藍牙裝置的資訊(Address, Manufacturer, H

在Windows mobile 6.0模拟器中實作藍牙資料采集

CI Version, LMP Version)就會顯示在界面上。

在程式啟動後,首先搜尋附近的藍牙裝置,搜尋過程完成以後,将其裝置的名字和藍牙位址顯示在下拉清單中,然後就可以進行雙向的消息發送和接收了。程式運作界面如下圖10所示:

需要注意的是,在裝置藍牙的時候,如果沒有将"對其他裝置可見"選項打上勾,即隻是将藍牙打開,如下圖所示。

可能無法正常通信,應用程式會提示無法發送消息。選中"對其他裝置可見"之後,就能夠正常通信了,如下圖12所示:

在藍牙配對中,當Windows Mobile 找到了藍牙裝置金瓯,金瓯設定的藍牙連接配接密碼是1234。如圖13所示。

在Windows mobile 6.0模拟器中實作藍牙資料采集
成功後,選擇打開藍牙時不會出錯
在Windows mobile 6.0模拟器中實作藍牙資料采集
正在搜尋裝置過程中
在Windows mobile 6.0模拟器中實作藍牙資料采集
搜尋完成
在Windows mobile 6.0模拟器中實作藍牙資料采集
建立連接配接過程中要求輸入連接配接密碼
在Windows mobile 6.0模拟器中實作藍牙資料采集
選擇藍牙服務——序列槽連接配接服務
在Windows mobile 6.0模拟器中實作藍牙資料采集
建立成功COM端口

二、Bluetooth操作的軟體實作

由于藍牙無線協定在手機中的應用較為廣泛,是以,無論是Windows Mobile SDK,還是其他第三方商業或開源軟體組織,都對藍牙程式設計有較好的支援。如著名的OpenNETCF,在其早期的版本中就提供了OpenNETCF.Bluetooth.WidComm庫,而微軟則有官方版本的Microsoft Bluetooth Stack提供,目前,比較流行的庫有32feet.net。三方的位址清單如下:

  1. InTheHand.NET項目(即32feet.net)
  2. OpenNETCF項目

在.NET Compact Framework下進行Bluetooth開發有幾個可選解決方案

  • 可以P/Invoke直接調用Bluetooth的API(btdrt.dll)
  • 使用MS的Windows Embedded Source Tools for Bluetooth
  • 使用32feet.NET庫

而這些庫裡,對藍牙序列槽服務的支援均存在一定的缺陷,如Feet32.Net的作者甚至聲稱其中建立序列槽連接配接的代碼是不安全的。這個問題在模拟器下更顯突出,原因是模拟器下,對序列槽的支援僅是從COM0~COM2,而在使用SDK中列舉時,得到的結果卻是COM0~COM10,這樣,如果随機選擇了大于COM2的虛拟端口,程式便會報錯。而這個問題,在手動建立藍牙虛拟序列槽服務時也會不時發生。由于暫時沒有真實的Mobile裝置進行測試,不能完全證明,是由于模拟器的問題還是SDK使用不當的問題,留待購置了Mobile裝置後再來研究。

OpenNETCF的新版本中,不知何故去除了WidComm的包,但仍有網友在其源代碼上進行部分的改造利用,下文就針對上面所列的兩種庫的使用進行簡要的說明。

2.1 利用32feet.net實作Windows Mobile上的藍牙資料傳輸

(一)32feet.NET簡介

在Windows mobile 6.0模拟器中實作藍牙資料采集

32feet.net是由InTheHand公司所屬的一個開源項目組織,專門針對基于.NET平台的無線資料傳輸(如藍牙,紅外等)提供技術支援。目前已經釋出的元件有:

  • Bluetooth(藍牙)
  • IrDA(紅外)
  • ObjectExchange(OBEX檔案交換)

對藍牙的支援需要裝置相容 Microsoft Bluetooth協定棧。CE.NET4.2或XP以上的作業系統。

(二)32feet.NET類庫結構

表1:InTheHand類庫結構

名字空間 描述
InTheHand The InTheHand namespace contains classes for working with the Uri class for personal area networks.
InTheHand.Net The InTheHand.Net namespace contains classes for working with addressing on personal area networks.
InTheHand.Net.Bluetooth The InTheHand.Net.Bluetooth namespace contains classes for working with Bluetooth functionality such as Radio hardware.
InTheHand.Net.Bluetooth.AttributeIds The InTheHand.Net.Bluetooth.AttributeIds namespace contains definitions of Service Discovery Protocol attributes.
InTheHand.Net.IrDA The InTheHand.Net.IrDA namespace contains classes for working with Infrared functionality.
InTheHand.Net.Mime The InTheHand.Net.Mime namespace holds types that are used to represent Multipurpose Internet Mail Exchange (MIME) headers
InTheHand.Net.Ports The InTheHand.Net.Ports namespace contains classes for working with legacy virtual COM ports over Bluetooth.
InTheHand.Net.Sockets The InTheHand.Net.Sockets namespace provides added functionality for working with IrDA and Bluetooth Sockets.
InTheHand.Windows.Forms The InTheHand.Windows.Forms namespace contains forms related to networking functionality.

現僅列舉32feet.NET中的Buletooth及Sockets名字空間,以供參考。

表2:InTheHand.Net.Bluetooth名字空間

描述
AttributeIdLookup Retrieves the name of the SDP Attribute ID with the given value in the specified Attribute ID class sets. Implementing Enum-like behaviour.
BluetoothProtocolDescriptorType Configures what type of element will be added by the ServiceRecordBuilder for the ProtocolDescriptorList attribute.
BluetoothRadio Represents a Bluetooth Radio device.
BluetoothSecurity Handles security between bluetooth devices.
BluetoothService Standard Bluetooth Profile identifiers.
BluetoothWin32Authentication Provides Bluetooth authentication services on desktop Windows.
BluetoothWin32AuthenticationEventArgs Provides data for an authentication event.
ClassOfDevice Describes the device and service capabilities of a device.
DeviceClass Class of Device flags as assigned in the Bluetooth specifications.
ElementType Represents the types that an SDP element can hold.
ElementTypeDescriptor Represents the type of the element in the SDP record binary format, and is stored as the higher 5 bits of the header byte.
HardwareStatus Specifies the current status of the Bluetooth hardware.
LanguageBaseItem Represents a member of the SDP LanguageBaseAttributeIdList, Attribute which provides for multi-language strings in a record.
LinkPolicy Flags to describe Link Policy.
Manufacturer Manufacturer codes.
MapServiceClassToAttributeIdList Gets a list of enum-like classes containing SDP Service Attribute Id definitions for a particular Service Class.
RadioMode Determine all the possible modes of operation of the Bluetooth radio.
ServiceAttribute Holds an attribute from an SDP service record.
ServiceAttributeId A Service Attribute Id identifies each attribute within an SDP service record.
ServiceClass
ServiceElement Holds an SDP data element.
ServiceRecord Holds an SDP service record.
ServiceRecordBuilder Provides a simple way to build a ServiceRecord, including ServiceClassIds and ServiceNames attributes etc.
ServiceRecordCreator Creates a Service Record byte array from the given ServiceRecord object.
ServiceRecordHelper Some useful methods for working with a SDP ServiceRecord including creating and accessing the ProtocolDescriptorList for an RFCOMM service.
ServiceRecordParser Parses an array of bytes into the contained SDP ServiceRecord.
ServiceRecordUtilities Utilities method working on SDP ServiceRecords, for instance to produce a 'dump' of the record's contents.
SizeIndex Represents the size of the SDP element in the record binary format, and is stored as the lower 3 bits of the header byte.
StringWithLanguageBaseAttribute Indicates that the field to which it is applied represents an SDP Attribute that can exist in multiple language instances and thus has a language base offset applied to its numerical ID when added to a record.

表3: InTheHand.Net.Sockets名字空間

Type Description
AddressFamily32 Specifies additional addressing schemes that an instance of the Socket class can use.
BluetoothClient Provides client connections for Bluetooth network services.
BluetoothDeviceInfo Provides information about an available device obtained by the client during device discovery.
BluetoothListener Listens for connections from Bluetooth network clients.
BluetoothProtocolType Specifies additional protocols that the Socket class supports.
BluetoothSocketOptionLevel Defines additional Bluetooth socket option levels for the SetSocketOption(SocketOptionLevel, SocketOptionName, Int32) and GetSocketOption(SocketOptionLevel, SocketOptionName) methods.
BluetoothSocketOptionName Defines Socket configuration option names for the Socket class.
IrDACharacterSet Describes the character sets supported by the device.
IrDAClient Makes connections to services on peer IrDA devices.
IrDADeviceInfo Provides information about remote devices connected by infrared communications.
IrDAHints Describes an enumeration of possible device types, such as Fax.
IrDAListener Places a socket in a listening state to monitor infrared connections from a specified service or network address.
IrDASocketOptionLevel Defines additional IrDA socket option levels for the SetSocketOption(SocketOptionLevel, SocketOptionName, Int32) and GetSocketOption(SocketOptionLevel, SocketOptionName) methods.
IrDASocketOptionName Socket option constants to set IrDA specific connection modes, and get/set IrDA specific features.
2.2 基于32feet.NET的藍牙程式設計

Bluetooth的應用十分廣泛,基于Bluetooth的通信程式開發主要有以下幾個步驟:

  1. 服務端
  • 設定本裝置為可發現。
  • 公開服務給其他Bluetooth裝置通路。
  • 接受其他Bluetooth裝置的連結。
  • 與連結上的Bluetooth裝置進行通信。
  1. 用戶端
  • 發現周邊Bluetooth裝置。
  • 主動與被發現的裝置發起連接配接。
  • 與連結上的Bluetooth裝置進行通信。
  1. 藍牙功能設定

(1)服務端

開啟藍牙

public
					static
					void DisplayBluetoothRadio()

           
{

           
BluetoothRadio myRadio = BluetoothRadio.PrimaryRadio;

           
if (myRadio == null)

           
{

           
WriteMessage("No radio hardware or unsupported software stack");

           
return;

           
}

           
// Enable discoverable mode
				
           
myRadio.Mode = RadioMode.Discoverable;

           
}

           

開啟服務

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。

服務端的偵聽BluetoothListener還是使用winsock實作和Windows Embedded Source Tools for Bluetooth的實作類似。

(2)用戶端

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();

           
}

           
}

           

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[]),是以可以傳輸任意類型的資料。用戶端在接收回應資訊後,不斷的往服務端發送資料。

  1. 藍牙配對

Connect() 函數的第二個參數是服務類型,十分重要,通信雙方必須使用同樣的服務類型。如果通信雙方的程式都是由我們負責開發,可以使用通用的服務類型,例如BluetoothService.SerialPort。但是如果要與第三方裝置進行通信,需要查出該裝置的服務類型,在裝置端的通信服務類型一般編寫在Firmware(固件)裡面,而且會按照規範編寫,例如Bluetooth耳機會使用BluetoothService.Handsfree。

try

{

BluetoothAddress deviceAddress = deviceAddresses[listBoxDevices.SelectedItem.ToString()];

client.SetPin(deviceAddress, textBoxPin.Text.Trim());

//client.Connect(deviceAddress, BluetoothService.Handsfree); //if connect ot Hands free.

client.Connect(deviceAddress, BluetoothService.SerialPort); //if connect to cell phone and so forth.

MessageBox.Show("Pair successful.");

//transfer data..

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

SetPin()的功能是設定連接配接密碼的,其函數原型如下:

[DllImport("btdrt.dll", SetLastError=true)]

public static extern int BthSetPIN(byte[] pba, int cPinLength, byte[] ppin);

(三) 虛拟序列槽

所謂Bluetooth Virtual Serial Port,其實是從軟體的角度看,把Bluetooth的通信轉化成Serial Port(序列槽)。經過這樣的轉換後,使用Bluetooth的Client程式可以像使用序列槽一樣操作Bluetooth。這個應用方式的出現是為了支援現有應用(Legacy system,遺産應用)。舉個例子,在Bluetooth出現以前,大部分移動裝置都是通過序列槽連接配接的GPS receiver的,基于GPS應用程式的開發也就通過的序列槽通信取出NMEA data。

所謂Bluetooth Virtual Serial Port,其實是從軟體的角度看,把Bluetooth的通信轉化成Serial Port(序列槽)。經過這樣的轉換後,使用Bluetooth的Client程式可以像使用序列槽一樣操作Bluetooth。這個應用方式的出現是為了支援現有應用(Legacy system,遺産應用)。舉個例子,在Bluetooth出現以前,大部分移動裝置都是通過序列槽連接配接的GPS receiver的,基于GPS應用程式的開發也就通過的序列槽通信取出NMEA data。随着Bluetooth的普及,移動裝置可以通過Bluetooth來連接配接GPS receiver了,那麼原先基于GPS的應用程式需要重新開發通信部分去讀取NMEA data,這為現有應用帶來很多麻煩,所有的現有應用都需要重寫通信部分,是以人們想出解決方法,把Bluetooth的通信轉化成Serial Port(序列槽)。硬體上使用Bluetooth來進行通信,在軟體上虛拟一個序列槽給應用程式,應用程式不需要任何的修改就可以支援Bluetooth的GPS Receiver了。這就像設計模式裡面的Adapter模式,但是這裡是為新裝置提供原有的接口,使得原先的Client不需要更改。

由于Bluetooth Virtual Serial Port的出現基于對現有系統(Legacy System)支援的需求,是以對于新的系統,MS不推薦使用Bluetooth Virtual Serial Port,而是直接使用Winsock進行通信。在使用Winsock進行Bluetooth通信需要指定服務,是以可以指定使用序列槽服務進行通信。Bluetooth Virtual Serial Port和Winsock的Bluetooth通信都是使用RFCOMM協定,是以兩者等同。使用Winsock的Bluetooth通信比Bluetooth Virtual Serial Port更簡單,不需要配置。而且更強壯(robust),因為使用Winsock的Bluetooth通信可以直接監聽到藍牙裝置關閉或者離開通信範圍,而Bluetooth Virtual Serial Port隻能通過Timeout來檢查。

由于支援現有系統(Legacy System),Bluetooth Virtual Serial Port還是有存在的價值,下面講述Bluetooth Virtual Serial Port的開發。在Windows Mobile下有兩種方法可以建立Bluetooth Virtual Serial Port:調用API建立Bluetooth Virtual Serial Port和修改系統資料庫建立Bluetooth Virtual Serial Port。

  1. 使用 32feet.net API

在32feet.net裡面,建立虛拟序列槽這些API封裝在InTheHand.Net.Ports.BluetoothSerialPort,可以直接使用。但是32feet.net的作者提醒這些API是不是可信賴的(unreliable),是以在使用之前請要謹慎考慮和詳細測試。我在wince 5下測試過,不能成功建立Bluetooth Virtual Serial Port。

  • 使用32feet.net建立服務端口

public static void CreateIncomingPort()

{

BluetoothSerialPort port = BluetoothSerialPort.CreateServer(BluetoothService.SerialPort);

Console.WriteLine(port.PortName);

}

  • 使用32feet.net建立用戶端口

public static void CreateIncomingPort()

{

BluetoothClient client = new BluetoothClient();

BluetoothDeviceInfo[] devices = client.DiscoverDevices();

BluetoothDeviceInfo device = null;

foreach (BluetoothDeviceInfo d in devices)

{

if (d.DeviceName == "BLUETOOTH_DEVICE")

{

device = d;

break;

}

}

BluetoothEndPoint endPoint = new BluetoothEndPoint(device.DeviceAddress, BluetoothService.SerialPort);

BluetoothSerialPort port = BluetoothSerialPort.CreateClient(endPoint);

Console.WriteLine(port.PortName);

}

(2)使用修改系統資料庫法建立虛拟序列槽

if (state)

{

//write registry settings for WM5 Serial Port support

//get available ports

Microsoft.Win32.RegistryKey rkPorts = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Bluetooth\\Serial\\Ports", true);

string[] supportedPorts = (string[])rkPorts.GetValue("SupportedPorts");

System.Collections.ArrayList alPorts = new System.Collections.ArrayList(supportedPorts);

//check availability

foreach (string deviceid in rkPorts.GetSubKeyNames())

{

Microsoft.Win32.RegistryKey rkDevice = rkPorts.OpenSubKey(deviceid);

//remove port from arraylist if unavailable

string port = rkDevice.GetValue("Port").ToString();

int nullPos = port.IndexOf('\0');

if (nullPos > -1)

{

port = port.Substring(0, nullPos);

}

if (alPorts.Contains(port))

{

alPorts.Remove(port);

}

rkDevice.Close();

}

if (alPorts.Count == 0)

{

throw new InvalidOperationException("No ports available");

}

//write port details to registry

Microsoft.Win32.RegistryKey rkNewPort = rkPorts.CreateSubKey(this.DeviceAddress.ToString("8"));

rkNewPort.SetValue("KeepDCB", 0);

rkNewPort.SetValue("RemoteDCB", 0);

rkNewPort.SetValue("Encryption", 0);

rkNewPort.SetValue("Authentication", 0);

rkNewPort.SetValue("Port", alPorts[0]);

rkNewPort.SetValue("Server", 0);

rkNewPort.Close();

rkPorts.Close();

//try open port now

try

{

InTheHand.Net.Ports.BluetoothSerialPort.CreateClient(alPorts[0].ToString(), new BluetoothEndPoint(this.DeviceAddress, BluetoothService.SerialPort));

}

catch

{

}

}

else

{

//find and remove registry entries

Microsoft.Win32.RegistryKey rkPorts = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Bluetooth\\Serial\\Ports", true);

foreach (string deviceAddress in rkPorts.GetSubKeyNames())

{

if (deviceAddress == this.DeviceAddress.ToString("8"))

{

rkPorts.DeleteSubKeyTree(deviceAddress);

break;

}

}

    rkPorts.Close();

}

當state為true時為注冊,當state為false時為反注冊。

posted on 2010-04-22 16:16  易學 閱讀( ...) 評論( ...) 編輯 收藏

轉載于:https://www.cnblogs.com/wantfei/archive/2010/04/22/1718097.html

繼續閱讀