Sharing an account between two devices

In any sales force application, it is a common requirement to share leads, opportunities, and customers with other mobile devices. Without any form of data-sharing capability, the data is locked in your device, and the only way to get it to another device is the unattractive option of rekeying in the data piece by piece.

There are a few ways to send raw data from one device to another. Infrared (IrDA) and Bluetooth are two examples. Before we go into the details of each implementation, let's first decide on a transmission format for your data.

As you know, an account (together with its file attachments, historical records, and tasks) can be conveniently represented using a single dataset object. The easiest way to transmit this account to another device would be to serialize this dataset into a byte array for easy transmission and then to deserialize it back into a dataset at the targeted device.

Let's take a look at the code that you can use to do this:

public static byte[] SerializeDataset(DataSet Data)
{
StringWriter strWriter = new StringWriter();
string _result;
ASCIIEncoding _encoding = new ASCIIEncoding();
Data.WriteXml(strWriter);
_result = strWriter.GetStringBuilder().ToString();
return _encoding.GetBytes(_result);
}
public static DataSet DeserializeDataset(byte[] Data)
{
ASCIIEncoding _encoding = new ASCIIEncoding();
string _stringData;
_stringData = _encoding.GetString(Data, 0, Data.Length);
StringReader _reader = new StringReader(_stringData);
DataSet _ds = new DataSet();
_ds.ReadXml(_reader);
return _ds;
}

Note

Take note that this Accounts dataset contains only the file attachment metadata and not the actual file attachments themselves. You will need to write additional code to transmit these files one by one to the targeted device as well via Infrared or Bluetooth.

Now that you have decided on a suitable format for data transmission, let's see how you can transmit these byte arrays across Infrared and Bluetooth channels.

Sharing an account between two devices using Infrared (IrDA)

Although new devices in the market nowadays don't really come with Infrared capability anymore, it is still a quick and cheap form of data transmission for older devices. Infrared requires line of sight during sending and receiving but, unlike Bluetooth, does not require the time-consuming process of device pairing.

Infrared functionality is based on a client-server model and is provided through the IrDAListener and IrDAClient classes. You can place both the client side and server side code in the same class. Let's take a look at the following code for this class:

using System;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace CRMLive
{
public class InfraredService
{
private IrDAListener _listener;
private string _serviceName;
private int _maxRetryCount;
private IrDAClient _client = null;
private Thread _serverThread;
private byte[] _receivedData;
public delegate void DataReceiveEndedEventHandler();
private DataReceiveEndedEventHandler
DataReceiveEndedEvent;
public event DataReceiveEndedEventHandler
DataReceiveEnded
{
add
{
DataReceiveEndedEvent =
(DataReceiveEndedEventHandler)
System.Delegate.Combine(DataReceiveEndedEvent,
value);
}
remove
{
DataReceiveEndedEvent =
(DataReceiveEndedEventHandler)
System.Delegate.Remove(DataReceiveEndedEvent,
value);
}
}

Because the server listens for Infrared connections in a blocking call, it's a good idea to run it in a thread. The following code shows how this is done:

public bool StartServer()
{
_serverThread = new Thread(new
System.Threading.ThreadStart(ReceiveFile));
_serverThread.Start();
}
public bool StopServer()
{
try
{
if (_serverThread != null)
{
_serverThread.Abort();
_serverThread = null;
}
}
catch (Exception ex)
{
throw(ex);
}
}

This is the function that sets up the Infrared device to listen for connections. Once data is received via this connection, it is written into a byte array.

private void ReceiveFile()
{
int _bytesRead = 0;
IrDAListener listener = new
IrDAListener(_serviceName);
IrDAClient client = null;
System.IO.Stream _stream = null;
byte[] _byteArray = new byte[1048576];
string _str = string.Empty;
_receivedData = null;
try
{
listener.Start();
client = listener.AcceptIrDAClient();
_stream = client.GetStream();
int counter = 0;
do
{
_bytesRead = _stream.Read(_byteArray, counter,
8192);
counter += _bytesRead;
} while (!(_bytesRead == 0));
}
catch (Exception ex)
{
throw(ex);
}
if (_stream != null)
{
_stream.Close();
}
if (client != null)
{
client.Close();
}
listener.Stop();
if (DataReceiveEndedEvent != null)
DataReceiveEndedEvent();
_receivedData = _byteArray;
}
public bool Initialize(string ServiceName)
{
_serviceName = ServiceName;
}
public byte[] GetReceivedData()
{
return _receivedData;
}

Now let's take a look at the client-side code. You can connect to a listening IrDA server by creating a new IrDA connection with the same service name as the server.

public bool StartClient()
{
int _retryCount = 0;
do
{
try
{
_client = new IrDAClient(_serviceName);
}
catch (Exception ex)
{
if (_retryCount >= _maxRetryCount)
{
throw (ex);
}
}
_retryCount++;
} while (_client == null && _retryCount <
_maxRetryCount);
if (_client == null)
{
return false;
}
else
{
return true;
}
}

You can serialize a dataset using the functions you've created earlier into a byte array, which can then be transmitted through this IrDA channel using a stream object.

public bool SendData(byte[] byteArray)
{
System.IO.Stream _stream = null;
try
{
_stream = _client.GetStream();
_stream.Write(_byteArray, 0, byteArray.Length);
}
catch (Exception ex)
{
throw(ex);
}
if (_stream != null)
{
_stream.Close();
}
}
public int MaxRetryCounts
{
get
{
return _maxRetryCount;
}
set
{
_maxRetryCount = value;
}
}
public bool StopClient()
{
try
{
if (_client != null)
{
_client.Close();
}
}
catch (Exception ex)
{
throw(ex);
}
}
}
}

To use this class, you can call the following code on the client side to send the dataset across:

InfraredService client = new InfraredService();
client.Initialize("MYSERVICE");
client.StartClient();
client.SendData(Generic.SerializeDataset(myDataset));

At the server side, you can use the following code to set up the Infrared connection. Once the dataset is received, the DataReceiveEnded event is raised. You can then obtain the received byte array using the GetReceivedData() function.

InfraredService server = new InfraredService();
server.Initialize("MYSERVICE");
server.StartServer();

Sharing an account between two devices using Bluetooth

Bluetooth technology has gained widespread support ever since its inception and remains the best way to transfer large amounts of data wirelessly to another device. Bluetooth devices can exchange information anywhere from 10 meters to 100 meters apart.

Unlike Infrared, there is no built-in support for Bluetooth transmission in the .NET Compact Framework. Bluetooth, however, supports the serial port profile, which allows the data to be exchanged over serial communications. Fortunately, the latest version of the .NET Compact Framework provides the SerialPort class in the System.IO.Ports namespace, which allows you to easily do this. Let's take a look at what is needed in the server-side code for Bluetooth communications.

using System.Diagnostics;
using System;
using System.Data;
using System.IO;
using System.IO.Ports;
namespace CRMLive
{
public class BluetoothService
{
public delegate void DataReceiveEndedEventHandler();
private byte[] _ReceivedData;
private SerialPort _port;
private DataReceiveEndedEventHandler
DataReceiveEndedEvent;
public event DataReceiveEndedEventHandler
DataReceiveEnded
{
add
{
DataReceiveEndedEvent =
(DataReceiveEndedEventHandler)
System.Delegate.Combine(DataReceiveEndedEvent,
value);
}
remove
{
DataReceiveEndedEvent =
(DataReceiveEndedEventHandler)
System.Delegate.Remove(DataReceiveEndedEvent,
value);
}
}

At the server side, we only need to create a new SerialPort object and assign an event handler for the DataReceived event. Whenever data is received through the port, your event handler is called.

public void StartServer()
{
_port = new SerialPort();
_port.DataReceived += new
SerialDataReceivedEventHandler(DataReceived);
}
private void DataReceived(object sender,
SerialDataReceivedEventArgs e)
{
int _counter;
int _bytesRead;
byte[] Buffer = new byte[1048577];
_bytesRead = 0;
try
{
_counter = 0;
do
{
_bytesRead = _port.Read(Buffer, _counter,
Bluetoothused, for transferring account between two devices8192);
_counter += _bytesRead;
} while (!(_bytesRead == 0));
}
catch (Exception ex)
{
throw (ex);
}
_ReceivedData = Buffer;
if (DataReceiveEndedEvent != null)
DataReceiveEndedEvent();
}
public byte[] GetReceivedData()
{
return _ReceivedData;
}

On the client side, you can just as easily send data to the server. You must first create a SerialPort object, set up its connection parameters, and then use the Write() function to write binary data to the port.

public bool SendData(byte[] Data)
{
try
{
_port = new SerialPort();
_port.PortName = "COM1";
_port.BaudRate = 9600;
_port.Parity = Parity.None;
_port.DataBits = 8;
_port.StopBits = StopBits.One;
_port.Open();
_port.Write(Data, 0, Data.Length);
_port.Close();
}
catch (Exception ex)
{
throw (ex);
}
}
}
}

To use this class, you could call the following code on the client side:

BluetoothService client = new BluetoothService();
client.SendData(Generic.SerializeDataset(myDataset));

As for the server side, you can use the following code. Once the dataset is received, the DataReceiveEnded event is raised. You can then obtain the received byte array using the GetReceivedData() function.

BluetoothService server = new BluetoothService();
server.StartServer();

Tip

Sending files across devices

As the preceding code allows you to transfer binary data across devices, you can reuse the same code to send any type of object (including files) with a little extra work.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.104.109