ロボホンにBLE血圧計繋いでみた。
ロボホンはロボット型のスマートフォンで、Android OS(バージョン 5.0)で動いています。
ロボホン(RoBoHoN)についてはこちらのページをご覧ください。
(1) Bluetoothの設定がONであることを確認する
まずBluetoothの機能を使うために、BluetoothAdapterインスタンスを取得します。
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); bluetoothAdapter = bluetoothManager.getAdapter();
BluetoothがOFFの場合は以下のように暗黙的Intentを投げてBluetoothの有効化を求めます。
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, REQUEST_ENABLE_BLUETOOTH);
REQUEST_ENABLE_BLUETOOTHはActivityに設定した定数なので適宜実装してください。
以下のようにリクエストコードが一致していることを確認後、ONかOFFかで処理を分けます。
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_ENABLE_BLUETOOTH) {
if (resultCode == RESULT_OK) {
// BluetoothがONだった時の処理
} else {
// BluetoothがOFFだった時の処理
}
}
super.onActivityResult(requestCode, resultCode, data);
}
(2)ロボホンと接続済みのBluetooth機器を確認する
(3)血圧計を探す
@Override public void onScanResult(int callbackType, final ScanResult result) {
super.onScanResult(callbackType, result);
if (result != null && result.getDevice() != null && deviceAddress == null) {
BluetoothDevice device = result.getDevice();
// deviceが血圧計であれば、そのMACアドレスを取得する
}
}
};
(4)Serviceを起動する
血圧計に接続したり接続状況を確認したりするために使うServiceを起動します。
Intent intent = new Intent(MainActivity.this, BluetoothLeService.class);
private final ServiceConnection serviceConnection = new ServiceConnection() {
@Override public void onServiceConnected(ComponentName componentName, IBinder service) {
bluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
// (5)で説明します
}
@Override public void onServiceDisconnected(ComponentName componentName) {
bluetoothLeService = null;
}
};
(5)血圧計との接続
// Bluetooth機器が接続or切断された
@Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
// (6)で説明します
}
// Bluetooth機器のサービスが見つかった
@Override public void onServicesDiscovered(BluetoothGatt gatt, int status) {
// (7)で説明します
}
// Bluetooth機器にCharacteristicの書き込みがあった
@Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
// (8)で説明します
}
// Bluetooth機器からCharacteristicの変更が通知された
@Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
// (9)で説明します
}
};
(6)血圧計が持つサービスを探す
mBluetoothGatt.discoverServices();
}
(7)血圧計に時刻を設定する
BluetoothGattService service = gatt.getService(UUID.fromString(“00001810-0000-1000-8000-00805f9b34fb”));
if (service != null) {
BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(“00002a08-0000-1000-8000-00805f9b34fb”));
if (characteristic != null) {
// Bluetooth機器に時刻を設定する
}
}
}
(8)血圧計でIndicationを行う
BluetoothGattService service = gatt.getService(UUID.fromString(“00001810-0000-1000-8000-00805f9b34fb”));
if (service != null) {
BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(“00002a35-0000-1000-8000-00805f9b34fb”));
if (characteristic != null) {
gatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(“00002902-0000-1000-8000-00805f9b34fb”));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
}
}
(9)血圧計の測定データを取得する
血圧計の測定が完了するとデータが送られてくるので、そのデータが血圧測定のCharacteristicだった場合に、血圧計の測定データを処理します。
// BluetoothGattCharacteristicから測定した情報を取得
}
今回はデータを受け取ってからの処理については扱っていませんが、ロボホンに測定結果を喋らせるとよりアプリの完成度が高まりますね!