p2p communication using the gtalk service api. introduction peer-to-peer communication highly used...
TRANSCRIPT
![Page 1: P2P communication Using the GTalk Service API. Introduction Peer-to-Peer communication highly used in mobile devices. Very efficient to when certain factors](https://reader036.vdocuments.mx/reader036/viewer/2022082817/56649e705503460f94b6db91/html5/thumbnails/1.jpg)
P2P communicationP2P communication
Using the GTalk Service APIUsing the GTalk Service API
![Page 2: P2P communication Using the GTalk Service API. Introduction Peer-to-Peer communication highly used in mobile devices. Very efficient to when certain factors](https://reader036.vdocuments.mx/reader036/viewer/2022082817/56649e705503460f94b6db91/html5/thumbnails/2.jpg)
IntroductionIntroduction• 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)
![Page 3: P2P communication Using the GTalk Service API. Introduction Peer-to-Peer communication highly used in mobile devices. Very efficient to when certain factors](https://reader036.vdocuments.mx/reader036/viewer/2022082817/56649e705503460f94b6db91/html5/thumbnails/3.jpg)
GTalkServiceGTalkService BasicsBasics• 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)
![Page 4: P2P communication Using the GTalk Service API. Introduction Peer-to-Peer communication highly used in mobile devices. Very efficient to when certain factors](https://reader036.vdocuments.mx/reader036/viewer/2022082817/56649e705503460f94b6db91/html5/thumbnails/4.jpg)
ServiceConnection-implementing classServiceConnection-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)
![Page 5: P2P communication Using the GTalk Service API. Introduction Peer-to-Peer communication highly used in mobile devices. Very efficient to when certain factors](https://reader036.vdocuments.mx/reader036/viewer/2022082817/56649e705503460f94b6db91/html5/thumbnails/5.jpg)
ServiceConnection continued …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
![Page 6: P2P communication Using the GTalk Service API. Introduction Peer-to-Peer communication highly used in mobile devices. Very efficient to when certain factors](https://reader036.vdocuments.mx/reader036/viewer/2022082817/56649e705503460f94b6db91/html5/thumbnails/6.jpg)
Main procedureMain 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
![Page 7: P2P communication Using the GTalk Service API. Introduction Peer-to-Peer communication highly used in mobile devices. Very efficient to when certain factors](https://reader036.vdocuments.mx/reader036/viewer/2022082817/56649e705503460f94b6db91/html5/thumbnails/7.jpg)
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);
![Page 8: P2P communication Using the GTalk Service API. Introduction Peer-to-Peer communication highly used in mobile devices. Very efficient to when certain factors](https://reader036.vdocuments.mx/reader036/viewer/2022082817/56649e705503460f94b6db91/html5/thumbnails/8.jpg)
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
![Page 9: P2P communication Using the GTalk Service API. Introduction Peer-to-Peer communication highly used in mobile devices. Very efficient to when certain factors](https://reader036.vdocuments.mx/reader036/viewer/2022082817/56649e705503460f94b6db91/html5/thumbnails/9.jpg)
IntentReceiver continued …IntentReceiver continued …• onReceiveIntent(Context context, Intent
intent)1. if
(intent.getAction().equals(GTalkDataMessageReceiver.ACTION)) {//point 2}
2. Bundle b = intent.getExtras();
3. b.getString(“1”); //returns “Line 1”
4. b.getInt(“2”); //returns 32