public class BluetoothManager
extends java.lang.Object
| Modifier and Type | Class and Description |
|---|---|
static interface |
BluetoothManager.BluetoothManagerCallback |
| Modifier and Type | Field and Description |
|---|---|
static int |
STATE_CONNECTED |
static int |
STATE_CONNECTING |
static int |
STATE_LISTEN |
static int |
STATE_NONE |
static int |
STATE_RELEASED |
static java.util.UUID |
UUID_SPP
SPP(Serial Port Profile)の場合のUUID
|
| Constructor and Description |
|---|
BluetoothManager(android.content.Context context,
java.lang.String name,
java.util.UUID secureProfileUUID,
BluetoothManager.BluetoothManagerCallback callback)
コンストラクタ
|
BluetoothManager(android.content.Context context,
java.lang.String name,
java.util.UUID secureProfileUUID,
java.util.UUID inSecureProfileUUID,
BluetoothManager.BluetoothManagerCallback callback)
コンストラクタ
|
| Modifier and Type | Method and Description |
|---|---|
void |
addCallback(BluetoothManager.BluetoothManagerCallback callback) |
protected void |
callOnConnect(android.bluetooth.BluetoothDevice device)
接続待ち中にリモート機器から接続された、
または#connectによるリモート機器への接続要求が成功した時
|
protected void |
callOnDisConnect()
リモート機器との接続が切断された時の処理
|
protected void |
callOnDiscover()
#startDiscoveryの呼び出しによって新しいリモート機器が見つかった時
|
protected void |
callOnFailed()
#connectによるリモート機器との接続要求が失敗した時
|
protected void |
callOnReceive(byte[] message,
int length)
リモート機器からデータを受信した時
|
void |
connect(android.bluetooth.BluetoothDevice device)
指定したリモートBluetooth機器に接続開始する(クライアントサイド)
|
void |
connect(BluetoothDeviceInfo info)
指定したリモートBluetooth機器へ接続開始する(クライアントサイド)
|
void |
connect(java.lang.String macAddress)
指定したMACアドレスを持つリモートBluetooth機器へ接続開始する(クライアントサイド)
|
static java.util.Set<android.bluetooth.BluetoothDevice> |
getBondedDevices()
ペアリング済みのBluetooth機器一覧を取得する
|
protected android.content.Context |
getContext() |
java.util.Collection<BluetoothDeviceInfo> |
getPairedDevices()
既にペアリング済みのBluetooth機器一覧を取得する
|
int |
getState()
現在の接続状態を取得
|
static boolean |
isAvailable()
端末がBluetoothに対応しているかどうかを確認
|
boolean |
isConnected()
接続されているかどうかを取得
|
static boolean |
isEnabled()
端末がBluetoothに対応していてBluetoothが有効になっているかどうかを確認
パーミッションがなければfalse
|
boolean |
isListening()
接続待機中かどうかを取得
|
boolean |
isReleased()
既に破棄されているかどうかを取得
|
protected void |
onConnect(android.bluetooth.BluetoothSocket socket,
android.bluetooth.BluetoothDevice device)
通信スレッドを生成して通信開始
接続待ちスレッドまたは接続要求スレッドでリモート機器との接続に成功した時に呼ばれる
|
void |
release()
関連するリソースを破棄する
|
void |
removeCallback(BluetoothManager.BluetoothManagerCallback callback) |
static boolean |
requestBluetoothEnable(android.app.Activity activity,
int requestCode)
端末がBluetoothに対応しているが無効になっていれば有効にするように要求する
前もってbluetoothAvailableで対応しているかどうかをチェックしておく
Bluetoothを有効にするように要求した時は#onActivityResultメソッドで結果を受け取る
有効にできればRESULT_OK, ユーザーがキャンセルするなどして有効に出来なければRESULT_CANCELEDが返る
|
static boolean |
requestBluetoothEnable(android.support.v4.app.Fragment fragment,
int requestCode)
端末がBluetoothに対応しているが無効になっていれば有効にするように要求する
前もってbluetoothAvailableで対応しているかどうかをチェックしておく
Bluetoothを有効にするように要求した時は#onActivityResultメソッドで結果を受け取る
有効にできればRESULT_OK, ユーザーがキャンセルするなどして有効に出来なければRESULT_CANCELEDが返る
|
static boolean |
requestBluetoothEnable(android.app.Fragment fragment,
int requestCode)
端末がBluetoothに対応しているが無効になっていれば有効にするように要求する
前もってbluetoothAvailableで対応しているかどうかをチェックしておく
Bluetoothを有効にするように要求した時は#onActivityResultメソッドで結果を受け取る
有効にできればRESULT_OK, ユーザーがキャンセルするなどして有効に出来なければRESULT_CANCELEDが返る
|
static boolean |
requestDiscoverable(android.app.Activity activity,
int duration)
他の機器から探索可能になるように要求する
bluetoothに対応していないか無効になっている時はIllegalStateException例外を投げる
|
static boolean |
requestDiscoverable(android.app.Fragment fragment,
int duration)
他の機器から探索可能になるように要求する
bluetoothに対応していないか無効になっている時はIllegalStateException例外を投げる
|
void |
send(byte[] message)
指定したデータを送信する
|
void |
send(byte[] message,
int offset,
int len)
指定したデータを送信する
|
void |
startDiscovery()
Bluetooth機器の探索を開始する。
|
void |
startListen()
接続待ち(サーバーサイド)
|
void |
stop()
接続中なら切断する
|
void |
stopDiscovery()
Bluetooth機器の探索中ならキャンセルする
|
public static final java.util.UUID UUID_SPP
public static final int STATE_RELEASED
public static final int STATE_NONE
public static final int STATE_LISTEN
public static final int STATE_CONNECTING
public static final int STATE_CONNECTED
public BluetoothManager(android.content.Context context,
java.lang.String name,
java.util.UUID secureProfileUUID,
BluetoothManager.BluetoothManagerCallback callback)
context - name - サービス名, 任意, nullまたは空文字列ならば端末のモデルとIDを使うsecureProfileUUID - 接続に使用するプロトコル(プロファイル)を識別するためのUUID。セキュア接続用。Android同士でつなぐなら任意で可。PC等のBluetoothシリアル通信を行うならUUID_SPPを使うcallback - public BluetoothManager(android.content.Context context,
java.lang.String name,
java.util.UUID secureProfileUUID,
java.util.UUID inSecureProfileUUID,
BluetoothManager.BluetoothManagerCallback callback)
context - name - サービス名, 任意, nullまたは空文字列ならば端末のモデルとIDを使うsecureProfileUUID - 接続に使用するプロトコル(プロファイル)を識別するためのUUID。セキュア接続用。Android同士でつなぐなら任意で可。PC等のBluetoothシリアル通信を行うならUUID_SPPを使うinSecureProfileUUID - 接続に使用するプロトコル(プロファイル)を識別するためのUUID。インセキュア接続用。Android同士でつなぐなら任意で可。PC等のBluetoothシリアル通信を行うならUUID_SPPを使う nullならsecureProfileUUIDを使うcallback - public static boolean isAvailable()
public static boolean isEnabled()
public static boolean requestBluetoothEnable(android.app.Activity activity,
int requestCode)
throws java.lang.SecurityException
activity - requestCode - java.lang.SecurityException - パーミッションがなければSecurityExceptionが投げられるpublic static boolean requestBluetoothEnable(android.app.Fragment fragment,
int requestCode)
throws java.lang.SecurityException
fragment - requestCode - java.lang.SecurityException - パーミッションがなければSecurityExceptionが投げられるpublic static boolean requestBluetoothEnable(android.support.v4.app.Fragment fragment,
int requestCode)
throws java.lang.SecurityException
fragment - requestCode - java.lang.SecurityException - パーミッションがなければSecurityExceptionが投げられるpublic static java.util.Set<android.bluetooth.BluetoothDevice> getBondedDevices()
public static boolean requestDiscoverable(android.app.Activity activity,
int duration)
throws java.lang.IllegalStateException
activity - duration - 探索可能時間[秒]java.lang.IllegalStateExceptionpublic static boolean requestDiscoverable(android.app.Fragment fragment,
int duration)
throws java.lang.IllegalStateException
fragment - duration - 0以下ならデフォルトの探索可能時間で120秒、 最大300秒まで設定できるjava.lang.IllegalStateExceptionpublic void release()
public void addCallback(BluetoothManager.BluetoothManagerCallback callback)
public void removeCallback(BluetoothManager.BluetoothManagerCallback callback)
public java.util.Collection<BluetoothDeviceInfo> getPairedDevices()
public void startDiscovery()
throws java.lang.IllegalStateException
java.lang.IllegalStateExceptionpublic void stopDiscovery()
public void startListen()
public void connect(BluetoothDeviceInfo info) throws java.lang.IllegalStateException
info - java.lang.IllegalStateExceptionpublic void connect(java.lang.String macAddress)
throws java.lang.IllegalArgumentException,
java.lang.IllegalStateException
macAddress - java.lang.IllegalArgumentException - アドレスが不正java.lang.IllegalStateExceptionpublic void connect(android.bluetooth.BluetoothDevice device)
throws java.lang.IllegalStateException
device - java.lang.IllegalStateExceptionpublic void stop()
public void send(byte[] message)
throws java.lang.IllegalStateException
message - java.lang.IllegalStateExceptionpublic void send(byte[] message,
int offset,
int len)
throws java.lang.IllegalStateException
message - offset - len - java.lang.IllegalStateExceptionpublic int getState()
public boolean isReleased()
public boolean isConnected()
public boolean isListening()
protected android.content.Context getContext()
protected void callOnDiscover()
protected void callOnConnect(android.bluetooth.BluetoothDevice device)
throws java.lang.IllegalStateException
device - java.lang.IllegalStateExceptionprotected void callOnDisConnect()
protected void callOnFailed()
protected void callOnReceive(byte[] message,
int length)
message - length - protected void onConnect(android.bluetooth.BluetoothSocket socket,
android.bluetooth.BluetoothDevice device)
socket - device -