【问题标题】:setting the BluetoothLEAdvertisement.Flags in C# for iBeacon advertisemet在 C# 中为 iBeacon 广告设置 BluetoothLEAdvertisement.Flags
【发布时间】:2016-06-10 20:11:32
【问题描述】:

MS 提供了samples 来发送和接收低功耗蓝牙广告。 我看到this very helpful answer 分解了 iBeacon 数据包。还有an example 用于将BluetoothLEAdvertisement.ManufacturerData 设置为ibeacon 标准。

请问如何设置BluetoothLEAdvertisement 的Flags? 例如将值设置为: 02-01-06

谢谢


编辑 1: 代码如下:

using System;
using System.Management;
using System.Text.RegularExpressions;
using Windows.Devices.Bluetooth.Advertisement;
using System.Runtime.InteropServices.WindowsRuntime;

namespace BLE_iBeacon
{
    class IBeacon
    {
        static void Main()
        {
            Console.WriteLine("Advertising as iBeacon. Press Enter to exit");

            // Create and initialize a new publisher instance.
            BluetoothLEAdvertisementPublisher publisher = new BluetoothLEAdvertisementPublisher();

            // Add a manufacturer-specific section:
            var manufacturerData = new BluetoothLEManufacturerData();

            // Set the company ID for the manufacturer data.
            // 0x004C   Apple, Inc.
            manufacturerData.CompanyId = 0x004C;

            byte[] dataArray = new byte[] {
                // last 2 bytes of Apple's iBeacon
                0x02, 0x15,
                // UUID E4 C8 A4 FC F6 8B 47 0D 95 9F 29 38 2A F7 2C E7
                0xE4, 0xC8, 0xA4, 0xFC,
                0xF6, 0x8B, 0x47, 0x0D,
                0x95, 0x9F, 0x29, 0x38,
                0x2A, 0xF7, 0x2C, 0xE7,
                // Major
                0x00, 0x00,
                // Minor
                0x00, 0x01,
                // TX power
                0xC5
            };

            manufacturerData.Data = dataArray.AsBuffer();

            // Add the manufacturer data to the advertisement publisher:
            publisher.Advertisement.ManufacturerData.Add(manufacturerData);

            publisher.Advertisement.Flags = BluetoothLEAdvertisementFlags.GeneralDiscoverableMode;

            publisher.Start();
            Console.Read();
            publisher.Stop();
        }
    }
}

编辑 2:

在 C# 代码中,如果我不设置标志,我的 Windows 笔记本电脑会宣传原始数据包,例如:

04 3E 27 02 01 
02 01 0D 45 84 D3 68 21 1B 1A FF 4C 00 
02 15 E4 C8 A4 FC F6 8B 47 0D 95 9F 29 38 2A F7 2C E7 
00 00 00 01 C5 BA

我的目的是使用树莓派作为 BLE 接收器。我使用了 Radius Networks 的代码here。您可以在ibeacon_scan 脚​​本中看到,他们通过以下方式检查广告的数据包以查看它是否是 iBeacon:

if [[ $packet =~ ^04\ 3E\ 2A\ 02\ 01\ .{26}\ 02\ 01\ .{14}\ 02\ 15 ]]; then

因此之前的原始数据包将无法识别,因为缺少标志部分。我想知道是否可以使用标志来宣传数据包,例如:

04 3E 2A 02 01 
02 01 0D 45 84 D3 68 21 1B **02 01 1A** 1A FF 4C 00 
02 15 E4 C8 A4 FC F6 8B 47 0D 95 9F 29 38 2A F7 2C E7 
00 00 00 01 C5 BA

而不是更改 pi 中的扫描脚本。

【问题讨论】:

    标签: c# bluetooth windows-10 bluetooth-lowenergy ibeacon


    【解决方案1】:

    Windows 上的 iBeacon

    以下代码在 Windows 10 机器上发布 iBeacon:

    // Create and initialize a new publisher instance.
    BluetoothLEAdvertisementPublisher publisher = new BluetoothLEAdvertisementPublisher();
    
    // Add a manufacturer-specific section:
    var manufacturerData = new BluetoothLEManufacturerData();
    
    // Set the company ID for the manufacturer data.
    // 0x004C   Apple, Inc.
    manufacturerData.CompanyId = 0x004c;
    
    // Create the payload
    var writer = new DataWriter();
    byte[] dataArray = new byte[] {
        // last 2 bytes of Apple's iBeacon
        0x02, 0x15,
        // UUID e2 c5 6d b5 df fb 48 d2 b0 60 d0 f5 a7 10 96 e0
        0xe2, 0xc5, 0x6d, 0xb5, 
        0xdf, 0xfb, 0x48, 0xd2, 
        0xb0, 0x60, 0xd0, 0xf5, 
        0xa7, 0x10, 0x96, 0xe0,
        // Major
        0x00, 0x00,
        // Minor
        0x00, 0x01,
        // TX power
        0xc5
    };
    writer.WriteBytes(dataArray);
    
    manufacturerData.Data = writer.DetachBuffer();
    
    // Add the manufacturer data to the advertisement publisher:
    publisher.Advertisement.ManufacturerData.Add(manufacturerData);
    
    publisher.Start();
    

    接近 UUID

    在对此进行测试时,我的 iOS 设备无法识别您提供的 Proximity UUID。我猜这是因为你自己生成了它,所以应用程序不知道要查找什么。相反,我使用了来自this answer 的邻近 UUID,它将 Windows 10 设备标识为 AirLocate iBeacon。

    标志

    Windows 10 目前不允许开发人员为蓝牙 LE 广告设置标志。幸运的是,要让 Windows 设备被识别为 iBeacon,您不需要这些标志!

    【讨论】:

    • 非常感谢!我添加了:publisher.Advertisement.Flags = BluetoothLEAdvertisementFlags.GeneralDiscoverableMode; 介于:publisher.Advertisement.ManufacturerData.Add(manufacturerData);publisher.Start() ; 但我收到以下错误:发生了“System.Exception”类型的未处理异常...附加信息:数据无效。检测到无效的广告负载。
    • 您能否将广告创建代码添加到问题(或要点)中,以便我们查看发生了什么?
    • 原始问题已编辑并添加了创建代码。谢谢!
    • 我已经完成了这项工作,我会更新我的答案:)
    • 非常感谢! This answer 给出了读取主板 uuid 的代码。但这是下一步。目前我只需要让接近 uuid 被读取。我刚刚更新了我的问题,但看起来我必须更改 pi 中的扫描脚本 ...
    【解决方案2】:

    理想情况下,您希望将标志字节设置为 0x1a,但其他值可能仍然有效。要设置的重要标志是 General Discoverable (0x02)。

    您可以使用BluetoothLEAdvertisementFlags 是位值的枚举here

    我的 C# 非常生锈,但您可以尝试直接设置 flags 十六进制值:publisher.Advertisement.Flags = 0x1A;

    【讨论】:

    • 感谢提醒!但我仍然收到以下错误:“存在显式转换”
    • 抱歉,我对这个错误并不感到意外。也许 C# 技能(或工作 C# 开发环境)不太生疏的人可以根据我发布的内容找出正确的语法来设置该标志值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    相关资源
    最近更新 更多