ysk-san KT

技術系の情報をKTするために、まずは勉強

Android上でUWB(Ultra-Wideband)を動かすための簡単なまとめ/A brief summary of running UWB (Ultra-Wideband) on Android

(English below)

Android上でUWB(Ultra-Wideband)を動かすための簡単なまとめです。UWBは位置情報を高精度で取得するための技術で、この記事ではAndroidバイスUWBを活用する方法を説明します。具体的なコードサンプルとデバイスの使用方法を提供しますが、注意点として、UWBバイスの種類やブランドによって異なることがあるため、特定のデバイスに関する詳細情報はデバイスの公式ドキュメンテーションを参照してください。

 

ステップ1: UWBバイスのセットアップ

まず、UWBバイスAndroidバイスに接続し、必要なドライバーやアプリをインストールします。デバイスに付属の取扱説明書に従ってセットアップを行います。この記事では、DecawaveのDWM1001C UWBバイスを使用することを想定しています。

ステップ2: Androidプロジェクトのセットアップ

Androidアプリを開発するためにAndroid Studioを使用します。以下の手順に従ってプロジェクトをセットアップします。

Android Studioを開き、新しいAndroidプロジェクトを作成します。
必要なパーミッションをAndroidManifest.xmlに追加します。UWBバイスにアクセスするために必要な権限を許可することを忘れないでください。

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>

UWBバイスと通信するためにBluetooth APIを使用するため、Bluetooth関連の依存関係をアプリのビルドファイルに追加します。

 

gradle
implementation 'com.github.ivbaranov:rxbluetooth:1.0.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.4'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'

ステップ3: UWBバイスとの通信

UWBバイスと通信するために、Bluetoothを使用します。以下は、UWBバイスに接続し、データを送受信する基本的なコードの一例です。

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import rxble.RxBleClient;
import rxble.RxBleDevice;

public class UwbCommunicationActivity extends AppCompatActivity {

    private RxBleClient rxBleClient;
    private RxBleDevice rxBleDevice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uwb_communication);

        // RxBleClientの初期化
        rxBleClient = RxBleClient.create(this);

        // UWBバイスMACアドレスを指定して接続
        String deviceMacAddress = "UWB_DEVICE_MAC_ADDRESS";
        rxBleDevice = rxBleClient.getBleDevice(deviceMacAddress);

        // 接続
        rxBleDevice.establishConnection(false)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        rxBleConnection -> {
                            // 接続成功時の処理
                            // データの送受信などをここで行う
                        },
                        throwable -> {
                            // 接続エラー時の処理
                        }
                );
    }
}

このコードは、UWBバイスに接続し、接続が確立されたときにデータの送受信を行う基本的な例です。

 

ステップ4: データの送受信

UWBバイスとのデータの送受信は、UWBテクノロジーを活用するアプリケーションの中核的な部分です。以下は、UWBバイスとのデータの送受信に関する基本的なコード例です。

データの受信

UWBバイスからデータを受信するためには、rxBleConnectionを使用します。データを受信するためには、データを送信する側と受信する側で共通のデータフォーマットやプロトコルを定義する必要があります。

rxBleConnection
    .setupNotification(characteristicUUID) // UWBバイスのキャラクタリスティックUUIDを指定
    .flatMap(notificationObservable -> notificationObservable)
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(
        data -> {
            // データを受信したときの処理
            String receivedData = new String(data); // バイトデータを文字列に変換
            // 受信したデータを処理する
        },
        throwable -> {
            // エラー処理
        }
    );

このコードは、指定されたUUIDのキャラクタリスティックからデータを非同期に受信し、受信したデータを文字列に変換して処理します。

データの送信

UWBバイスにデータを送信するためには、rxBleConnection.writeCharacteristic()メソッドを使用します。以下は、データの送信の例です。

String dataToSend = "Hello, UWB Device!";
byte dataBytes = dataToSend.getBytes(); // 文字列データをバイトデータに変換

rxBleConnection
    .writeCharacteristic(characteristicUUID, dataBytes) // UWBバイスのキャラクタリスティックUUIDを指定
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(
        characteristic -> {
            // データの送信が成功したときの処理
        },
        throwable -> {
            // 送信エラー処理
        }
    );

このコードは、指定されたUUIDのキャラクタリスティックにデータを非同期に送信します。送信が成功した場合、成功時の処理が実行されます。

データの送受信に関する詳細は、UWBバイスの仕様に従ってカスタマイズする必要があります。データのフォーマット、プロトコル、およびUUIDなどは、デバイスとの連携に関連しています。デバイスドキュメンテーションを参照して詳細を把握し、適切なデータの送受信処理を実装してください。

 

====English translation====

 

This is a brief summary of running UWB (Ultra-Wideband) on Android; UWB is a technology used to acquire location information with high accuracy, and this article describes how to take advantage of UWB on Android devices. Specific code samples and device usage are provided, but as a reminder, please refer to the official device documentation for more information on specific devices, as they may vary by type and brand of UWB device.

Step 1: Set up your UWB device

First, connect the UWB device to your Android device and install the necessary drivers and apps. Follow the instruction manual that came with the device to complete the setup. This article assumes you are using Decawave's DWM1001C UWB device.

Step 2: Set up your Android project

Use Android Studio to develop your Android application. Follow these steps to set up your project

Open Android Studio and create a new Android project.
Add the necessary permissions to AndroidManifest.xml. remember to grant the necessary permissions to access the UWB device.

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>

Add Bluetooth-related dependencies to the app build file in order to use the Bluetooth API to communicate with UWB devices.

gradle
implementation 'com.github.ivbaranov:rxbluetooth:1.0.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.4'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'

Step 3: Communicate with UWB devices

Use Bluetooth to communicate with the UWB device. Below is an example of a basic code to connect to a UWB device and send/receive data.

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import rxble.RxBleClient;
import rxble.RxBleDevice;

public class UwbCommunicationActivity extends AppCompatActivity {

    private RxBleClient rxBleClient;
    private RxBleDevice rxBleDevice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uwb_communication);

        // RxBleClientの初期化
        rxBleClient = RxBleClient.create(this);

        // UWBバイスMACアドレスを指定して接続
        String deviceMacAddress = "UWB_DEVICE_MAC_ADDRESS";
        rxBleDevice = rxBleClient.getBleDevice(deviceMacAddress);

        // 接続
        rxBleDevice.establishConnection(false)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        rxBleConnection -> {
                            // 接続成功時の処理
                            // データの送受信などをここで行う
                        },
                        throwable -> {
                            // 接続エラー時の処理
                        }
                );
    }
}

This code is a basic example of connecting to a UWB device and sending and receiving data when a connection is established.

Step 4: Send and Receive Data

Sending and receiving data to and from UWB devices is a core part of any application that takes advantage of UWB technology. The following is a basic code example for sending and receiving data to and from a UWB device.

Receiving Data

To receive data from a UWB device, use rxBleConnection. To receive data, it is necessary to define a common data format and protocol for the sender and receiver of the data.

rxBleConnection
    .setupNotification(characteristicUUID) // UWBバイスのキャラクタリスティックUUIDを指定
    .flatMap(notificationObservable -> notificationObservable)
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(
        data -> {
            // データを受信したときの処理
            String receivedData = new String(data); // バイトデータを文字列に変換
            // 受信したデータを処理する
        },
        throwable -> {
            // エラー処理
        }
    );

This code receives data asynchronously from the characteristic of the specified UUID, converts the received data into a string, and processes it.

Sending Data

To send data to a UWB device, use the rxBleConnection.writeCharacteristic() method. The following is an example of sending data.

String dataToSend = "Hello, UWB Device!";
byte dataBytes = dataToSend.getBytes(); // 文字列データをバイトデータに変換

rxBleConnection
    .writeCharacteristic(characteristicUUID, dataBytes) // UWBバイスのキャラクタリスティックUUIDを指定
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(
        characteristic -> {
            // データの送信が成功したときの処理
        },
        throwable -> {
            // 送信エラー処理
        }
    );

This code sends data asynchronously to the characteristic of the specified UUID. If the transmission is successful, the process is executed on success.

Details regarding the transmission and reception of data must be customized according to the specifications of the UWB device. Data formats, protocols, and UUIDs are relevant to working with the device. Refer to the device documentation to understand the details and implement the appropriate data send/receive process.