×
Namespaces

Variants
Actions

Archived:Flash LiteとJava MEを接続する方法

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

We do not recommend Flash Lite development on current Nokia devices, and all Flash Lite articles on this wiki have been archived. Flash Lite has been removed from all Nokia Asha and recent Series 40 devices and has limited support on Symbian. Specific information for Nokia Belle is available in Flash Lite on Nokia Browser for Symbian. Specific information for OLD Series 40 and Symbian devices is available in the Flash Lite Developers Library.

Article Metadata
Article
Translated:
By fnjwikimng
Last edited: hamishwillee (14 Aug 2013)

最近、Java MEやFlash Liteをサポートする新世代のモバイル端末が市場に広く出回っていて、(複数技術を使った)ハイブリッドアプリケーションのために門戸を開いています。Jarpaは、そのビジネス上の可能性を模索する形で、Flash Lite開発者がそのコンテンツをJava Meアプリケーションとして配信・拡張できる仕組みとして作られたものです。

Jarpaの作者は、Felipe Andrade, Alessandro Pace です。JarpaはApache Software Licenseの約定下であれば、ソフトウェアソリューションに対して費用はかかりません。


Contents

ダウンロード

コードとそのサンプルについては、Google code中のサイトからダウンロードできます。

ソケットクラス (Java ME)

データを読み書きするために、ソケット接続を行います。

/*  Jarpa 1.05 - Java Packaging for Flash Lite Developers
*
* @author Felipe Andrade <felipe.andrade@i2tecnologia.com.br>
*
* http://www.i2tecnologia.com.br/jarpa/
* http://blog.felipeandrade.org/
* http://www.biskero.org
*/

 
package com.i2tecnologia.jarpa;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.ServerSocketConnection;
import javax.microedition.io.SocketConnection;
 
public class Connection implements Runnable {
public static Connection _instance;
private ServerSocketConnection _server;
private InputStream _in;
private SocketConnection _client;
public String _status = "not connected";
public boolean _isConnected = false;
private OutputStream _out;
 
public static Connection getInstance() {
if(_instance == null) {
_instance = new Connection();
}
 
return _instance;
}
 
public Connection() {
_status = "connecting...";
new Thread(this).start();
}
 
public void write(String aData) throws Exception {
_status = "sending data...";
aData += '\0';
 
synchronized (this) {
if(_client != null) {
_out.write(aData.getBytes());
_out.flush();
}
}
}
 
public String read() {
StringBuffer recv = new StringBuffer();
 
synchronized (this) {
if(_client != null) {
try {
int ch;
if(_in.available() > 0) {
_status = "reading data...";
while(( ch = _in.read()) != '\0') {
recv.append( (char) ch );
}
}
} catch (Exception e) {
_status = "read: " + e.toString();
}
}
}
 
return recv.toString();
}
 
public void close() {
try {
_server.close();
_client.close();
} catch (IOException e) {
_status = "close: " + e.toString();
}
}
 
public void run() {
try {
_status = "connecting...";
_server = (ServerSocketConnection) Connector.open("socket://:9002");
_client = null;
} catch (Exception e) {
_status = "Run (1): " + e.toString();
}
 
try {
_status = "waiting for incoming connection...";
_client = (SocketConnection) _server.acceptAndOpen();
_client.setSocketOption(SocketConnection.DELAY, 0);
_client.setSocketOption(SocketConnection.KEEPALIVE, 2);
 
_out = _client.openOutputStream();
_in = _client.openInputStream();
 
_status = "client connected...";
} catch (Exception e) {
_status = "Run (2): " + e.toString();
}
}
}

MIDletクラス (Java ME)

カスタムデータをFlash Liteクライアントに送る役割を担います。これにより、ロケーションなどのサービスに直接アクセスすることができます。

/*  Jarpa 1.05 - Java Packaging for Flash Lite Developers
*
* @author Felipe Andrade <felipe.andrade@i2tecnologia.com.br>
*
* http://www.i2tecnologia.com.br/jarpa/
* http://blog.felipeandrade.org/
* http://www.biskero.org
*/

 
package com.i2tecnologia.jarpa;
 
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
//import javax.microedition.location.*;
 
public class Jarpa extends MIDlet implements CommandListener, Runnable {
private Form _form;
private Command _cmdExit;
private StringItem _status;
private StringItem _statusRead;
 
// values short than 20 sometimes close the midlet
private static int MAX_INTERVAL = 20;
 
// target folder
private String _targetFolder = "file:///E:/Others/Jarpa_105.swf";
 
// sample increment
private int _inc = 0;
 
public void startApp() {
_form = new Form("Jarpa Debug");
_cmdExit = new Command("Exit", Command.EXIT, 1);
_status = new StringItem("Teste STR", "");
_statusRead = new StringItem("", "");
 
_form.append(_status);
_form.append(_statusRead);
 
_form.addCommand(_cmdExit);
_form.setCommandListener(this);
Display.getDisplay(this).setCurrent(_form);
 
new Thread(this).start();
}
 
public void pauseApp() {}
public void destroyApp(boolean aCond) {}
 
public void run() {
try {
Resources.getInstance().copyResources(_targetFolder);
platformRequest(_targetFolder);
} catch(Exception e) {
_status.setText("Error: " + e.toString());
}
 
while(true) {
_status.setText("Status: " + Connection.getInstance()._status);
 
try {
String sent = "" + (++_inc);
String recv = "";
 
try {
Connection.getInstance().write(sent);
recv = Connection.getInstance().read();
} catch (Exception e) {
destroyApp(true);
notifyDestroyed();
}
 
_statusRead.setText("Write: " + sent + "\nRead: " + recv + " / len: " + recv.length() );
Thread.sleep(MAX_INTERVAL);
} catch (Exception e) {
e.printStackTrace();
}
}
}
 
public void commandAction(Command aCmd, Displayable aDisplay) {
destroyApp(true);
notifyDestroyed();
}
}

Resourcesクラス (Java ME)

Resourcesクラスは、JAR中のSWFファイルを端末フォルダにコピーするために使われます。

/*  Jarpa 1.05 - Java Packaging for Flash Lite Developers
*
* @author Felipe Andrade <felipe.andrade@i2tecnologia.com.br>
*
* http://www.i2tecnologia.com.br/jarpa/
* http://blog.felipeandrade.org/
* http://www.biskero.org
*/

 
package com.i2tecnologia.jarpa;
 
import java.io.DataOutputStream;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
 
public class Resources {
public static Resources _instance;
 
// data from the swf
private InputStream _incomingData;
 
private String _contentFolder = "";
private String _toFolder = "";
private String _fromFolder = "";
 
public static Resources getInstance() {
if(_instance == null)
_instance = new Resources();
 
return _instance;
}
 
public void copyResources(String aFolder) {
_toFolder = aFolder;
int index = aFolder.lastIndexOf('/');
String fileName = aFolder.substring(index + 1, aFolder.length());
 
_contentFolder = aFolder.substring(0, index);
_fromFolder = "/res/" + (fileName);
 
try {
// get resources from the jar
this._incomingData = this.getClass().getResourceAsStream(this._fromFolder);
 
// open a connection (sign problems ... on N95 only working in the memory card)
FileConnection fConn = (FileConnection)Connector.open(this._toFolder);
FileConnection fContent = (FileConnection)Connector.open(this._contentFolder);
 
if(!fContent.exists()){
fContent.mkdir();
}
 
fContent.close();
 
// if not exists creates the file
if (!fConn.exists()) {
fConn.create();
 
int ch = 0;
DataOutputStream dataStorage = fConn.openDataOutputStream();
 
// write to the file
while ((ch = this._incomingData.read()) > -1) {
dataStorage.write(ch);
}
 
// closes
dataStorage.flush();
dataStorage.close();
 
if(this._incomingData != null)
this._incomingData.close();
}
 
fConn.close();
} catch (Exception e) {
 
}
}
}

Jarpaコネクタ (Flash Lite)

このActionScriptクラスにより、Flash Lite開発者がJava MEアプリケーションからファイルに接続し、データを制御することができます。

/**
Jarpa - Jarpa Framework
Copyright (C) 2008 i2tecnologia
*/

 
import mx.utils.Delegate;
import mx.events.EventDispatcher;
 
/**
Jarpa
@version 1.05
@author: Felipe Andrade
*/

class com.i2tecnologia.jarpa.Jarpa extends EventDispatcher {
private var host_str:String;
private var port_num:Number;
private var jarpa_xmls:XMLSocket;
private var isConnected_bool:Boolean = false;
 
/**
Jarpa(host_str:String, port_num:Number)
@host_str: server host
@port_num: port number ( > 1024 )
 
Constructor
*/

public function Jarpa(host_str:String, port_num:Number) {
this.host_str = host_str;
this.port_num = port_num;
 
this.jarpa_xmls = new XMLSocket();
}
 
/**
connect():Void
 
Connect to local server
*/

public function connect():Void {
this.jarpa_xmls.connect(this.host_str, this.port_num);
this.jarpa_xmls.onConnect = Delegate.create(this, onConnect);
this.jarpa_xmls.onClose = Delegate.create(this, onClose);
}
 
/**
onConnect(success_bool:Boolean)
@success_bool: connected or not
 
Connection handler
*/

public function onConnect(success_bool:Boolean):Void {
setStatus(true);
dispatchEvent({type: "onJarpaConnect",
status:success_bool});
stateReading();
}
 
/**
write(data_str:String)
@data_str: write a message to socket
 
Write a message to socket
*/

public function write(data_str:String):Void {
this.jarpa_xmls.send(data_str);
}
 
/**
onReadData(data_str:Boolean)
@data_str: receive a message from socket
 
Asynchronous handler
*/

public function onReadData(data_str:String):Void {
dispatchEvent({type: "onReadData",
data:data_str});
}
 
/**
onClose():Void
 
Invoked when the server is closed
*/

public function onClose():Void {
setStatus(false);
dispatchEvent({type: "onJarpaClose"})
}
 
/**
stateReading():Void
 
Start handling messages
*/

public function stateReading():Void {
this.jarpa_xmls.onData = Delegate.create(this, onReadData);
}
 
/**
setStatus(status_bool:Boolean):Void
@status: boolean value that indicates the status of application
 
Set status of application
*/

public function setStatus(status_bool:Boolean):Void {
this.isConnected_bool = status_bool;
}
 
/**
getStatus():Boolean
 
Get status of application
*/

public function getStatus():Boolean {
return this.isConnected_bool;
}
}

コンテンツへのコネクタ (Flash Lite)

新しいFlash Liteファイル(.fla)上に、次のコードを記述する必要があります。

stop();
fscommand2("FullScreen", true);
fscommand2("SetSoftkeys", "left", "right");
import com.i2tecnologia.jarpa.Jarpa;
 
var connection:Jarpa = new Jarpa('', 9002);
 
/* ***********************************************************
Jarpa
************************************************************** */

var handler_obj:Object = new Object();
var id:Number;
 
handler_obj.onJarpaConnect = function(evt_obj:Object):Void {
preloader_mc._visible = false;
 
if(evt_obj.status) {
status_txt.text = "Connected.";
} else {
status_txt.text = "Connection failed.";
id = setInterval(function() {
connection.connect();
preloader_mc._visible = true;
status_txt.text = "Reconnecting.";
clearInterval(id);
}, 4000);
}
}
 
handler_obj.onJarpaClose = function(evt_obj:Object):Void {
status_txt.text = "Connection closed.";
}
 
handler_obj.onReadData = function(evt_obj:Object):Void {
status_txt.text = evt_obj.data;
}
 
connection.addEventListener("onJarpaConnect", handler_obj);
connection.addEventListener("onJarpaClose", handler_obj);
connection.addEventListener("onReadData", handler_obj);
 
connection.connect();
status_txt.text = "Connecting to Java ME.";
 
/* ***********************************************************
Event keys
************************************************************** */

var handlerKey_obj:Object = new Object();
handlerKey_obj.onKeyDown = function() {
switch(Key.getCode()) {
case ExtendedKey.SOFT1:
connection.write("JARPA");
break;
case ExtendedKey.SOFT2:
fscommand2("Quit");
break;
}
}
 
Key.addListener(handlerKey_obj);
This page was last modified on 14 August 2013, at 09:52.
210 page views in the last 30 days.