Android で Bluetooth Low Energy(BLE) を使用する
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
]
開始行:
[[Software/Android]]
*AndroidでBLE(Bluetooth Low Energy)を使用する [#q1a24fbb]
#contents
*1.BLE(Bluetooth Low Energy)について [#u0b9f77a]
Bluetooth Low Energyの略でBluetooth 4.0規格の一部です。
特徴:~
超低消費電力(ボタン電池で一年以上動作)~
2.4GHz帯を使用~
これまでのBluetooth(Classic Bluetooth)との互換性は無し~
通信距離は2.5mから10m程度~
同時接続数に制約無し(Classic Bluetoothでは最大7台)~
*2.BLEの通信についての基礎知識 [#s2588a83]
BLEはGATT(Generic Attribute Profile)と呼ばれるクライアン...
デバイス(サーバ)の仕様はプロファイル、サービス、キャラク...
#ref(./gatt.png);
**プロファイル [#m0e8536d]
GATT(Generic Attribute Profile)~
BLEデバイス間のデータ交換の論理的な仕様です。~
データ構造の読み出し、データの読み書き、データの変更通知...
GATTはサービス、キャラクタリスティック、ディスクリプタと...
プロファイルは1つ以上のサービスから構成されます。~
**サービス [#i7451cdc]
サービスはプロファイルの機能を表します。~
サービスは複数の入れ子になっているサービスと複数のキャラ...
**キャラクタリスティック [#f771bd50]
単一の値を持った属性を表します。~
自身の値と値へのアクセス方法を定義するプロパティ、複数の...
**ディスクリプタ [#y275f105]
ディスクリプタはキャラクタリスティックに付加情報が必要な...
キャラクタリスティックによってはディスクリプタが無いもの...
*3.AndroidでのBLE [#uf86151e]
Androidは4.3(API Level 18)から対応しています。
**Bluetooth パッケージ [#q183fa1b]
android.bluetooth.*~
**BluetoothManager [#ec596057]
Bluetoothの機能管理を行います。~
BluetoothManagerからBluetoothAdapterを取得します。~
**BluetoothAdapter [#bc15a37e]
端末のBluetoothを制御します。~
Bluetoothデバイスのスキャンを行います。~
**BluetoothGatt [#p5ea484e]
GATTプロファイルを操作します。~
サービスの検索、キャラクタリスティックのread/write、ディ...
**BluetoothDevice [#iceea4d5]
BluetoothAdapterのスキャン結果として受け取ります。~
Bluetoothデバイス情報です。~
**BluetoothGattService [#rcc5482d]
(GATT)サービス情報です。~
キャラクタリスティック(BluetoothGattCharacteristic)を保持...
**BluetoothGattCharacteristic [#i5902700]
キャラクタリスティック情報です。~
ディスクリプタ(BluetoothGattDescriptor)を保持しています(...
**BluetoothGattDescriptor [#r1b8377e]
ディスクリプタ情報です。~
*4.アプリの実装 [#hed0d226]
**パーミッション [#u66c19cc]
マニフェスト(AndroidManifest.xml)に以下の記述を追加します。
パーミッション設定
<uses-permission android:name="android.permission.BLUETO...
<uses-permission android:name="android.permission.BLUETO...
アプリがBLE対応端末のみに対応していることを宣言するには以...
<uses-feature android:name="android.hardware.bluetooth_l...
**初期化 [#f580a6a7]
BluetoothManager bluetoothManager = (BluetoothManager)ge...
mBluetoothAdapter = bluetoothManager.getAdapter();
**BLEの有効化 [#b957ce6f]
実行時にBluetoothが利用可能かどうかをチェックし、無効にな...
BluetoothManager bluetoothManager = (BluetoothManager)ge...
BluetoothAdapter mBluetoothAdapter = bluetoothManager.ge...
if (mBluetoothAdapter == null) {
return false;
}
// Bluetooth機能が有効になっているかのチェック。無効の場...
if (mBluetoothAdapter.isEnabled()) {
// Bluetoothが利用可
} else {
// Bluetoothが利用不可。確認ダイアログを表示
Intent enableBtIntent = new Intent(BluetoothAdapter.ACT...
activity.startActivityForResult(enableBtIntent, 1);
}
**スキャン [#bb5b1b12]
// 10秒後にスキャンを停止
private static final long SCAN_PERIOD = 10000;
...
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCall...
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
...
}
private LeDeviceListAdapter mLeDeviceListAdapter;
...
// デバイススキャン後のコールバック関数
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, i...
byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
String msg = "name =" + device.getName() + ", bondst...
+ device.getBondState() + ", address = "
+ device.getAddress() + ", type" + devic...
+ ", uuid = " + device.getUuid().toStrin...
Log.d("Scan", msg);}
});
}
};
**デバイスとの接続 [#qb926923]
onLeScan()で返されるBluetoothDevice#connectGatt()を呼び出...
BluetoothDevice:
BluetoothGatt connectGatt(Context context, boolean autoC...
***BluetoothGattCallbackの実装 [#l508541c]
private final BluetoothGattCallback callback = new Blue...
// 接続状態が変更されたときに実行される
@Override
public void onConnectionStateChange(BluetoothGatt ga...
// 接続された
if (newState == BluetoothProfile.STATE_CONNECTED...
Log.i(TAG, "Connected to GATT server.");
// サービスを検索する
Log.i(TAG, "Attempting to start service disc...
// 切断された
} else if (newState == BluetoothProfile.STATE_DI...
Log.i(TAG, "Disconnected from GATT server.");
}
}
@Override
// サービスの検索結果を返す
public void onServicesDiscovered(BluetoothGatt gatt,...
// 成功
if (status == BluetoothGatt.GATT_SUCCESS) {
// サービスのリストを取得
List<BluetoothGattService> gattService = get...
for(BluetoothGattService service : gattServi...
UUID uuid = service.getUuid();
// サービスからCharacteristicsのリストを...
List<BluetoothGattCharacteristic> charas...
// Characteristic に Notification の受信要求を設定
for(BluetoothGattCharacteristic charasti...
//
gatt.setCharacteristicNotification(c...
}
}
} else {
Log.w(TAG, "onServicesDiscovered received: "...
}
}
// 読み込み通知
@Override
public void onCharacteristicRead(BluetoothGatt gatt,...
if (status == BluetoothGatt.GATT_SUCCESS) {
// Characteristicの読込成功
}
}
// 書き込み通知
@Override
public void onCharacteristicWrite(BluetoothGatt gat...
if (status == BluetoothGatt.GATT_SUCCESS) {
// Characteristicの書込成功
}
...
};
**読込 [#p547ccfa]
読込は非同期の為、読込要求と読込通知とからなります。
BluetoothGatt#readCharacteristic()を実行します。~
結果はBluetoothGattCallback#onCharacteristicRead()に返り...
***読込要求 [#f7d05b19]
mBluetoothGatt.readCharacteristic(characteristic);
***読込通知 [#p01a6e6d]
public void onCharacteristicRead(BluetoothGatt gatt,Blue...
if (status == BluetoothGatt.GATT_SUCCESS) {
// Characteristicの読込成功
}
}
**書込 [#wf89816b]
書込は非同期の為、書込要求と書込通知とからなります。
BluetoothGatt#writeCharacteristic()を実行します。~
結果はBluetoothGattCallback#onCharacteristicWrite()に返り...
***書込要求 [#o6f832ac]
mBluetoothGatt.writeCharacteristic(characteristic);
***書込通知 [#c6b1e070]
public void onCharacteristicWrite(BluetoothGatt gatt,Blu...
if (status == BluetoothGatt.GATT_SUCCESS) {
// Characteristicの書込成功
}
}
**接続の解除 [#wb4063ed]
BluetoothGatt#close()を実行します。
*複数のデバイスを接続する [#l0c9daee]
複数のデバイスを接続する場合、同時に接続処理を行うのでは...
接続結果を待たずに別のデバイスの接続を行うと動作が不安定...
また、スキャン実行中の接続も動作が不安定になります。~
**[ケース] [#e1877340]
デバイスA,B,Cを接続するケース
**[処理手順] [#y13c25bb]
1.デバイスAの接続~
BluetoothDevice#connectGatt()~
2.接続結果を待つ~
BluetoothGattCallback#onConnectionStateChange()~
3.デバイスBの接続~
BluetoothDevice#connectGatt()~
4.デバイスBの接続結果を待つ~
BluetoothGattCallback#onConnectionStateChange()~
5.デバイスCの接続~
BluetoothDevice#connectGatt()~
6.デバイスCの接続結果を待つ~
BluetoothGattCallback#onConnectionStateChange()~
*Embedded Technology 2015のデモについて [#nc9416ae]
Android端末(Nexus7 2013)で6台のBLEデバイスに接続し、操作...
**同時接続 [#nd6c7738]
BLEデバイスを複数接続~
6台のデバイスを順次接続しています~
#youtube(FCtvmpk3XSE);
**LED照明 [#j228c80f]
明るさを6段階で変更~
個別/同時に操作
#youtube(e8rH0BX88Nc);
**スイッチ [#c8b5b54b]
3個のリレーを搭載し、個別/同時にON/OFF操作
#youtube(115ipQ69tik);
**LED電球 [#jc7c0129]
LED電球(RGB)の色を操作
#youtube(u_36gwa3jsE);
----
RIGHT:YASHIRO Masayuki
終了行:
[[Software/Android]]
*AndroidでBLE(Bluetooth Low Energy)を使用する [#q1a24fbb]
#contents
*1.BLE(Bluetooth Low Energy)について [#u0b9f77a]
Bluetooth Low Energyの略でBluetooth 4.0規格の一部です。
特徴:~
超低消費電力(ボタン電池で一年以上動作)~
2.4GHz帯を使用~
これまでのBluetooth(Classic Bluetooth)との互換性は無し~
通信距離は2.5mから10m程度~
同時接続数に制約無し(Classic Bluetoothでは最大7台)~
*2.BLEの通信についての基礎知識 [#s2588a83]
BLEはGATT(Generic Attribute Profile)と呼ばれるクライアン...
デバイス(サーバ)の仕様はプロファイル、サービス、キャラク...
#ref(./gatt.png);
**プロファイル [#m0e8536d]
GATT(Generic Attribute Profile)~
BLEデバイス間のデータ交換の論理的な仕様です。~
データ構造の読み出し、データの読み書き、データの変更通知...
GATTはサービス、キャラクタリスティック、ディスクリプタと...
プロファイルは1つ以上のサービスから構成されます。~
**サービス [#i7451cdc]
サービスはプロファイルの機能を表します。~
サービスは複数の入れ子になっているサービスと複数のキャラ...
**キャラクタリスティック [#f771bd50]
単一の値を持った属性を表します。~
自身の値と値へのアクセス方法を定義するプロパティ、複数の...
**ディスクリプタ [#y275f105]
ディスクリプタはキャラクタリスティックに付加情報が必要な...
キャラクタリスティックによってはディスクリプタが無いもの...
*3.AndroidでのBLE [#uf86151e]
Androidは4.3(API Level 18)から対応しています。
**Bluetooth パッケージ [#q183fa1b]
android.bluetooth.*~
**BluetoothManager [#ec596057]
Bluetoothの機能管理を行います。~
BluetoothManagerからBluetoothAdapterを取得します。~
**BluetoothAdapter [#bc15a37e]
端末のBluetoothを制御します。~
Bluetoothデバイスのスキャンを行います。~
**BluetoothGatt [#p5ea484e]
GATTプロファイルを操作します。~
サービスの検索、キャラクタリスティックのread/write、ディ...
**BluetoothDevice [#iceea4d5]
BluetoothAdapterのスキャン結果として受け取ります。~
Bluetoothデバイス情報です。~
**BluetoothGattService [#rcc5482d]
(GATT)サービス情報です。~
キャラクタリスティック(BluetoothGattCharacteristic)を保持...
**BluetoothGattCharacteristic [#i5902700]
キャラクタリスティック情報です。~
ディスクリプタ(BluetoothGattDescriptor)を保持しています(...
**BluetoothGattDescriptor [#r1b8377e]
ディスクリプタ情報です。~
*4.アプリの実装 [#hed0d226]
**パーミッション [#u66c19cc]
マニフェスト(AndroidManifest.xml)に以下の記述を追加します。
パーミッション設定
<uses-permission android:name="android.permission.BLUETO...
<uses-permission android:name="android.permission.BLUETO...
アプリがBLE対応端末のみに対応していることを宣言するには以...
<uses-feature android:name="android.hardware.bluetooth_l...
**初期化 [#f580a6a7]
BluetoothManager bluetoothManager = (BluetoothManager)ge...
mBluetoothAdapter = bluetoothManager.getAdapter();
**BLEの有効化 [#b957ce6f]
実行時にBluetoothが利用可能かどうかをチェックし、無効にな...
BluetoothManager bluetoothManager = (BluetoothManager)ge...
BluetoothAdapter mBluetoothAdapter = bluetoothManager.ge...
if (mBluetoothAdapter == null) {
return false;
}
// Bluetooth機能が有効になっているかのチェック。無効の場...
if (mBluetoothAdapter.isEnabled()) {
// Bluetoothが利用可
} else {
// Bluetoothが利用不可。確認ダイアログを表示
Intent enableBtIntent = new Intent(BluetoothAdapter.ACT...
activity.startActivityForResult(enableBtIntent, 1);
}
**スキャン [#bb5b1b12]
// 10秒後にスキャンを停止
private static final long SCAN_PERIOD = 10000;
...
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCall...
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
...
}
private LeDeviceListAdapter mLeDeviceListAdapter;
...
// デバイススキャン後のコールバック関数
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, i...
byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
String msg = "name =" + device.getName() + ", bondst...
+ device.getBondState() + ", address = "
+ device.getAddress() + ", type" + devic...
+ ", uuid = " + device.getUuid().toStrin...
Log.d("Scan", msg);}
});
}
};
**デバイスとの接続 [#qb926923]
onLeScan()で返されるBluetoothDevice#connectGatt()を呼び出...
BluetoothDevice:
BluetoothGatt connectGatt(Context context, boolean autoC...
***BluetoothGattCallbackの実装 [#l508541c]
private final BluetoothGattCallback callback = new Blue...
// 接続状態が変更されたときに実行される
@Override
public void onConnectionStateChange(BluetoothGatt ga...
// 接続された
if (newState == BluetoothProfile.STATE_CONNECTED...
Log.i(TAG, "Connected to GATT server.");
// サービスを検索する
Log.i(TAG, "Attempting to start service disc...
// 切断された
} else if (newState == BluetoothProfile.STATE_DI...
Log.i(TAG, "Disconnected from GATT server.");
}
}
@Override
// サービスの検索結果を返す
public void onServicesDiscovered(BluetoothGatt gatt,...
// 成功
if (status == BluetoothGatt.GATT_SUCCESS) {
// サービスのリストを取得
List<BluetoothGattService> gattService = get...
for(BluetoothGattService service : gattServi...
UUID uuid = service.getUuid();
// サービスからCharacteristicsのリストを...
List<BluetoothGattCharacteristic> charas...
// Characteristic に Notification の受信要求を設定
for(BluetoothGattCharacteristic charasti...
//
gatt.setCharacteristicNotification(c...
}
}
} else {
Log.w(TAG, "onServicesDiscovered received: "...
}
}
// 読み込み通知
@Override
public void onCharacteristicRead(BluetoothGatt gatt,...
if (status == BluetoothGatt.GATT_SUCCESS) {
// Characteristicの読込成功
}
}
// 書き込み通知
@Override
public void onCharacteristicWrite(BluetoothGatt gat...
if (status == BluetoothGatt.GATT_SUCCESS) {
// Characteristicの書込成功
}
...
};
**読込 [#p547ccfa]
読込は非同期の為、読込要求と読込通知とからなります。
BluetoothGatt#readCharacteristic()を実行します。~
結果はBluetoothGattCallback#onCharacteristicRead()に返り...
***読込要求 [#f7d05b19]
mBluetoothGatt.readCharacteristic(characteristic);
***読込通知 [#p01a6e6d]
public void onCharacteristicRead(BluetoothGatt gatt,Blue...
if (status == BluetoothGatt.GATT_SUCCESS) {
// Characteristicの読込成功
}
}
**書込 [#wf89816b]
書込は非同期の為、書込要求と書込通知とからなります。
BluetoothGatt#writeCharacteristic()を実行します。~
結果はBluetoothGattCallback#onCharacteristicWrite()に返り...
***書込要求 [#o6f832ac]
mBluetoothGatt.writeCharacteristic(characteristic);
***書込通知 [#c6b1e070]
public void onCharacteristicWrite(BluetoothGatt gatt,Blu...
if (status == BluetoothGatt.GATT_SUCCESS) {
// Characteristicの書込成功
}
}
**接続の解除 [#wb4063ed]
BluetoothGatt#close()を実行します。
*複数のデバイスを接続する [#l0c9daee]
複数のデバイスを接続する場合、同時に接続処理を行うのでは...
接続結果を待たずに別のデバイスの接続を行うと動作が不安定...
また、スキャン実行中の接続も動作が不安定になります。~
**[ケース] [#e1877340]
デバイスA,B,Cを接続するケース
**[処理手順] [#y13c25bb]
1.デバイスAの接続~
BluetoothDevice#connectGatt()~
2.接続結果を待つ~
BluetoothGattCallback#onConnectionStateChange()~
3.デバイスBの接続~
BluetoothDevice#connectGatt()~
4.デバイスBの接続結果を待つ~
BluetoothGattCallback#onConnectionStateChange()~
5.デバイスCの接続~
BluetoothDevice#connectGatt()~
6.デバイスCの接続結果を待つ~
BluetoothGattCallback#onConnectionStateChange()~
*Embedded Technology 2015のデモについて [#nc9416ae]
Android端末(Nexus7 2013)で6台のBLEデバイスに接続し、操作...
**同時接続 [#nd6c7738]
BLEデバイスを複数接続~
6台のデバイスを順次接続しています~
#youtube(FCtvmpk3XSE);
**LED照明 [#j228c80f]
明るさを6段階で変更~
個別/同時に操作
#youtube(e8rH0BX88Nc);
**スイッチ [#c8b5b54b]
3個のリレーを搭載し、個別/同時にON/OFF操作
#youtube(115ipQ69tik);
**LED電球 [#jc7c0129]
LED電球(RGB)の色を操作
#youtube(u_36gwa3jsE);
----
RIGHT:YASHIRO Masayuki
ページ名:
BC::labsへの質問は、bc9-dev @ googlegroups.com までお願い致します。