Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

unity3d - How to send BLE advertisements from Android to Unity on HoloLens v2

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.

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

}

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

We tested the Bluetooth.Advertisement API and works well on the HoloLens. I found that you assigned the CompanyId(a 16-bit unsigned integer) property a signed decimal number, but we usually provide a hexadecimal number as a Bluetooth LE company identifier code. Could you double-check this point both in your watcher and publisher? For example, it should look like 0xFFFE. Besides, more information about how to use the Bluetooth Advertisement API to send and receive Bluetooth Low Energy advertisements please see:Bluetooth advertisement sample


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...