JSR82:Java APIs for Bluetooth

Download Report

Transcript JSR82:Java APIs for Bluetooth

JSR82:Java APIs for
Bluetooth
02k1035 増田玲子
目次
1. Bluetooth
2. JSR82:Java APIs for Bluetooth
3. J2ME Wireless Toolkit 2.2デモ
1.Bluetooth
Bluetoothとは
「狭い範囲にある電子機器および携帯端末間
をワイヤレス接続するための技術」
2.45GHz帯のISM(Industry Science
Medical)バンドの電波を利用
最大通信距離10m
通信速度1Mps
音声、データの通信
Bluetoothの特徴
モバイル
「小型・軽量・低消費電力」を実現
無指向性
遮断物があっても通信可能
音声とデータの通信
音声とデータの双方を併用して利用可能
Bluetoothの特徴
任意ネットワーク
ピコネット
・ 1対多のワイヤレスネットワークを
構築し、同時通信を行うことが出来る。
・ 制御機能を担う端末「マスター」、そ
の他は「スレーブ」
ピコネット
スキャターネット
・ 複数の独立したピコネットが存在し、
それぞれのピコネット同士がマスター
またはスレーブを介して相互接続され
ている。
スキャターネット
Bluetoothの特徴
双方向性
双方向に情報のやり取りが可能
エリア限定
限られた範囲内において通信エリアを提供
全世界共通仕様
世界中で使用可能
ほかの無線機器との違い
Bluetooth 赤外線
(IrDA)
115.2kbp
通信速度 1Mbps
s
1m
最大通信 10m
可能距離
消費電力 小さい
やや小さ
い
指向性
なし
あり
無線LAN Zigbee
(Wi-Fi)
11Mbps 250kbps
100m
30m
大きい
かなり小
さい
なし
なし
Bluetoothプロトコルスタック
• アプリケーションとハードウェアの間に入り
Bluetoothの基本処理を行うファームウェア
L2CAP : Logical Link Control and Adaptation Protocol
HCI : Host Controller Interface
Bluetoothの技術仕様
• コア(Core)
– ワイヤレス接続の物理的、論理的な基礎を定義
• プロファイル(Profiles)
– 組み込まれる各機能ごとの相互接続性を保障するため、従わなけれ
ばならない技術要件
– 機器固有の通信手順(プロトコル)をデバイスの特性ごとに標準化し
たもの
アプリケーションを提供する複数のプロファイルの
組み合わせがコアと共に製品に実装される
Bluetoothのプロファイル仕様
すべての機器が実装しなければならない基本プロファイル:
GAP(Generic Access Profile)とSDP(Service Discovery Profile )
各デバイス用のプロファイル:その他のプロファイル
2.JSR82:Java APIs for
Bluetooth
JSR82:Java APIs for Bluetoothとは
• Bluetooth通信機能をJavaアプリケーション
から制御するためのJava APIの標準化セット
• Bluetoothバージョン1.1仕様に基く
• Bluetoothプロトコルスタックの複雑さを隠す
→ 低レベルのBluetoothの細部でなくアプリ
ケーション開発に集中できる
JSR82の概要
• Bluetooth通信の基本機能とOBEX(Object
Exchange)通信機能に関して定義された
• J2MEのオプショナルパッケージ
• CLDC以上のコンフィグレーション環境を対象
としている
• GCF(Generic Connection Framework)の
フレームワークの仕組みを利用
JSR82の仕様
• javax.bluetooth
通信の基本機能であるL2CAP,RFCOMM,SPP,SDPを扱う
• javax.obex
汎用的なOBEX通信機能(ファイル転送など)を扱う
汎用的なOBEX機能
Bluetooth通信に特化した機能
JSR82で定義されている機能
Bluetoothのアプリケーション構造
1. 初期化(JSR82では定義されていない)
2. デバイス管理
各デバイスがどのような属性を持っているかを定義・
参照する
3. デバイス、サービス検索(クライアント)
デバイスを検索し、そのデバイスで提供されている
サービスを検索する
4. サービス登録(サーバ)
5. 通信
• デバイスの管理
– javax.bluetoothパッケージ
• LocalDeviceクラス
• RemoteDeviceクラス
...
// retrieve the local Bluetooth device object
LocalDevice local = LocalDevice.getLocalDevice();
// retrieve the Bluetooth address of the local device
String address = local.getBluetoothAddress();
// retrieve the name of the local Bluetooth device
String name = local.getFriendlyName();
...
// retrieve the device that is at the other end of
//the Bluetooth Serial Port Profile connection,
// L2CAP connection, or OBEX over RFCOMM connection
RemoteDevice remote = RemoteDevice.getRemoteDevice(
javax.microedition.io.Connection c);
// retrieve the Bluetooth address of the remote device
String remoteAddress = remote.getBluetoothAddress();
// retrieve the name of the remote Bluetooth device
String remoteName = local.getFriendlyName(true);
...
• デバイス、サービスの検索
– デバイスの検索
– javax.bluetoothパッケージ
• DiscoveryListenerインターフェース
• DiscoveryAgentクラス
...
// retrieve the discovery agent
DiscoveryAgent agent 1= local.getDiscoveryAgent();
// place the device in inquiry mode
boolean complete = agent1.startInquiry();
...
DiscoveryAgent agent2 = local.getDiscoveryAgent();
// return an array of pre-known devices
RemoteDevice[] devices =
agent2.retrieveDevices(DiscoveryAgent.PREKNOWN);
...
DiscoveryAgent agent 3= local.getDiscoveryAgent();
// return an array of devices found in a previous inquiry
RemoteDevice[] devices =
agent3.retrieveDevices(DiscoveryAgent.CACHED);
...
– サービスの検索
• デバイスが検出された後に行う
• javax.bluetoothパッケージ
– UUID
– DataElement
– ServiceRecord
• サービス登録(サーバ側のみ)
– MIDP/CLDC API
• StreamConnectionNotifierクラス
• StreamConnectionクラス
– JSR82 API (javax.bluetoothパッケージ)
• ServiceRecordクラス
...
// create a server connection
StreamConnectionNotifier service =
(StreamConnectionNotifier)Connector.open("someURL");
// obtain the service record created by the server device
ServiceRecord sr = local.getRecord(service);
//accept a client connection
StreamConnection connection =
(StreamConnection)service.acceptAndOpen();
//close the connection and remove the service record
service.close();
...
• 通信
–
–
–
–
通信プロトコル層ごとに可能となるAPIがある
OBEX以外の通信用APIはjavax.bluetoothパッケージ
OBEXはjavax.obexパッケージ
通信の開始はGCFの流儀に従う
– javax.microedition.io.Connector#open(接続先のURI)
クライアント:プロトコル識別名+接続先アドレス
サーバ:プロトコル識別名+ローカルホストアドレス
プロトコル
プロトコル識別名 インターフェース
L2CAP
btl2cap
javax.bluetooth.L2CAPConnection
javax.bluetooth.L2CAPConnectionNotifier
SPP
btspp
javax.microedition.io.StreamConnection
javax.microedition.io.StreamConnectionNotifier
OBEX
btgoep
javax.obex.ClientSession
javax.obex.SessionNotifier
Bluetooth APIのプロトコル識別名とコネクションインターフェース
– SPPプロトコル通信サーバ側
...
// assuming the service UID has been retrieved
String serviceURL = "btspp://localhost:"+serviceUID.toString());
// more explicitly:
String ServiceURL = "btspp://localhost:10203040607040A1B1C1DE100;
name=SPP Server1";
try {
// create a server connection
StreamConnectionNotifier notifier =
(StreamConnectionNotifier) Connector.open(serviceURL);
// accept client connections
StreamConnection connection = notifier.acceptAndOpen();
// prepare to send/receive data byte
buffer[] = new byte[100]; String msg = "hello there, client";
InputStream is = connection.openInputStream();
OutputStream os = connection.openOutputStream();
// send data to the client
os.write(msg.getBytes());
// read data from client
is.read(buffer);
connection.close();
} catch(IOException e) { e.printStackTrace(); }
...
– SPPプロトコル通信クライアント側
...
// (assuming we have the service record)
// use record to retrieve a connection URL
String url = record.getConnectionURL( record.NOAUTHENTICATE_NOENCRYPT, false);
// open a connection to the server
StreamConnection connection = (StreamConnection) Connector.open(url);
// Send/receive data
try {
byte buffer[] = new byte[100];
String msg = "hello there, server";
InputStream is = connection.openInputStream();
OutputStream os = connection.openOutputStream();
// send data to the server os.
write(msg.getBytes);
// read data from the server
is.read(buffer);
connection.close();
}
catch(IOException e) { e.printStackTrace(); }
...
Use Case
javax.bluetooth
コアとなるJava Bluetooth APIの全
体のアーキテクチャとコンテキスト
1. Discovery Manager
アプリケーションが端末やサービスを探すときに使用
2. Generic Connection Framework(GCF)
3. Bluetooth Manager
Bluetoothスタックの様々な機能にアクセスする
Java Bluetooth Development Kits
 Complete Development Kits
ハードウェアとソフトウェアを提供
 Software Only Development Kits
実際のBluetooth搭載機として互換できる
 Simulation Development Kits
シミュレーションのみが出来る
→ J2ME Wireless Toolkit
(参考) http://www.javabluetooth.com/development_kits.html
参考文献
• http://developers.sun.com/techtopics/mobility/midp/articl
es/bluetooth1/
• http://developers.sun.com/techtopics/mobility/midp/articl
es/bluetooth2/
• http://developers.sun.com/techtopics/mobility/apis/article
s/bluetoothintro/index.html
• http://developers.sun.com/techtopics/mobility/apis/article
s/bluetoothcore/index.html
• http://www.nokia.co.jp/forum/developer/tech_doc/
• Bluetoothガイドブック、宮津和弘、日刊工業新聞社
• Javaモバイルアプリケーション、立川敬行、ソフトリサーチセ
ンター