仮想と物理とエトセトラ

xRや物理とかごった煮の備忘録的技術ブログ

HoloLensからBluetooth接続可能な端末を確認する

Bluetooth接続をHoloLens上で取り扱う方法のうち、接続可能な端末の情報を確認する方法をメモします。
将来的には、Bluetooth接続で別端末と情報をやり取りする方法の習得を目指します。
APIは下記のAPIを主に使用します。

docs.microsoft.com

1. 準備

接続するためのスクリプトを作成します。

  • BlueToothScan.cs
クリックで展開
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using TMPro;


#if WINDOWS_UWP 
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.Advertisement;
#endif

public class BlueToothScan : MonoBehaviour
{
#if WINDOWS_UWP 
    /// <summary>
    /// Bluetooth監視用Advatisement
    /// </summary>
    BluetoothLEAdvertisementWatcher watcher= new BluetoothLEAdvertisementWatcher();
#endif
    /// <summary>
    /// 表示用TMProテキスト
    /// </summary>
    public TextMeshPro tmpText;

    /// <summary>
    /// 受信開始状態
    /// </summary>
    bool isStarted = false;

    /// <summary>
    /// 受信情報格納用文字列
    /// </summary>
    string text = "";

    // Start is called before the first frame update
    void Start()
    {
#if WINDOWS_UWP 
        // Bluetooth読み込み時のイベント登録
        watcher.Received += OnAdvertisementReceived;
        
#endif
    }

    // Update is called once per frame
    void Update()
    {
        tmpText.text = text;
    }

    /// <summary>
    /// 10秒BlueToothスキャン
    /// </summary>
    public void GetBluetoothInfo()
    {
 
        tmpText.text = "";
#if WINDOWS_UWP
        
        if(!isStarted)
        {
            isStarted = true;
            // 読み込み開始
            watcher.Start();
        }
        else
        {
            isStarted = false;
            // 読み込み停止
            watcher.Stop();
        }

        
#endif
    }

#if WINDOWS_UWP
    /// <summary>
    /// 受信イベント
    /// </summary>
    /// <param name="watcher"></param>
    /// <param name="eventArgs"></param>
    void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
    {
        text = "";
        if(eventArgs.Advertisement.LocalName != null)
        {
            text += "== Start ==\n";
            // デバイス名
            text += "Device name : [" + eventArgs.Advertisement.LocalName + "]\n";
            // アドバタイズのタイプ
            text += "AdvertisementType : " + eventArgs.AdvertisementType + "\n";
            // BlueToothアドレス
            text += "BluetoothAddress  : " + eventArgs.BluetoothAddress + "\n";
            // 接続可否
            text += "IsConnectable : " + eventArgs.IsConnectable + "\n";
            // タイムスタンプ
            text += "Timestamp : " + eventArgs.Timestamp + "\n";
            // 接続強度
            text += "RawSignalStrengthInDBm : " + eventArgs.RawSignalStrengthInDBm + "dBm \n";
            text += "== End ==\n";

        }
    }
#endif
}

watcher.Start()でスキャンを開始し、watcher.Stop()でスキャンを停止します。

Bluetoothアドレスは端末ごとに割り振られた値です。

macaddresschanger.com

UnityEditor上ではWindows.Devices.Bluetoothを使用できないため、プラットフォーム依存でのコンパイルを行います。

次に、スクリプトをボタンにアタッチし、OnClickに設定します。
また、情報の出力先としてシーン上のTextMeshProをBlueToothScanコンポーネントに追加します。
f:id:napo909:20210620203146p:plain

最後に、ProjectSetting→Player→PublishingSetting→CapabilitiesのBluetoothにチェックを入れます。
f:id:napo909:20210620203330p:plain

動作確認

ボタンを押して読み込みをスタートすると、読み込んだ内容が表示されていきます。
1件読み込むごとにOnAdvertisementReceivedが呼ばれるので、近くに複数端末ある場合はどんどん切り替わっていきます。
f:id:napo909:20210620210651j:plainバイス名が表示されていない問題があるので、後日調査します。
※端末側で設定されていないだけの可能性もあり。