Windows moible, WinCE uses. NET Compact framework for Bluetooth development 32feet. net

Source: Internet
Author: User
Tags getstream
ArticleDirectory
    • References

In the previous article, Windows Embedded source tools for Bluetooth under. NET Compact framework tells about the Bluetooth development of Windows Embedded source tools for Bluetooth. This article describes 32feet. net.
32feet. net is a shared-source project that supports cf.net 2.0 and desktop versions. net Framework, which provides the communication function of the short-range field (personal area networking IE) and supports Bluetooth and infrared (IRDA) infrared. In this article, we mainly introduce the development of Bluetooth.

32feet. Net project Homepage
Http://inthehand.com/content/32feet.aspx

32feet. net installation package and example
Http://32feet.codeplex.com/Release/ProjectReleases.aspx? Releaseid = 7373

32feet. netSource code
Http://32feet.codeplex.com/SourceControl/ListDownloadableCommits.aspx

32feet after the. NET installation package is successfully installed, two assemblies will be installed, and the cf.net version is stored in
C: \ Program Files \ In the hand Ltd \ 32feet. Net 2.3 \ assemblies \ CE2 \ inthehand. net. Personal. dll
While the desktop version is stored in
C: \ Program Files \ In the hand Ltd \ 32feet. Net 2.3 \ assemblies \ xp2 \ inthehand. net. Personal. dll
The two versions share the same API, that is, both cf.net and the complete. NET Framework are the same for 32feet. net. The differences between different platforms are described below.

Server Public   Static   Void Displaybluetoothradio ()
{
Bluetoothradio myradio = Export thradio. 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 ()
{
Listen thlistener listener =   New Descrithlistener (descrithservice. SerialPort );
Listener. Start ();
Writemessage ( " Service started! " );
Bluetoothclient Client = Listener. acceptblustmthclient ();
Writemessage ( " Got a request! " );

Stream peerstream=Client. getstream ();

StringDatatosend= "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 ( " Grouping data: "   + Data );
}
}
}

Displayincluthradio is used to display local device information and to set local Bluetooth devices as discoverable devices. If 32feet. Net 2.3 is used, that is, the latest release version, WinCE does not support reading and setting radio mode. Read 32feet. net source code will find 32feet. net was modified from Windows Embedded source tools for Bluetooth. In the initial version, Microsoft was used to connect to the namespace. windows Mobile. sharedsource. bluetooth; therefore, it inherits Microsoft. windows Mobile. sharedsource. disadvantages of Bluetooth: Reading and Writing radio mode is not supported in wince, because bthutil. DLL does not exist. The latest source code (see the link above), that is, there is no release version. This problem has been modified. The new version uses the bthreadscanenablemask and bthwritescanenablemask of btdrt. DLL to read and write radio mode. It also needs to be noted that Microsoft Windows stack in Windows Mobile supports reading and writing radio mode, but in Broadcom stack, it only supports reading and writing radio mode. In the desktop version, both Microsoft Windows stack and Broadcom stack support radio mode reading and writing.

The server uses WinSock to listen to javasthlistener, which is similar to the Windows Embedded source tools for Bluetooth.

Client Private   Static   Void Connectservice ()
{
Bluetoothclient Client =   New Bluetoothclient ();
Descrithdeviceinfo [] devices = Client. discoverdevices ();
Bluetoothdeviceinfo Device =   Null ;
Foreach (Descrithdeviceinfo 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, incluthservice. SerialPort );
Stream peerstream = Client. getstream ();

// Create storage for processing ing 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 ( " Grouping 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 ();
}
}

The biggest difference from Windows Embedded source tools for Bluetooth is that 32feet. Net 2.3 provides the self-discovery function to find nearby bluetooth devices through client. discoverdevices. In the above example, specify the device with the connection name "th_device. Winsock communication is similar to Windows Embedded source tools for Bluetooth. Client. Connect (device. deviceaddress, incluthservice. SerialPort) is used to connect a device named "bluetooth_device" and also specify the serial port service. The transmitted data is a byte stream (byte []), so any type of data can be transmitted. After receiving the response information, the client continuously sends data to the server.

 

SourceCode: Http://files.cnblogs.com/procoder/Bluetooth.rar

References

32feet. Net-user's guide, which is stored in the 32feet. net installation package.

Related Article

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.