Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Windows Mobile: Communicating with the Outside World : Page 2

Explore the various options available on the .NET Compact Framework that allow your apps to connect with the outside world.


advertisement
Receiving Data
To receive data on a Windows Mobile device using infrared, you need to use the IrDAListener class's AcceptIrDAClient() method to listen for and then accept an incoming infrared connection. Once a connection is established, you will use a Stream object to read the incoming data into a byte array.

The following code snippet shows how you can listen for incoming infrared data and then print out the received data to the output window (see Listing 1).

Sending Data
To send data to another device using infrared, you first try to establish a connection with the remote device using the IrDAClient class. Once a connection is established, you can send the dataover to the remote device using a Stream object.



The code in Listing 2 shows how you can connect to a remote device using infrared and then send a string to the device.

Note that two communicating devices must have the same service name (defined by the constant SERVICE_NAME), as specified in the constructor of the IrDAListener and IrDAClient classes.

Bluetooth/Serial Port
While infrared communication is a quick and dirty way of exchanging information, the downside is it requires line-of-sight in order to work correctly. In addition, infrared communication is inherently slow, and is thus only suitable for exchanging small amounts of information.

Another popular wireless technology widely used today is Bluetooth. Bluetooth is a short-range wireless technology that does not require line-of-sight to work. Once two devices are paired, information can be exchanged between the two devices within a distance of 10 to 100 meters.

The .NET Compact Framework does not include any managed classes for dealing with Bluetooth devices, so it's not easy to manage Bluetooth communication within your Windows Mobile application without resorting to using the native APIs via P/Invoke. However, most Bluetooth devices support the serial port profile, which means you can communicate with these devices over a serial connection. Fortunately, beginning with .NET CF v2.0, you can use the SerialPort class to manage serial communications.

Here's a list of some of the devices that you can communicate with using the serial port:

  • Windows Mobile to Windows Mobile (through Bluetooth)
  • GPS receivers (through Bluetooth)
  • Mobile phones (through Bluetooth and using AT commands)
  • RFID readers and writers (through direct hardware connection)
To use the SerialPort class in the .NET CF, you need to import the following namespace:

using System.IO.Ports;

The following statement creates an instance of the SerialPort class:

SerialPort sp = new SerialPort();

Sending Data
Before you can use the serial port, you first need to setup the required communication parameters. Next, open the connection and once the connection is established, you can send data to the remote device using the Write() or WriteLine() method:

const string MESSAGE_TO_SEND = "hello world!"; //---close the port if it is open--- if (sp.IsOpen) { sp.Close(); } try { //---set all the required comms parameter--- sp.PortName = "COM1"; sp.BaudRate = 9600; sp.Parity = Parity.None; sp.DataBits = 8; sp.StopBits = StopBits.One; //---open the connection--- sp.Open(); //---send some data over--- sp.WriteLine(MESSAGE_TO_SEND); } catch (Exception ex) { throw ex; }

Receiving Data
Receiving data from a serial port could not be easier. You simply need to handle the DataReceived event of the SerialPort class and this event is fired every-time there is incoming data:

SerialPort sp = new SerialPort(); //---wire up the DataReceived event handler--- sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);

You can read the incoming data using the Read(), ReadByte(), ReadChar(), ReadExisting(), ReadLine(), or ReadTo() methods:

void sp_DataReceived(object sender, SerialDataReceivedEventArgs e) { //---read all incoming data and write it to the output window--- Console.WriteLine(sp.ReadExisting()); }



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap