https://www.youtube.com/watch?v=vUbFB1Qypg8 より抜粋
本記事は以下の記事の続きになります。
[第一回] 生活で使えるBLEデバイス
[第二回] 生活で使えるBLEデバイス~ペアリング編~
[第三回] 生活で使えるBLE~アプリ間通信編~
はじめに
私事ですが、最近携帯をGalaxy S6に変更しました。(とても気に入っています)
しかしGalaxy S6は電池の容量が少ないらしく、日常的に使っていても少し電池の減りが早い気がします。
なんとか出来ないかと電池の消費などについて少し調べてみると、充電回数を減らすことや過充電を行わないことが電池の寿命を延ばすことに繋がるようです。
ということで、今回はBLEデバイスを使って少しでも携帯の電池寿命の延命を行いたいと思います。
1. 過充電を防いで電池の寿命を長く保ちたい。
2. 家の中で携帯をすぐ無くすので、居場所を特定したい。
Battery ServiceとImmediate Alertのサービスがあるので、やりたいことは実現出来そうです。
では、実際に作ってみましょう。
実際に作ってみる
BLEデバイスは鳴動可能で、物理ボタンがあるモノにしました。今後もいろいろ遊べそうです。
やりたいこと
1. 過充電を防いで電池の寿命を長く保ちたい。
2. 家の中で携帯をすぐ無くすので、居場所を特定したい。
イメージとしては以下のよう感じです。
1. 過充電防止
2. 携帯の位置通知
BLEデバイスのサービスの確認
まず、購入したBLEデバイスを実際に接続してサービスを確認しましょう。
以下のようなサービスがあるようです。
以下のようなサービスがあるようです。
サービス名
|
UUID
|
説明
|
Generic Access
|
1800
|
デバイス名などの情報取得
|
Immediate Alert
|
1802
|
アラームを鳴らす
|
Link Loss
|
1803
|
接続が切れたときの挙動を設定する
|
Tx Power
|
1804
|
BLEの送信のパワー
|
Battery Service
|
180f
|
バッテリーの状態
|
では、実際に作ってみましょう。
実際に作ってみる
1. 過充電防止
ただし、電源接続している時だけ通知が欲しいので、ACTION_POWER_CONNECTEDを契機に、バッテリーの状態を監視するサービスを常駐させるようにしたいと思います。
構成としては以下の通りです。
図3 過充電防止-構成
では次に、Android側の実装に移ります。
使用するUUID
サービスのImmediate Alertを使用します。
Alert Levelに値(0 or 1or 2)を設定することで通知を行えるようです。
public static final UUID ALERT_SERVICE_UUID = UUID.fromString("00001802-0000-1000-8000-00805f9b34fb");
public static final UUID ALERT_LEVEL_UUID = UUID.fromString("00002a06-0000-1000-8000-00805f9b34fb");
AndroidManifestに宣言
PowerConnectedReciverの実装
電源の接続/切断を受けるレシーバーで、電源接続を契機にバッテリーの監視を行い、電源の切断を契機にバッテリーの監視を終了します。
BattryMonitorServiceからの通知を受けて、BLEデバイスへアラームの鳴動要求を通知します。
過充電を防ぐことで、電池の寿命を長持ちさせましょう。
2. 携帯の位置通知
<service android:name=".power.BatteryMonitorService" android:enabled="true"/>
<service android:name=".ble.BluetoothLeService" android:enabled="true"/>
<receiver android:name=".power.PowerConnectedReceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>
電源の接続/切断を受けるレシーバーで、電源接続を契機にバッテリーの監視を行い、電源の切断を契機にバッテリーの監視を終了します。
public class PowerConnectedReceiver extends BroadcastReceiver {
private static final String TAG = "BleNotify.PowerConnectedReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive : " + intent.getAction());
if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
Intent serviceIntent = new Intent(context, BatteryMonitorService.class);
context.startService(serviceIntent);
} else if (intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) {
Intent serviceIntent = new Intent(context, BatteryMonitorService.class);
context.stopService(serviceIntent);
}
}
}
BatteryMonitorServiceの実装
起動時にバッテリーの状態変化を監視するサービスで、バッテリーが90%以上になるとブロードキャストを送信します。
BLEデバイスへ通知起動時にバッテリーの状態変化を監視するサービスで、バッテリーが90%以上になるとブロードキャストを送信します。
public class BatteryMonitorService extends Service {
private static final String TAG = "BleNotify.BatteryMonitorService";
private ChargingOnReceiver mChargingOnReceiver;
private boolean isRegisteredChargingReceiver = false;
class ChargingOnReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level / (float)scale;
Log.d(TAG, "change battery state : " + batteryPct*100 + "%");
if (batteryPct > 0.9) {
Intent notify = new Intent(BluetoothLeService.ACTION_CALL_BATTERY_NOTIFY);
LocalBroadcastManager.getInstance(context).sendBroadcast(notify);
Log.d(TAG, "send broadcast Notify");
}
}
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate");
mChargingOnReceiver = new ChargingOnReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(mChargingOnReceiver, filter);
isRegisteredChargingReceiver = true;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "starting the BatteryMonitorService.");
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
Log.d(TAG, "terminate the BatteryMonitorService.");
// close process
if (isRegisteredChargingReceiver) {
unregisterReceiver(mChargingOnReceiver);
}
super.onDestroy();
}
}
BattryMonitorServiceからの通知を受けて、BLEデバイスへアラームの鳴動要求を通知します。
public class BluetoothLeService extends Service {
private BluetoothGatt mBluetoothGatt;
public final static String ACTION_CALL_BATTERY_NOTIFY =
"com.brilliant.blenotify.ble.le.ACTION_CALL_BATTERY_NOTIFY";
〜中略〜
class GattRequestReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive : " + intent.getAction());
if (mBluetoothGatt == null) {
Log.e(TAG, "GattService is not connected...");
return;
}
if (ACTION_CALL_BATTERY_NOTIFY.equals(intent.getAction())) {
BluetoothGattCharacteristic c =
getCharacteristic(GattAttributes.ALERT_SERVICE_UUID,
GattAttributes.ALERT_LEVEL_UUID);
if (c != null) {
// 1 : vibrator
// 2 : sound
int level = 2;
c.setValue(new byte[]{(byte) level});
mBluetoothGatt.writeCharacteristic(c);
}
}
}
}
public BluetoothGattCharacteristic getCharacteristic(UUID sid, UUID cid) {
BluetoothGattService s = mBluetoothGatt.getService(sid);
if (s == null) {
Log.w(TAG, "Service NOT found :" + sid.toString());
return null;
}
BluetoothGattCharacteristic c = s.getCharacteristic(cid);
if (c == null) {
Log.w(TAG, "Characteristic NOT found :" + cid.toString());
return null;
}
return c;
}
このようにすることで、充電中で90パーセントを超えると音を鳴らして通知してくれます。過充電を防ぐことで、電池の寿命を長持ちさせましょう。
2. 携帯の位置通知
次携帯の位置通知は、BLEデバイス側に物理ボタンがあるので、押された場合に端末側にて鳴動等で位置を知らせます。
今回は端末がバイブレーションするところまで作ります。
構成としては以下の通り。
NotificationをONに設定
今回は端末がバイブレーションするところまで作ります。
構成としては以下の通り。
図4 携帯の位置通知-構成
NotificationをONに設定
まずはBLEデバイスからの通知を受け取る為に設定を行います。
サービス内のキャラクタリスティックスにNotification設定をONにすることで、BLEデバイスからの通知を受けることが出来ます。
サービス内のキャラクタリスティックスにNotification設定をONにすることで、BLEデバイスからの通知を受けることが出来ます。
調べてみると、Battery Service内にある「00002a1b」から始まるキャラクタリスティックスがどうやらNotificationのプロパティを持っているようです。
このキャラクタリスティックスを利用すれば、BLEデバイスからの通知を受けることが出来そうです。
※取得したキャラクタリスティックスはBluetoothGattCharacteristic#getProperties()で確認出来ます。
Blutooth SIGで定義されているBattery ServiceにはBattery Levelしかいないようなので、カスタムで追加されているようです。
このキャラクタリスティックスを利用すれば、BLEデバイスからの通知を受けることが出来そうです。
※取得したキャラクタリスティックスはBluetoothGattCharacteristic#getProperties()で確認出来ます。
Blutooth SIGで定義されているBattery ServiceにはBattery Levelしかいないようなので、カスタムで追加されているようです。
「Power State Lebel(00002a1b)」の参考:
https://groups.google.com/d/msg/android-group-japan/8PffzGRfMms/0gtEcPdjQY8J
public static final UUID BATTERY_SERVICE_UUID = UUID.fromString("0000180f-0000-1000-8000-00805f9b34fb");
public static final UUID BATTERY_LEVEL_STATE_UUID = UUID.fromString("00002a1b-0000-1000-8000-00805f9b34fb");
BluetoothGattCharacteristic c =
getCharacteristic(GattAttributes.BATTERY_SERVICE_UUID,
GattAttributes.BATTERY_LEVEL_STATE_UUID);
if (c != null) {
final int charaProp = c.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
Log.d(TAG, "has PROPERTY_NOTIFY");
}
mBluetoothGatt.setCharacteristicNotification(c, true);
}
通知はボタンを押された時だけに限定します。
通知されたデータの1byte目でボタンの状態がわかります。
public class BluetoothLeService extends Service {
public final static String ACTION_FINDME_NOTIFY =
"com.brilliant.blenotify.ble.le.ACTION_FINDME_NOTIFY";
〜中略〜
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
UUID uuid = characteristic.getUuid();
Log.e(TAG, "onCharacteristicChanged : " + characteristic.getUuid());
if (GattAttributes.BATTERY_LEVEL_STATE_UUID.equals(uuid)) {
// For all other profiles, writes the data formatted in HEX.
final byte[] data = characteristic.getValue();
Log.d(TAG, "BATTERY_LEVEL_STATE_UUID received : " + new String(data));
if (data != null && data.length > 0) {
// if button pressed.
if (data[0] == 1) {
Intent intent = new Intent(ACTION_FINDME_NOTIFY);
sendBroadcast(intent);
Log.d(TAG, "send broadcast ACTION_FINDME_NOTIFY.");
} else {
Log.d(TAG, "Button is not pressed.");
}
}
} else {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
}
FindMeRecieverの実装BLEデバイスから通知を受けたときの動作を実装します。
public class FindMeReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
long[] pattern = {3000, 1000, 3000, 1000};
vibrator.vibrate(pattern, -1);
}
}
AndrodManifestにも以下を追加しておきます。
<uses-permission android:name="android.permission.VIBRATE"/>
<receiver android:name=".findme.FindMeReceiver" android:enabled="true" android:exported="false"/>
<intent-filter/>
<action android:name="com.brilliant.blenotify.ble.le.ACTION_FINDME_NOTIFY" />
</intent-filter/>
</receiver/>
これで、BLEデバイスから通知があったら端末がブルブルと震えてくれるのでどこに置いたかわかるようになりました。
さいごに
以上で「生活で使えるBLEデバイス」シリーズは終了となります。
いかがでしたでしょうか。
BLEの接続方法から実際のデータ通信までの方法を紹介してきましたが、BLEを利用するにあたって多少の苦労がありました。
BLEデバイスの持っているプロファイルに関して、独自に追加されているサービスやキャラクタリスティックスは情報がなかったり、デバイス毎にどのサービスをどの機能に利用しているか等が不明だったりします。(今回で言うところのPower State Lebel)
BluetoothやBLEでの通信のとっつきにくさはこのあたりも関係しているような気がします。
しかし、コツさえ掴んでしまえばアイデア次第で自由なモノ作りが可能な為、様々な分野で活躍できる技術であると思います。
今回までの記事が、そのコツを掴むまでのお手伝いになれば幸いです。
記載されている会社名、および商品名等は、各社の商標または登録商標です。






0 コメント:
コメントを投稿