group_2

stuckwarmersMobile - Wireless

Dec 14, 2013 (3 years and 5 months ago)

201 views

P2P communication

Using the GTalk Service API

Introduction


Peer
-
to
-
Peer communication highly used in
mobile devices.


Very efficient to when certain factors such as
battery, mobility and any other external costs
are considered.


Short Message Service (SMS)?


Ideal for two very remote devices (satellite?)


Slow


Expensive


Might not carry enough data into one SMS (even
more expensive)


GTalkService

Basics


Very similar to the Java Listener and Handler
technique


Mainly consists of:


Several
Intents


used to send/receive data


initialize the service connection


A class implementing the
ServiceConnection

interface


Used once the connection is established


A class extending the
IntentReceiver

abstract class


Used when the message is received


An
IGTalkService


Used to extract a GTalk session (
IGTalkSession
)


An IGtalkSession


Used to send the data using
sendMessage(String, Intent)


ServiceConnection
-
implementing class


Must be implemented to specify what to do
during certain connection events


Two methods


onServiceConnected(ComponentName, IBinder)


Invoked by the Android platform once the requested
service connection is established


onServiceDisconnected(ComponentName)


Invoked by the Android platform once the connection is
lost (legally/illegally)

ServiceConnection continued …


onServiceConnected(ComponentName name,
IBinder service)

1.
IGTalkService gtalk_service =
IGTalkService.Stub.asInterface(service);

2.
IGTalkSession session =
gtalk_service.getDefaultSession();


onServiceDisconnected(ComponentName
name)

1.
Session = null; //no point in keeping
a non
-
connected session instance

Main procedure


Initialize two instances (more on these later on):


A ServiceConnection, and


An IntentReceiver


Request a GTalk Service by binding it to your Activity


Create a new Intent


Set the Intent’s component to
GTalkService.GTALK_SERVICE_COMPONENT


Bind the intent using
bindService(Intent, ServiceConnection,
int);



Register the IntentReceiver using
registerReceiver(IntentReceiver, IntentFilter)


The platform will automatically invoke the
onServiceConnected(ContextName, IBinder)

method


Sending a message…


Using the recently extracted session, use the
sendMessage(String, Intent);


Intent msg = new Intent();


msg.setAction(GTalkDataMessageReceiver
.ACTION);//to be explained on the next
slide


msg.putExtra(“1”, “Line 1”);


msg.putExtra(“2”, 34);


session.sendDataMessage(“john.doe”,
msg);


IntentReceiver


abstract class


Must implement one particular abstract method:
onReceiveIntent(Context, Intent)


Invoked by the Android platform once an intent is
received from some external entity


Passes the Intent which was sent by the sender


Must make sure the incoming Intent is a GTalk
message sent by the sender

IntentReceiver continued …


onReceiveIntent(Context context, Intent
intent)

1.
if
(intent.getAction().equals(GTalkDataM
essageReceiver.ACTION)) {//point 2}

2.
Bundle b = intent.getExtras();

3.
b.getString(“1”); //returns “Line 1”

4.
b.getInt(“2”); //returns 32