Site icon image CB Interaction Viewer

macOS向けアプリ「CB Interaction Viewer」公式ブログです。使い方や更新情報を発信しています。CB Interaction Viewerは、モバイルシミュレータ( Xcodeシミュレータ )で動作するモバイルアプリでBLE(Bluetooth Low Energy)デバイスとの実通信を実現するアプリです。 アプリ入手 → https://apps.apple.com/jp/app/cb-interaction-viewer/id6757977616?mt=12 X(Twitter) → https://x.com/CBIViewer

AndroidBLEForCBIV導入手順

目次


はじめに


ここでは、以下の内容を説明しています。
  • Android BLE APIと互換性のあるインターフェースを提供
  • Androidエミュレータ上でBLE APIの実行が可能
  • 既存のAndroid BLE実装を最小限の修正で利用可能
  • macOS CoreBluetoothを利用した実通信に対応

AndroidBLEForCBIVの目的

AndroidアプリにおけるBLE(Bluetooth Low Energy)通信の開発では、通常Android実機が必要となります。本ライブラリは、macOS上で動作するAndroidエミュレータ環境においてBLE APIを実行可能にすることを目的としています。

AndroidBLEForCBIVの概要

AndroidBLEForCBIVは、Android標準のBLE APIと互換性のあるインターフェースを提供しながら、実機とエミュレータの両方のアプリビルドで利用可能なラッパーライブラリです。そのため導入の際はAndroid BLE APIのimportを変更し、BluetoothManagerの取得コードを書きかるだけの数ステップで実現できます。

graph TD
	subgraph lib[AndroidBLEForCBIV]
		if[Android BLE I/F]
		mock[Android BLE Mock]
		real[Android BLE Real]
	end
	
	if <--> mock
	if <--> real
	
classDef appLayer fill:#E3F2FD,stroke:#1E88E5,stroke-width:2px,color:#0D47A1;
classDef libLayer fill:#E8F5E9,stroke:#43A047,stroke-width:2px,color:#1B5E20;
classDef macLayer fill:#FFF3E0,stroke:#FB8C00,stroke-width:2px,color:#E65100;
classDef deviceLayer fill:#FCE4EC,stroke:#D81B60,stroke-width:2px,color:#880E4F;

class if,real,mock appLayer;
class lib libLayer;
class viewer macLayer;
class device deviceLayer;
AndroidBLEForCBIVは、内部に実機モジュールとエミュレータ用Mockモジュールを内包します。

BLE通信の仕組み

AndroidBLEForCBIVは、macOSアプリケーションであるCB Interaction Viewerを介してCoreBluetoothを利用し、Androidエミュレータ上から実際のBLEデバイスとの通信を可能にします。

graph TD
	logic["App Logic"]
	api["Android BLE I/F"]
	viewer["CB Interaction Viewer"]
	device["Real BLE Device"]
	
	subgraph lib["AndroidBLEForCBIV"]
	    api
	    mock["Android BLE Mock"]
	end
	
	subgraph APP["Your Android Application"]
	    logic
	    lib
	end
	
	subgraph SIM["Android Device Simulator"]
	    APP
	end
	
	subgraph MAC["macOS"]
	    SIM
	    viewer
	end
	
  logic <--> api
  api <--> mock
	mock <-->|Socket Communication| viewer
	viewer <-->|BLE| device
	
classDef appLayer fill:#E3F2FD,stroke:#1E88E5,stroke-width:2px,color:#0D47A1;
classDef libLayer fill:#E8F5E9,stroke:#43A047,stroke-width:2px,color:#1B5E20;
classDef macLayer fill:#FFF3E0,stroke:#FB8C00,stroke-width:2px,color:#E65100;
classDef deviceLayer fill:#FCE4EC,stroke:#D81B60,stroke-width:2px,color:#880E4F;

class logic,api,mock appLayer;
class lib libLayer;
class viewer macLayer;
class device deviceLayer;
Android デバイスエミュレータ実行時
graph TD
	logic["App Logic"]
	api["Android BLE I/F"]
	device["Real BLE Device"]
	
	subgraph lib["AndroidBLEForCBIV"]
	    api
	    real["Android BLE Real"]
	end
	
	subgraph APP["Your Android Application"]
	    logic
	    lib
	end
	
	subgraph phone["Android Device"]
	    APP
	end
	
  logic <--> api
  api <--> real
	real <-->|BLE| device
	
classDef appLayer fill:#E3F2FD,stroke:#1E88E5,stroke-width:2px,color:#0D47A1;
classDef libLayer fill:#E8F5E9,stroke:#43A047,stroke-width:2px,color:#1B5E20;
classDef macLayer fill:#FFF3E0,stroke:#FB8C00,stroke-width:2px,color:#E65100;
classDef deviceLayer fill:#FCE4EC,stroke:#D81B60,stroke-width:2px,color:#880E4F;

class logic,api,real appLayer;
class lib libLayer;
class viewer macLayer;
class device deviceLayer;
Android デバイス実行時

AndroidBLEForCBIVの導入


対応環境

AndroidBLEForCBIVは、以下の環境での動作確認を行なっています。

  • Android Studio Narwhal 4 Feature Drop | 2025.1.4
  • Android 8.1 以降
  • Kotlin / Java
  • CB Interaction Viewer 1.1.2 以降

GitHubリポジトリ

aarを直接利用する場合は、こちらからダウンロードできます。

AndroidBLEForCBIVのプロジェクトは、こちらからクローンできます。プロジェクトをビルドしてaarを作成する場合は、以下のコマンドで作成できます。

./gradlew :AndroidBLEForCBIV:assembleRelease

AARで利用する場合

前項で取得したaarを直接利用する場合、ご自身のアプリプロジェクト内のsrc/libsにaarを格納してください。その後、アプリモジュールのdependenciesに依存関係を記述します。

モジュール利用の場合

  1. クローンしたプロジェクトから、AndroidBLEForCBIVディレクトリを自分のプロジェクトにコピー
  2. settings.gradle にモジュールを追加
include(":AndroidBLEForCBIV")
  1. アプリモジュールの dependencies に依存関係を追加

AndroidBLEForCBIVの利用方法


Step① Android BLE APIのimport置き換え

従来のAndroid BLE APIのうち、以下のimportは全て置き換えます。

// Before Import
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothGatt
import android.bluetooth.BluetoothGattCallback
import android.bluetooth.BluetoothGattCharacteristic
import android.bluetooth.BluetoothGattDescriptor
import android.bluetooth.BluetoothGattServer
import android.bluetooth.le.BluetoothLeScanner
import android.bluetooth.le.ScanCallback
import android.bluetooth.le.ScanRecord
import android.bluetooth.le.ScanResult
// After Import
import com.tochy0115.android.cbinteractionviewer.bluetooth.api.*

Step② BluetoothProviderの初期化

実行環境に応じて初期化を行います。

// エミュレータ実行時
BluetoothProvider.initialize(context, true, null)

// 実機実行時
BluetoothProvider.initialize(context, false, null)

Step③ BluetoothManagerの取得

従来のAndroid BLEでは、BluetoothManagerの生成はgetSystemServiceで行いますが、AndroidBLEForCBIVでは、実行モードに応じて内部でBluetoothManagerを生成します。生成されるBluetoothManagerは従来と同様にシングルトンです。BluetoothManagerが取得できれば、以降のBLE通信はこれまでと同様のコードで実行することができます。

val bluetoothManager = BluetoothProvider.getManager()

BDアドレスの取り扱いについて

macOSのCoreBluetoothでは、BLEデバイスのBDアドレスを取得することができません。そのため、Androidエミュレータ環境にいては、Android BLE APIで一般的に利用されるBDアドレス指定によるスキャンアドレス指定によるBluetoothDevice生成はBluetoothProviderの初期化時に設定が必要です。以下のコードのように、BDアドレスとローカルネームの対応テーブルを事前に設定しておく必要があります。

val tbl = mutableListOf(
    BluetoothProvider.ConvTblBDAddress2LocalName(
        "01:02:03:04:05:06",
        "Dummy_LocalName1"
    ),
    BluetoothProvider.ConvTblBDAddress2LocalName(
        "11:12:13:14:15:16",
        "Dummy_LocalName2"
    )
)

BluetoothProvider.initialize(context, true, tbl)

さいごに


AndroidBLEForCBIVは、Androidエミュレータ環境においてBLE通信を実行可能にするための基盤ライブラリです。Android標準のBLE APIと互換性のあるインターフェースを提供することで、既存のアプリケーションコードを大きく変更することなく、エミュレータおよび実機の両環境に対応できます。また、macOS上ではCB Interaction Viewerを介してCoreBluetoothを利用する構成により、実際のBLEデバイスとの通信を可能としています。これにより、物理的なAndroid端末に依存しない開発・検証環境を構築できます。

本ライブラリは、以下のような場面で有効です。

  • BLE通信を含むアプリケーションの設計・実装初期段階での動作検証
  • 実機が利用できない環境での機能確認
  • macOSを中心とした開発環境における効率的なBLEデバッグ

BLE通信の開発は、環境依存やデバイス依存の影響を受けやすい分野です。AndroidBLEForCBIVは、その制約を緩和し、より柔軟な開発体制を実現することを目的としています。

本プロジェクトが、BLEアプリケーション開発の効率向上および品質向上の一助となれば幸いです。