如何在 HoloLens v2 上将 BLE 广告从 Android 发送到 Unity [英] How to send BLE advertisements from Android to Unity on HoloLens v2

查看:34
本文介绍了如何在 HoloLens v2 上将 BLE 广告从 Android 发送到 Unity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地使用 BLE 广告从一个 Android 设备广播信息并在另一个设备上接收它.现在我希望观察者成为在 HoloLens v2 上运行的 Unity 应用程序.HoloLens 不需要连接到 android 设备,因为我知道 这似乎没有支持.我正在寻找广播公司 ->观察者解决方案.

I have already successfully used BLE advertising to broadcast information from one android device and receive it on another. Now I want the observer to be a Unity-app running on the HoloLens v2. The HoloLens does not need to connect to the android-device as I am aware that this does not seem to be supported. I am looking for a broadcaster -> observer solution.

如前所述,我已经编写了广播器,并且在 android 上运行良好 ->安卓.现在我已经在 Unity 中实现了我的观察者,主要受到 这篇文章,它看起来像这样:

As mentioned, I already have the broadcaster written and it works fine with android -> android. Now I have implemented my observer in Unity, largely inspired by this article, and it looks like this:

#if ENABLE_WINMD_SUPPORT
using System;
using Windows.Devices.Bluetooth.Advertisement;
#endif

public class DemoManager : MonoBehaviour
{

    [SerializeField] private StatusDisplay statusDisplay;


    private void Awake()
    {
#if ENABLE_WINMD_SUPPORT
        StartWatcher();
#else
        statusDisplay.Display("UWP APIs are not supported on this platform!");
#endif
    }

#if ENABLE_WINMD_SUPPORT
    private void StartWatcher()
    {
        void OnAdvertisementReceived(object sender, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
        {
            statusDisplay.Display("Advertisement received!");
        }

        try {
            BluetoothLEAdvertisementWatcher watcher = new BluetoothLEAdvertisementWatcher();
            watcher.AdvertisementFilter.Advertisement.ManufacturerData.Add(GetManufacturerData());
            watcher.Received += OnAdvertisementReceived;
            watcher.Start();
            
            statusDisplay.Display("Watcher started!");
        } catch (Exception e){
            statusDisplay.Display($"Watcher could not start! Error: {e.Message}");
        }
    }

    private BluetoothLEManufacturerData GetManufacturerData()
    {
        var manufacturerData = new BluetoothLEManufacturerData();
        manufacturerData.CompanyId = 1234;

        return manufacturerData;
    }
#endif

}

StatusDisplay 脚本用于以线程安全的方式显示文本.广播公司也使用公司 ID 1234.

The StatusDisplay script is used for displaying text in a thread-safe way. The company-id 1234 is also used by the broadcaster.

我的应用具有蓝牙功能(在 Unity 编辑器和内置解决方案中均启用)

My app has bluetooth capabilities (enabled both in the Unity-editor and in the built solution)

一切看起来都很有希望,但遗憾的是广告似乎从未收到,或者至少我没有收到相应的状态消息.

All looks very promising, but sadly the advertisement never seems to be received, or at the very least I am getting no corresponding status message.

有人知道什么可能是错的吗?有没有人遇到过这个问题?

Does anybody have any ide what might be wrong? Does anyone have any experience with this problem?

推荐答案

问题不在于 Unity 端.我的广告格式不正确.我用一个观察者测试了我的广告,这个观察者也是我自己在 Android 上写的.所以我在那里考虑了不正确的格式,但当然,C# 广告观察者没有.

The problem was not with the Unity-side. My advertisement was malformed. I tested my advertisements with a observer that I also wrote myself on Android. So I accounted for the incorrect formatting there, but of course, the C# Advertisement-watcher did not.

这篇关于如何在 HoloLens v2 上将 BLE 广告从 Android 发送到 Unity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆