オルタナティブ・ブログ > ビジネスをデザインするブログ >

事業開発ほどクリエイティブな行為は他に無いと思いこんでいる人間の日常

(iアプリで)モバイルFeliCaのIDmを取得してサーバに投げる方法(メモ)

»

モバイルFeliCaのIDmを取得し、サーバアプリと連携すれば、簡単なマーケティングアプリケーション(ポイントとかクーポンシステム)なら、簡単に作れます(以前のエントリに書きましたが、クリティカルな利用はお勧めできません)。

私は、モバイルFeliCaアプリを作る際、ユーザビリティの確認や、クライアントへのプレゼンに使うデモなどに利用します。

知っておくと便利ですが、ネットなどにあまりソースがないので、私のダメコードですが、貼り付けておきます。

えーと、実行環境は、DoJa 5.Xです(が、Starプロファイルでも動くとは思います・・・)。

以下、

/*
* FeliCaWithServer.java
*
* DATE : 2009/12/01 09:45
*/
import com.nttdocomo.ui.*;
import com.nttdocomo.device.felica.*;
import com.nttdocomo.io.*;
import java.io.*;
import javax.microedition.io.Connector;

/**
* FeliCaWithServer
*
* @author NAME
*/
public class FeliCaWithServer extends IApplication {

public void start() {
/*
* The program of IApplication is written here.
*/
Display.setCurrent((Frame)new MainPanel());
}
}

/**
* MainPanel
*
*/
class MainPanel extends Panel implements ComponentListener{

TextBox idm_tbx;
TextBox ret_tbx;
Button get_idm_bt;
Button put_idm_bt;
HTMLLayout lm;
String idm_str;

MainPanel() {
setTitle("FeliCa AppDemo");

idm_tbx = new TextBox("",20,1,TextBox.DISPLAY_ANY);
ret_tbx = new TextBox("",20,1,TextBox.DISPLAY_ANY);
get_idm_bt = new Button("GetIDM");
put_idm_bt = new Button("PutIDM");

lm = new HTMLLayout();
setLayoutManager(lm);
lm.begin(HTMLLayout.CENTER);

add(idm_tbx);
lm.br();
add(get_idm_bt);
lm.br();
lm.br();
add(put_idm_bt);
lm.br();
add(ret_tbx);
lm.end();

//ret_tbx.setText("Hello");

setComponentListener((ComponentListener)this);

}

public void componentAction(Component source, int type, int param) {

//IDmの取得
if (source == get_idm_bt) {
if (type == BUTTON_PRESSED) {
try{
//FeliCaをOpen
Felica.open();
//FreeAreaを取得
FreeArea fa = Felica.getFreeArea();
//IDMを取得する配列を確保
byte [] bytes = fa.getIDm();
idm_str = "";

//ループしながらデータを取得
for(int i = 0;i < bytes.length; i++){
String tmp = "";
//16進数で取得
tmp = Integer.toHexString(bytes[i] & 0xff);
//1ケタなら頭に0を足して2桁にする
if(tmp.length() < 2) tmp = "0" + tmp;
//文字列結合
idm_str += tmp;
}
//FeliCaをClose
Felica.close();
}catch(Exception e){
//エラー内容を出力
e.printStackTrace();
}
//大文字に変換
idm_str = idm_str.toUpperCase();
//TextBoxに表示
idm_tbx.setText(idm_str);
}
}
//IDmをサーバに送信
if (source == put_idm_bt) {
if (type == BUTTON_PRESSED) {
HttpConnection con = null;
BufferedReader br = null;
StringBuffer buf = new StringBuffer();
try{

//iapp/idm.aspxに配置
String url = IApplication.getCurrentApp().getSourceURL() + "idm.aspx?idm=" + idm_str;

//GETの場合はREAD,POSTならREAD_WRITE Trueはタイムアウトするかどうか
con = (HttpConnection)Connector.open(url,Connector.READ,true);
con.setRequestMethod(HttpConnection.GET);

//接続
con.connect();

//データを受信
InputStream in = con.openInputStream();
InputStreamReader ir = new InputStreamReader(in);
br = new BufferedReader(ir);
String s;
while ((s = br.readLine()) != null){
buf.append(s);
}
String str = new String(buf);

//textboxに表示
ret_tbx.setText(str);

//オブジェクトを破棄
con.close();
br.close();

}catch(Exception e){
//エラー内容表示
e.printStackTrace();
}
}
}
}

}

改行やインデントがグジャグジャなのはご勘弁を。

(誰か、ブログにソースをうまく貼り付ける方法を教えてください!!!)

さて、このサンプルでは、GETIDMというボタンと、PUTIDMというボタンを配置し、GETIDMボタンを押した際、IDMを取得し、PUTIDMでサーバに送ります。送り方はGETのパラメータとして送っています。

サーバ側にidm.aspxというパラメータを取得するアプリをおいていることを前提とします。必要に応じて、ファイル名などを変えてください。idmというパラメータを取得すれば、PHPでも、Rubyでも同じです。

docomoではFreeAreaをずどんと取得すると、その中にIDmは含まれております。

ソフトバンク、AU・・・さんでの方法はまた別の機会に。では。

Comment(0)