本日は HoloLens の技術調査枠です。
HoloLens の3Dアプリで BlueTooth のアドバタイズを受信する方法を記事にします。
以下の記事の続きです。
bluebirdofoz.hatenablog.com
BluetoothAdvertisementを参考に作成したサンプルコード
BluetoothAdvertisement のウォッチャークラスのコードを参考に、テキストオブジェクトに結果を出力するクラスを作成しました。
・BluetoothAdvertiseWatcher.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if WINDOWS_UWP
using System;
using Windows.Storage.Streams;
using Windows.Devices.Bluetooth.Advertisement;
#endif
public class BluetoothAdvertiseWatcher : MonoBehaviour {
<summary>
</summary>
public UnityEngine.UI.Text MessageText;
string showmessage;
<summary>
</summary>
void Start ()
{
App_Message("Start");
#if WINDOWS_UWP
Awake_BluetoothLEAdvertisementWatcher();
OnEnable_BluetoothLEAdvertisementWatcher();
Run_BluetoothLEAdvertisementWatcher();
#endif
}
<summary>
</summary>
private void Update()
{
if (showmessage.Length != 0)
{
MessageText.text = showmessage;
showmessage = "";
}
}
<summary>
</summary>
<param name="message"></param>
void App_Message(string message)
{
showmessage = message;
}
<summary>
</summary>
void OnApplicationQuit()
{
#if WINDOWS_UWP
OnDisable_BluetoothLEAdvertisementWatcher();
#endif
}
#if WINDOWS_UWP
<summary>
</summary>
private BluetoothLEAdvertisementWatcher watcher;
<summary>
</summary>
public void Awake_BluetoothLEAdvertisementWatcher()
{
watcher = new BluetoothLEAdvertisementWatcher();
var manufacturerData = new BluetoothLEManufacturerData();
manufacturerData.CompanyId = 0xFFFE;
var writer = new DataWriter();
writer.WriteUInt16(0x1234);
manufacturerData.Data = writer.DetachBuffer();
watcher.AdvertisementFilter.Advertisement.ManufacturerData.Add(manufacturerData);
watcher.SignalStrengthFilter.InRangeThresholdInDBm = -70;
watcher.SignalStrengthFilter.OutOfRangeThresholdInDBm = -75;
watcher.SignalStrengthFilter.OutOfRangeTimeout = TimeSpan.FromMilliseconds(2000);
}
<summary>
</summary>
public void OnEnable_BluetoothLEAdvertisementWatcher()
{
watcher.Received += OnReceived_AdvertisementWatcher;
watcher.Stopped += OnStopped_AdvertisementWatcher;
}
<summary>
</summary>
public void OnDisable_BluetoothLEAdvertisementWatcher()
{
watcher.Stop();
watcher.Received -= OnReceived_AdvertisementWatcher;
watcher.Stopped -= OnStopped_AdvertisementWatcher;
}
<summary>
</summary>
public void Run_BluetoothLEAdvertisementWatcher()
{
watcher.Start();
}
<summary>
</summary>
public void Stop_BluetoothLEAdvertisementWatcher()
{
watcher.Stop();
}
<summary>
</summary>
<param name="watcher"></param>
<param name="eventArgs"></param>
private async void OnReceived_AdvertisementWatcher(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
{
App_Message("Reseived");
DateTimeOffset timestamp = eventArgs.Timestamp;
BluetoothLEAdvertisementType advertisementType = eventArgs.AdvertisementType;
Int16 rssi = eventArgs.RawSignalStrengthInDBm;
ulong BlueToothAddress = eventArgs.BluetoothAddress;
string localName = eventArgs.Advertisement.LocalName;
string manufacturerDataString = "";
var manufacturerSections = eventArgs.Advertisement.ManufacturerData;
if (manufacturerSections.Count > 0)
{
BluetoothLEManufacturerData manufacturerData = manufacturerSections[0];
byte[] data = new byte[manufacturerData.Data.Length];
using (DataReader reader = DataReader.FromBuffer(manufacturerData.Data))
{
reader.ReadBytes(data);
}
manufacturerDataString = string.Format("0x{0}: {1}",
manufacturerData.CompanyId.ToString("X"),
BitConverter.ToString(data));
}
App_Message(manufacturerDataString);
}
<summary>
</summary>
<param name="watcher"></param>
<param name="eventArgs"></param>
private async void OnStopped_AdvertisementWatcher(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementWatcherStoppedEventArgs eventArgs)
{
App_Message("Stop Event");
}
#endif
}
Empty オブジェクトを作成し、本スクリプトをアタッチしました。
スクリプトの MessageText にテキストオブジェクトの参照を追加します。
Capabilityの追加
UWPアプリで Bluetooth を利用する場合、Capability の設定が必要です。
メニューから Edit -> Project Settings - Player を開きます。
Inspector ビューから Publishing Setttings を開きます。
[Capabilities]欄の[Bluetooth]にチェックを入れます。
後は HoloLens 向けにプロジェクトをビルドしてインストールします。
UnityプロジェクトのビルドとHoloLensへのインストール手順については以下を参照してください。
bluebirdofoz.hatenablog.com
送信側アプリの実行
PC側で前回ビルドした BluetoothAdvertisement アプリを起動します。
[Foreground publisher]のタブを開き、[Run]を実行して BlueTooth のアドバタイズを実行しておきます。
HoloLensでの動作確認
この状態で HoloLens 上で動作確認を行います。
正常に BlueTooth のアドバタイズを受信すると、テキストオブジェクトにIDとデータ値が表示されます。