Click on any phrase to play the video from that point.
[metallic whirring]
[ADOBE DEVELOPER CONNECTION]
[Tomas Krcha - Platform Evangelist] Hi there.
This is Tom Krcha, platform evangelist at Adobe.
Today I am going to show you how we can actually control the computer software
running in Flash or AIR using your phone.
So how it works is that you basically connect from the phone to the computer
using local WiFi connection and peer-to-peer.
And it's very simple, so let's have a look.
I have for you here the mockup of application which is looking like this.
Here we can see an arrow which we are going to rotate
using the phone accelerometer.
And also we have some text area where you can look to see information
and also a Debug button for sending some data out to the network.
So let's get to the code.
First of all, we have to define some NetConnection.
So I just call the variable connection, and it's going to be NetConnection class like that.
Then we need some group, so you actually put all the devices
which you want to connect together into one group,
and this group is instance of class NetGroup.
The other thing we want to add is some kind of boolean variable
saying isConnected,
and we're going to set it as False.
And then we just prepare the function connect, which is going to do the connection itself.
So we just initialize the connection variable like that, new NetConnection,
and we add EventListener so we're going to listen to NetStatusEvent.NET_STATUS
like that.
And we just connect.
Normally, if you are doing NetConnection you are connecting to some kind of server
which is somewhere on the network.
But with the local peer-to-peer connections it is different.
You just say that you want to connect to RTMFP protocol like that.
I am not specifying any server, I am not specifying anything,
I am just saying that I want to connect to RTMFP protocol, no server, nothing.
So right now we just defined a NetStatusEvent, which is very simple.
Like that, and we're going to log some information coming from the network.
event.info.code
And then we also switch through the event.info.code
to actually react on the events coming from the network.
So one of these events is called NetConnection.Connect.Success,
which means that we actually connected to the NetConnection itself.
Then we have to define NetGroup.Connect.Success,
which means that we have successfully joined a group.
And there we are again to define one more but on this later on.
So right now when we actually connect to the NetConnection,
which happens actually when the application is complete,
where we connect to the NetConnection,
we actually call the function setupGroup or joinGroup--it doesn't matter how we call it.
And in this function setupGroup we are going to set up the group, obviously.
So what are we going to do here?
First we define a group specifier, which is some kind of class used for
the group specification, very simply said.
And we give it some name, so let's say some localDeviceConnections
and we also define one thing here, that we want to enable routing.
Routing is a very cool method for the device communication on the local area network
because it has the lowest latency possible you can get using group functions.
Then we say that we want to enable IPMulticastMemberUpdates,
which means that we are communicating on the local network.
And we also say that we want to add IPMulticastAddress,
which takes care of the communication on the local area network.
And all the devices communicating together must be on the same IPMulticastAddress.
So the lowest IPMulticastAddress is 224.0.0.0 on the port 1024,
but we are just going to make it a little bit unique like that,
and we're going to change the port a little bit, for instance, like that
just to ensure that new one is going to interfere into the communication
in our application.
And the last thing we enable here is multicast.
Maybe we want to use it, maybe not.
So we will see.
Anyway, right now we just need to define a group, so we say new NetGroup,
and here we pass that connection and also groupspec.groupspecWithAuthorizations.
That means that we can send and receive the data so it's not only read only
that is the case without authorizations.
And we also hook it up to EventListener.
So I just choose this and say
addEventListener (NetSTatusEvent.NET_STATUS, netStatus)
like that.
And we are ready to go.
So right now we are connecting to the network.
Then we are setting up the group here.
And the last thing obviously is going to connect this button with a handler
so when we click on the button we're going to actually send some data to the group
and then we are going to receive the data here in the NetStatus switch.
So first of all, let's check if everything is working okay.
Right now we can see that we have connected to the group,
and when I launch to Windows we can also see that these two windows
have connected together.
So what we're going to do here in the last step
is that we add a clickHandler.
And when we click on it, we actually send some data to all neighbors.
We're going to use directed routing for that.
We're going to just send to some random number,
so I just round it and generate some random number, like from 0 to 360.
When we receive this number, we're going to rotate the arrow.
So NetGroup.SendTo.Notify.
That's the event where you actually get a message from the directed routing.
So we just say arrow.rotation
and here we just pass event.info.message which contains the angle
we want to rotate the arrow.
So when we open this up in two Windows and click on this button,
you can see here that we are actually rotating the arrow
by the information we get from this window to this window.
So right now it actually works in two different windows in this browser.
But what we want to do is to actually send the information
about accelerometer from this device to the application here over the local WiFi
using the peer-to-peer. It's very simple.
So what we have to do for this is that we have to prepare another application,
and it's going to be action screen mobile application.
Here we just copy and paste most of the code we actually wrote here.
So I just take this complete code and copy it here.
We just add imports and also group specifier
and that's basically it.
The only difference here is that we actually connect here in the constructor.
Once we connect, we also set up some listener
to actually listen to accelerometer.
So we also create new variable called accelerometer:Accelerometer; like that.
We initialize it here
and we addEventListener like that, (AccelerometerEvent.UPDATE.
And we can just create new function, onAccel, for instance.
This is what we're going to define here at the bottom of the code.
AccelerometerEvent
And the last thing we actually do here is that we say
group.sendToAllNeighbors
and we're going to send to the event accelerometer Y,
which means that we are rotating the phone like that
so we want to reflect rotation to the arrow which is displayed on the display here
on my laptop.
We're going to multiply it by minus 90 degrees.
and also round it.
And that's it.
So when we launch it, it actually launches on the device.
So we just click Run.
Right now it's copying the application to my Android phone
and it's opening the application.
And when we open this application here, we can see that they actually hook up together
and I can control the arrow here from my phone.
So that's basically it, and now you can imagine the power of these local WiFi connections
because you can do much more here.
You can basically do any kind of control or joystick, virtual joystick.
For instance, you can display cards in a poker game in the local WiFi network and so on.
So it's pretty nice to actually be able to control your app from the mobile phone.
It can be app not lying only on the desktop here;
it can be app, for instance, on some plasma screen on the street
or it can be also app, for instance, running on Google TV and so on.
So that's basically it: local connections over peer-to-peer WiFi.
[Tom's Blog - flashrealtime.com]
I thank you very much for watching. See you next time.
[ADOBE DEVELOPER CONNECTION]
[metallic whirring]



