[第一回] 生活で使えるBLEデバイス
[第二回] 生活で使えるBLEデバイス~ペアリング編~
はじめに
前回は端末側でのBLE検知と接続までの方法をまとめました。
そして今回はセントラル(端末)とペリフェラル(BLEデバイス)での通信方法に関して触れていきたいと思います。
環境
前回記事と同様に以下の環境で行います。
- Nexus 5(端末)
- Nexus 9(BLEデバイス)
独自サービス
今回のデータ送受信では、セントラル(GATT Client)側からペリフェラル(GATT Server)へ接続し、サービス内の必要なキャラクタリスティクスにアクセスして、データ(Value)の読み書きを行います。
なので、今回のデータ送受信に使用するサービスとキャラクタリスティクスを定義しておきます。
例として、以下のように定義しておきます。
・サービス(図1 のService部分)
SERVICE_UUID = "00000001-0000-1000-8000-2f97f3b2dcd5";
・キャラクタリスティクス(図2 のCharacterri部分)
CHAR_READ_UUID = "00000010-0000-1000-8000-2f97f3b2dcd5";
→データ読み込み用
CHAR_WRITE_UUID = "00000011-0000-1000-8000-2f97f3b2dcd5";
→データ書き込み用
図1 GATT Server & GATT Client
アドバタイジングについて
前回からNexus 9をペリフェラルとして機能させていますが、どのようになっていたのでしょうか。
GATT通信の方法と併せて、Android端末でのアドバタイジング方法を簡単に説明していきます。(Bluetooth機能の確認と位置情報の権限取得は前回記事を参照)
BluetoothLeAdvertiserは端末のアドバタイジング開始/停止の操作等を行えるクラスです。
GATT通信の方法と併せて、Android端末でのアドバタイジング方法を簡単に説明していきます。(Bluetooth機能の確認と位置情報の権限取得は前回記事を参照)
BluetoothLeAdvertiserは端末のアドバタイジング開始/停止の操作等を行えるクラスです。
BluetoothManager mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
if (mBluetoothManager != null) {
BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter();
}
次にGATT Serverのインスタンスを取得します。(BluetoothManager#openGattServer)取得する際に引数として、BluetoothGattServerCallbackを渡します。
このコールバックにて読み書き等、セントラルから要求が行われた際の動作を実装していくようになります。
ペリフェラル機能を実装していく上で本体となる部分です。
mGattServer = mBluetoothManager.openGattServer(this, new BLEServer());
class BLEServer extends BluetoothGattServerCallback {
//セントラルから読み込み要求が来ると呼ばれる
public void onCharacteristicReadRequest(android.bluetooth.BluetoothDevice device, int requestId,
int offset, BluetoothGattCharacteristic characteristic) {
}
//セントラルから書き込み要求が来ると呼ばれる
public void onCharacteristicWriteRequest(android.bluetooth.BluetoothDevice device, int requestId,
BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded,
int offset, byte[] value) {
}
}
次は宣言しておいたサービスとキャラクタリスティクスをGATT Serverに設定していきます。
この時、GATT Serverのcloseも忘れずに。
アドバタイジングが開始された後、セントラル(Nexus 5)側から検知出来るようになっていることでしょう。
そして、定義したサービスとGATT Serverによって、データの読み書きを行う準備も出来ました。
では、実際にデータの読み書きを行ってみましょう。
ここでGATT Serverに設定することで、セントラルから独自宣言したサービスを検知することが可能になります。
この時、読み込み用のキャラクタリスティクスには読み込みの、書き込み用のキャラクタリスティクスには書き込みのプロパティと権限を付与しています。
キャラクタリスティクスの権限とプロパティを正しく設定出来ていないと、読み込み/書き込みに失敗してしまうので注意してください。
この時、読み込み用のキャラクタリスティクスには読み込みの、書き込み用のキャラクタリスティクスには書き込みのプロパティと権限を付与しています。
キャラクタリスティクスの権限とプロパティを正しく設定出来ていないと、読み込み/書き込みに失敗してしまうので注意してください。
private void setServices() {
//serviceUUIDを設定BluetoothGattService service = new BluetoothGattService(
UUID.fromString(Constants.SERVICE_UUID),
BluetoothGattService.SERVICE_TYPE_PRIMARY);
//characteristicUUIDを設定
BluetoothGattCharacteristic charRead = new BluetoothGattCharacteristic(
UUID.fromString(Constants.CHAR_READ_UUID),
BluetoothGattCharacteristic.PROPERTY_READ,
BluetoothGattCharacteristic.PERMISSION_READ);
BluetoothGattCharacteristic charWrite = new BluetoothGattCharacteristic(
UUID.fromString(Constants.CHAR_WRITE_UUID),
BluetoothGattCharacteristic.PROPERTY_WRITE,
BluetoothGattCharacteristic.PERMISSION_WRITE);
//characteristicUUIDをserviceUUIDにのせる
service.addCharacteristic(charRead);
service.addCharacteristic(charWrite);
//serviceUUIDをサーバーにのせる
mGattServer.addService(service);
}
次にアドバタイジング時の設定(AdvertiseSettings)とデータ(AdvertiseData)の設定を行います。
そして、アドバタイジングの設定の準備が出来たらBluetoothLeAdvertiser#startAdvertisingで、アドバタイジングを開始します。
アドバタイジングを終了する時はBluetoothLeAdvertiser#stopAdvertisingを呼びます。//AdvertiseSettingsの設定
private AdvertiseSettings buildAdvertiseSettings() {
AdvertiseSettings.Builder settingsBuilder = new AdvertiseSettings.Builder();
settingsBuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_POWER);
settingsBuilder.setTimeout(0);
return settingsBuilder.build();
}
//AdvertiseDataの設定
private AdvertiseData buildAdvertiseData() {
AdvertiseData.Builder dataBuilder = new AdvertiseData.Builder();
dataBuilder.addServiceUuid(ParcelUuid.fromString(Constants.SERVICE_UUID));
dataBuilder.setIncludeDeviceName(true);
return dataBuilder.build();
}
//Advertiseの開始
private void startAdvertising() {
setServices();
AdvertiseSettings settings = buildAdvertiseSettings();
AdvertiseData data = buildAdvertiseData();
mAdvertiseCallback = new SimpleAdvertiseCallback();
mBluetoothLeAdvertiser.startAdvertising(settings, data,mAdvertiseCallback);
}
//Advertiseの成功可否
private class SimpleAdvertiseCallback extends AdvertiseCallback {
@Override
public void onStartFailure(int errorCode) {
super.onStartFailure(errorCode);
Log.d(TAG, "Advertising failed");
}
@Override
public void onStartSuccess(AdvertiseSettings settingsInEffect) {
super.onStartSuccess(settingsInEffect);
Log.d(TAG, "Advertising successfully started");
}
}
この時、GATT Serverのcloseも忘れずに。
private void stopAdvertising() {
Log.d(TAG, "Service: Stopping Advertising");
if (mGattServer != null) {
mGattServer.clearServices();
mGattServer.close();
mGattServer = null;
}
if (mBluetoothLeAdvertiser != null) {
mBluetoothLeAdvertiser.stopAdvertising(mAdvertiseCallback);
mAdvertiseCallback = null;
}
}
以上で、Nexus 9がペリフェラルとして機能するようになります。アドバタイジングが開始された後、セントラル(Nexus 5)側から検知出来るようになっていることでしょう。
そして、定義したサービスとGATT Serverによって、データの読み書きを行う準備も出来ました。
では、実際にデータの読み書きを行ってみましょう。
データの読み込み
セントラル側からペリフェラルのキャラクタリスティクスデータを読み込む手順です。
図3 データの読み込み
接続済みのBluetoothGattから読み込み用キャラクタリスティクスを取得して、読み込み要求(BluetoothGattl#readCharacteristic)を行います。
public void readCharacteristic() {
BluetoothGattCharacteristic read = mBluetoothLeService.getCharacteristic(
GattAttributes.SERVICE_UUID,
GattAttributes.CHAR_READ_UUID);
mBluetoothGatt.readCharacteristic(read);
}
public BluetoothGattCharacteristic getCharacteristic(String sid, String cid) {
BluetoothGattService s = mBluetoothGatt.getService(UUID.fromString(sid));
if (s == null) {
Log.w(TAG, "Service NoT found :" + sid);
return null;
}
BluetoothGattCharacteristic c = s.getCharacteristic(UUID.fromString(cid));
if (c == null) {
Log.w(TAG, "Characteristic NOT found :" + cid);
return null;
}
return c;
}
読み込み要求を行い、成功するとBluetoothGattCallback#onCharacteristicReadが呼び出されます。
その引数に渡されるBluetoothGattCharacteristicにペリフェラルからのレスポンスを受け取ることが出来ます。
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
final byte[] data = characteristic.getValue();
Log.d(TAG, "onCharacteristicRead : " + new String(data));
}
<ペリフェラル(Nexus 9)側>
セントラル側から読み込み要求が行われると、BluetoothGattServerCallback#onCharacteristicReadRequestに通知が来ます。
セントラルに対して送りたいデータをGattServer#sendResponseにセットすることで、セントラルに任意のデータを送信することが出来ます。
//セントラルからReadRequestが来ると呼ばれる
public void onCharacteristicReadRequest(android.bluetooth.BluetoothDevice device, int requestId,
int offset, BluetoothGattCharacteristic characteristic) {
//セントラルに任意の文字を返信する
if (UUID.fromString(Constants. CHAR_READ_UUID).equals(characteristic.getUuid())) {
String response = "your message.";
byte value[] = response.getBytes();
mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
}
}
接続が完了したBluetoothGattから書き込み用キャラクタリスティクスを取得し、書き込みたいデータをセットすることでペリフェラル側へデータを送ることが出来ます。
※書き込み権限のないキャラクタリスティクスに書き込み要求(BluetoothGatt#writeCharacteristic)を行うと戻り値に false が返り、書き込みに失敗します。
public void writeCharacteristic() {
BluetoothGattCharacteristic write = getCharacteristic(
UUID.fromString(GattAttributes.SERVICE_UUID),
UUID.fromString(GattAttributes.CHAR_WRITE_UUID));
String message = "your message";
write.setValue(message);
mBluetoothGatt.writeCharacteristic(characteristic);
}
public BluetoothGattCharacteristic getCharacteristic(String sid, String cid) {
BluetoothGattService s = mBluetoothGatt.getService(UUID.fromString(sid));
if (s == null) {
Log.w(TAG, "Service NoT found :" + sid);
return null;
}
BluetoothGattCharacteristic c = s.getCharacteristic(UUID.fromString(cid));
if (c == null) {
Log.w(TAG, "Characteristic NOT found :" + cid);
return null;
}
return c;
}
<ペリフェラル(Nexus 9)側>
セントラル側から書き込み要求がされると、BluetoothGattServerCallback#onCharacteristicWriteRequestに通知が来ます。 セントラルから書きこまれた内容はcharacteristicから取得することが出来ます。
また、処理が終わる時には必ずGattServer#sendResponseを呼んでください。
さいごに
以上がAndroid間でのBLE通信(読み書き)の手順です。
今回までで、BLE接続、通信を行えるようになりました。
なので次回からはタイトル通り、生活の中でBLEデバイスを使ったアイデアを考えてみたいと思います。
セントラル側から書き込み要求がされると、BluetoothGattServerCallback#onCharacteristicWriteRequestに通知が来ます。 セントラルから書きこまれた内容はcharacteristicから取得することが出来ます。
また、処理が終わる時には必ずGattServer#sendResponseを呼んでください。
//セントラルから書き込み要求が来ると呼ばれる
public void onCharacteristicWriteRequest(android.bluetooth.BluetoothDevice device, int requestId,
BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded,
int offset, byte[] value) {
Log.d(TAG, "onCharacteristicWriteRequest");
if (UUID.fromString(Constants.CHAR_WRITE_UUID).equals(characteristic.getUuid())) {
final byte[] data = characteristic.getValue();
Log.d(TAG, "onCharacteristicRead : " + new String(data));
mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, null);
}
}
以上がAndroid間でのBLE通信(読み書き)の手順です。
今回までで、BLE接続、通信を行えるようになりました。
なので次回からはタイトル通り、生活の中でBLEデバイスを使ったアイデアを考えてみたいと思います。
記載されている会社名、および商品名等は、各社の商標または登録商標です。






第一回から拝見させていただきました。
返信削除Androidの公式サンプルコードに同じように書き込んだつもりですが、第二回までの実装はできましたが、今回の部分のコードはどうやらうまくいっておりません。
自ら送るデータを読み取れていないようです。
理由としては、サービスやキャラクタリスティックのUUIDが使えないものだからでしょうか。
もしよければ教えていただきたいです。宜しくお願い致します。