For Windows Moible, Wince uses. NET Compact Framework for Bluetooth development-Bluetooth Virtual Serial port (Bluetooth Virtual Serial)

Source: Internet
Author: User
Tags windows 5
Document directory
  • References

The previous two Articles respectively described how to use Windows Embedded Source Tools for Bluetooth and 32feet. NET for Bluetooth development under. NET Compact Framework. The links are as follows:
Windows Embedded Source Tools for Bluetooth development under. NET Compact Framework
32feet. NET for Bluetooth development under. NET Compact Framework
This article describes the development of Bluetooth Virtual Serial Port. The so-called Bluetooth Virtual Serial Port is actually converting Bluetooth communication into a Serial Port from the software perspective ). After such conversion, the Client program using Bluetooth can operate Bluetooth like using a serial port. This application method is used to support existing applications (Legacy applications ). For example, before the emergence of Bluetooth, most mobile devices connect to the GPS receiver through a serial port. The NMEA data is also obtained through serial communication based on the development of GPS applications. For the development of gps nmea data, see gps nmea data analysis under. NET Compact Framework. For serial port development, see serial port communication under. NET Compact Framework. With the popularity of Bluetooth, mobile devices can connect to the GPS receiver through Bluetooth, so the original GPS-based application needs to re-develop the communication part to read the NMEA data, this brings a lot of trouble to existing applications. All existing applications need to rewrite the communication part. Therefore, people come up with a solution to convert Bluetooth communication into Serial Port ). The hardware uses Bluetooth for communication, and a serial port is virtualized on the software to the application. The application can support Bluetooth GPS Referer without any modification. This is like the Adapter mode in the design mode, but here we provide the original interface for the new device, so that the original Client does not need to be changed.

Because the emergence of Bluetooth Virtual Serial Port is based on the requirements supported by the existing System (Legacy System), we do not recommend using Bluetooth Virtual Serial Port for new systems in MS, instead, Winsock is used for communication. When using Winsock for Bluetooth communication, you must specify a service. Therefore, you can specify a serial port for communication. Bluetooth Virtual Serial Port and Winsock both use the RFCOMM protocol, so the two are the same. Bluetooth communication using Winsock is simpler than Bluetooth Virtual Serial Port and does not need to be configured. In addition, robust is stronger, because the Bluetooth communication using Winsock can directly listen to the Bluetooth device to close or exit the communication range, while the Bluetooth Virtual Serial Port can only be checked through Timeout.

Bluetooth Virtual Serial Port still has value because it supports existing systems (Legacy System). The following describes the development of Bluetooth Virtual Serial Port. There are two ways to create a Bluetooth Virtual Serial Port in Windows Mobile: Call the API to create a Bluetooth Virtual Serial Port and modify the Registry to create a Bluetooth Virtual Serial Port.

Call the API to create a Bluetooth Virtual Serial Port

You can call the API to create a Bluetooth Virtual Serial Port and call RegisterDevice.

HANDLE h = RegisterDevice (L "COM", index, L "btd. dll", (DWORD) & pp );

 

To create a service port, you must configure the following parameters:

PORTEMUPortParams pp;
Memset (& pp, 0, sizeof (pp ));
Pp. flocal = TRUE;
Pp. channel = channel & 0xff;

The Service port flocal is true. Channel can use RFCOMM_CHANNEL_MULTIPLE (0xfe), so that RFCOMM will automatically allocate available channels.

To create a client port, you must configure the following parameters:

 

PORTEMUPortParams pp;
Memset (& pp, 0, sizeof (pp ));
Pp. device = ba;
Pp. channel = channel & 0xff;

The Service port flocal is false. Device is the server address.

The anti-registration port uses the following API

DeregisterDevice (h );

For details, refer to the MSDN article Creating a Connection to a Remote Device Using a Virtual COM Port. For more information, see references.

In 32feet.net, these APIs are encapsulated in InTheHand. Net. Ports. javasthserialport and can be directly used. However, the authors of 32feet.net warned whether these APIs are reliable. Therefore, consider and test them carefully before using them. I tested it in windows and cannot create a Bluetooth Virtual Serial Port.

Use 32feet.net to create a service port

Public static void CreateIncomingPort ()
{
Export thserialport port = export thserialport. CreateServer (export thservice. SerialPort );
Console. WriteLine (port. PortName );
}

 

Use 32feet.net to create a client port

Public static void CreateIncomingPort ()
{
Effecthclient client = new effecthclient ();
Descrithdeviceinfo [] devices = client. DiscoverDevices ();
Required thdeviceinfo device = null;
Foreach (effecthdeviceinfo d in devices)
{
If (d. DeviceName = "th_device ")
{
Device = d;
Break;
}
}
Incluthendpoint endPoint = new incluthendpoint (device. DeviceAddress, incluthservice. SerialPort );
Export thserialport port = export thserialport. CreateClient (endPoint );
Console. WriteLine (port. PortName );
}

 

Modify the Registry to create a Bluetooth Virtual Serial Port

Because the first method is not very reliable, you can select the second method. The second method is to modify the registry
Modify the HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Bluetooth \ Serial \ Ports. However, this method only supports windows mobile5 and later systems, and does not support wince5. In addition, if you use this method, you need to restart the system.

This is the Windows Mobile Registry. You can use a program to modify the registry project. After the program is modified, You need to restart Windows Mobile.

It is a Windows 5 Registry with different structures. In Windows 5, you cannot modify the Registry to implement the Bluetooth Virtual Serial Port.

The following code comes from the SetServiceState () method in the 32feet.net pluthdeviceinfo class. Here we demonstrate how to modify the registry.

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. incluthserialport. CreateClient (alPorts [0]. ToString (), new incluthendpoint (this. DeviceAddress, incluthservice. 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 ();
}

If the state is true, it indicates registration, and if the state is false, it indicates anti-registration.

 

References

Bluetooth COM Ports On Windows CE
Windows Mobile 5.0 Bluetooth Virtual Serial Ports
Any Port in a Storm
MSDN: Creating a Connection to a Remote Device Using a Virtual COM Port

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.