Introduction

"And soon I will have understanding of videocassette recorders and car telephones. And when I have understanding of them, I shall have understanding of computers. And when I have understanding of computers, I shall be the Supreme Being!" (Time Bandits 1981)

Chris Ellis is a freelance Senior Flash Programmer, former Lingo Programmer and resident of London.

He has an MSc in Interactive Multimedia from Westminster University, graduating in 2000. He started programming at the age of 8 with a BBC Model B and a few books on BASIC.

What follows are some snippets from what he's currently up to.

Wednesday, 28 December 2011

RTMFP - Flex Mobile Redeems Itself

Peer-to-peer with Flex Mobile using RTMFP

As I was starting to doubt that Flex Mobile, I had an idea to link together my iPad with my Android phone. That was when I came across RTMFP. This is a really really simple way to connect multiple devices on the same wireless network. Just create a NetConnection and connect with 'rtmfp'. Then join a group and you're good to send and receive messages from all members of the group!

I created a simple app which will transfer images between the devices. I am sure this will not be allowed by Apple as it means you can transfer images without iTunes. Great for me though!

It should be possible to transfer any type of file between any devices that support AIR using this technique. It's not super quick (takes about 4 seconds to transfer an image) but could probably be speeded up by using FZip of something similar.

To use devices that are not connected via the same LAN is still possible, but you'd need to use Adobe's Cirrus.

http://labs.adobe.com/technologies/cirrus/

which is not currently available for commercial use.

Here is the code snippet to connect devices on the same LAN.

localNc = new NetConnection();
localNc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
localNc.connect("rtmfp:");


Then you need to join a group:

//define the group
var groupspec:GroupSpecifier = new GroupSpecifier("MyGroupName");
groupspec.ipMulticastMemberUpdatesEnabled = true;
groupspec.multicastEnabled = true;
groupspec.routingEnabled = true;
groupspec.postingEnabled = true;
//multicast ip should begin with 224 at least and port should be higher than 1024
groupspec.addIPMulticastAddress("238.254.254.1:30302");
//creates/joins existing group
netGroup = new NetGroup(localNc, groupspec.groupspecWithAuthorizations());
netGroup.addEventListener(NetStatusEvent.NET_STATUS, netStatus);


To send an image, you first need to turn it into a byte array:

var size:Rectangle = new Rectangle(0,0,loader.content.width,loader.content.height);
var byteArray:ByteArray = Bitmap(loader.content).bitmapData.getPixels(size);
byteArray.position = 0;
//send object to listeners
netGroup.sendToAllNeighbors({type:GALLERY_IMAGE, size:size,ba:byteArray});


When this is received it can be deserialsed. Here I display the image and attempt to save it to the gallery as well:

var byteArray:ByteArray = data.ba;
var bitmapData:BitmapData = new BitmapData( data.size.width, data.size.height, false,0x00000000);
bitmapData.setPixels(new Rectangle(0,0, data.size.width, data.size.height),byteArray);
myImage.source = bitmapData;
if(CameraRoll.supportsAddBitmapData){
//this will probably not be allowed by Apple as I believe it is against their control policy
trace('saving image to gallery');
var cr:CameraRoll = new CameraRoll();
cr.addBitmapData(bitmapData);
}else{
trace('cannot save image to gallery');
}


The full source for this experiment can be found here:

SOURCE

It's rough, but just shows the process of how to do the above.

0 comments: