エミュレータ上でbluetooth通信する方法

このエントリーをはてなブックマークに追加
はてなブックマーク - エミュレータ上でbluetooth通信する方法
Share on Facebook
Post to Google Buzz
Bookmark this on Yahoo Bookmark
Bookmark this on Livedoor Clip
Share on FriendFeed

iアプリエミュレータ上でbluetooth通信をする方法を紹介します。ちょっとコツが必要です。
まずは、以下のようなアプリを作成します。

import java.io.DataInputStream;
import java.io.OutputStream;

import com.docomostar.StarApplication;
import com.docomostar.device.Bluetooth;
import com.docomostar.device.RemoteDevice;
import com.docomostar.io.SPPConnection;
import com.docomostar.ui.Button;
import com.docomostar.ui.Component;
import com.docomostar.ui.ComponentListener;
import com.docomostar.ui.Display;
import com.docomostar.ui.HTMLLayout;
import com.docomostar.ui.Label;
import com.docomostar.ui.Panel;

public class Main extends StarApplication {

	public void started(int arg0) {
		Display.setCurrent(new BluetoothPanel());

	}

	private class BluetoothPanel extends Panel implements ComponentListener{

		private Button sendDeviceReadyBtn;
		private Button sendBtn;
		private Button receiveDeviceReadyBtn;
		private Button receiveBtn;
		private OutputStream out;
		private DataInputStream in;
		private Label result;

		public BluetoothPanel(){

			//コンポーネントのイベントリスナー登録
			setComponentListener(this);
			sendDeviceReadyBtn = new Button("送信側準備");
			receiveDeviceReadyBtn = new Button("受信側準備");
			sendBtn = new Button("データ送信");
			receiveBtn = new Button("データ受信");
			result = new Label("");

			//以下、画面レイアウト等の設定
			HTMLLayout layout = new HTMLLayout();
			setLayoutManager(layout);
			add(sendDeviceReadyBtn);
			layout.br();
			add(sendBtn);
			layout.br();
			layout.br();
			layout.br();
			add(receiveDeviceReadyBtn);
			layout.br();
			add(receiveBtn);
			layout.br();
			layout.br();
			add(result);
		}

		public void componentAction(Component source, int type, int param) {
			if(source.equals(sendDeviceReadyBtn)){
				//データ送信ボタンが押下された場合
				try{
					Bluetooth bluetooth = Bluetooth.getInstance();
					RemoteDevice remote = bluetooth.searchAndSelectDevice();
					remote.isAvailable(Bluetooth.SPP);
					SPPConnection connection = (SPPConnection)remote.connect(Bluetooth.SPP);
					out = connection.openOutputStream();
				}catch(Throwable t){
					t.printStackTrace();
				}
			}
			else if(source.equals(sendBtn)){
				try{
					if(out != null){
						//接続し終わったら、ストリームを通じてデータを書き込みます
						out.write(100);
						out.flush();
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			else if(source.equals(receiveDeviceReadyBtn)){
				try{
					Bluetooth bluetooth = Bluetooth.getInstance();
					RemoteDevice remote = bluetooth.scan();
					remote.isAvailable(Bluetooth.SPP);
					SPPConnection connection = (SPPConnection)remote.accept(Bluetooth.SPP);
					in = new DataInputStream(connection.openInputStream());
				}catch(Throwable t){
					t.printStackTrace();
				}
			}
			else if(source.equals(receiveBtn)){
				try{
					if(in != null){
						//接続し終わったら、ストリームを通じてデータを書き込みます
						int a = in.read();
						if(a == 100){
							//通信に成功したら結果を表示
							result.setText("通信成功:" + a);
						}
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
	}
}

データの送信側では、上の2つのボタンを使用します。データの受信側では、下の2つのボタンを使用します。
では、早速動かしてみましょう。

まず、エミュレータを2つ起動します。

Bluetoothはトラステッド機能の為、エミュレータにてトラステッド許可を設定します。

Bluetooth機能の使用許可のYesにチェックを入れてください

トラステッド機能を使用する為には、TrustedAPIDというものが必要です。こちらに任意の11桁の数値を入力します。

ビルドを行い、エラーが出なければ成功です

アプリを起動します。上のエミュレータが送信側端末、下のエミュレータが受信側端末とします。
2つともアプリが起動したら、送信側エミュレータの「送信側準備」ボタンを押下します。

送信側エミュレータにて、以下のような画面に切り替わりますので、このタイミングで受信側エミュレータにて、「受信側準備」ボタンを押下します。

すると、接続先を選択するリストが表示されますので、「確定」を選択します。

成功すると、以下のような画面になります。ここで、両方のエミュレータにて「了解」を選択します。

bluetooth接続が確立すると、以下の画面に戻ります。ここで、送信側エミュレータにて「データ送信」ボタンを押下し、次に、受信側エミュレータにて「データ受信」ボタンを押下します。

通信に成功しました。

カテゴリー: Bluetooth, 未分類   パーマリンク

エミュレータ上でbluetooth通信する方法 への1件のコメント

  1. m_yamada より:

    コメントありがとうございます。
    申し訳ないのですが、Windows Mobile emulatorでは動かした事がないのでわかりません。
    こちらの記事は、あくまでiappli emulatorに限った話になっています。

コメントをどうぞ

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

*

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Additional comments powered by BackType