Raspberry piでiBeacon受信」でRaspberry piでiBeaconを受信しましたが、今度はWindows10でiBeaconを受信します。

Android アプリ「 Beacon Simulator」の設定

Android アプリ「 Beacon Simulator」を使って次のiBeaconを発生します。

uuid:FFB2C814-5A15-42E2-864A-9D996FC499BC,major:2,minor:0
major: 2
minor: 0

uuid:86815C38-AE83-4244-96A3-161EF2AC9E08
major:1
minor:55

BeaconScannerアプリの作成

BeaconScanner「BeaconScanTest」アプリの作成します。「uuid」で次のクラス「BeaconScan」をインスタンス化して、「ScanStart」メソッドを呼びます。

BeaconScanTest

using System;
using Windows.Devices.Bluetooth.Advertisement;
using Windows.Storage.Streams;

namespace BeaconScanTest
{
    class BeaconScan
    {
        private const int AdjustedLengthInBytes = -2;   //CompanyID分の2桁ずれている為読み取り位置補正
        private static BluetoothLEAdvertisementWatcher watcher;

        public BeaconScan()
        {

        }
        public void ScanStart()
        {
            watcher = new BluetoothLEAdvertisementWatcher();
            watcher.Received += Watcher_Received;
            watcher.Stopped += Watcher_Stopped;

            watcher.ScanningMode = BluetoothLEScanningMode.Passive;

            watcher.Start();
            Console.ReadKey();

            watcher.Stop();

            while (true) { };

        }

        protected static void Watcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
        {
            string UUID;
            int Major;
            int Minor;
            string retBeaconData;

            var manufacturerSections = args.Advertisement.ManufacturerData;

            if (manufacturerSections.Count > 0)
            {
                var manufacturerData = manufacturerSections[0];
                var data = new byte[manufacturerData.Data.Length];

                using (var reader = DataReader.FromBuffer(manufacturerData.Data))
                {
                    reader.ReadBytes(data);
                }

                //バイトデータから抽出
                //公式での出力値(Windowsでは2byteずれているので補正が必要)
                // Byte(s)  WinByte(s) Name
                // --------------------------
                // 0-1      none       Manufacturer ID (16-bit unsigned integer, big endian)
                // 2-3      0-1        Beacon code (two 8-bit unsigned integers, but can be considered as one 16-bit unsigned integer in little endian)
                // 4-19     2-17       ID1 (UUID)
                // 20-21    18-19      ID2 (16-bit unsigned integer, big endian)
                // 22-23    20-21      ID3 (16-bit unsigned integer, big endian)
                // 24       22         Measured Power (signed 8-bit integer)
                // 25       23         Reserved for use by the manufacturer to implement special features (optional)

                string uuid = BitConverter.ToString(data, 4 + AdjustedLengthInBytes, 16);
                System.Text.StringBuilder sb = new System.Text.StringBuilder(uuid);

                sb.Replace("-", " ", 11, 1);
                sb.Replace("-", " ", 17, 1);
                sb.Replace("-", " ", 23, 1);
                sb.Replace("-", " ", 29, 1);

                sb.Replace("-", "");
                sb.Replace(" ", "-");

                UUID = sb.ToString();

                byte[] revData;

                revData = new byte[] { data[20 + AdjustedLengthInBytes], data[21 + AdjustedLengthInBytes] };    // Bytes 18-19
                Array.Reverse(revData);
                Major = BitConverter.ToUInt16(revData, 0);

                revData = new byte[] { data[22 + AdjustedLengthInBytes], data[23 + AdjustedLengthInBytes] };    // Bytes 20-21
                Array.Reverse(revData);
                Minor = BitConverter.ToUInt16(revData, 0);

                retBeaconData = string.Format("uuid:{0},", UUID);   //"00000000-0000-0000-0000-000000000000"
                retBeaconData += string.Format("major:{0},", Major.ToString("D"));
                retBeaconData += string.Format("minor:{0}", Minor.ToString("D"));
                Console.WriteLine(retBeaconData);
            }
            else
            {
                Console.Error.Write("Manufacturer Sections Count Error\n");
            }
        }
        protected static void Watcher_Stopped(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementWatcherStoppedEventArgs eventArgs)
        {
             Environment.Exit(0);
        }
    }
}

BeaconScannerアプリの実行

BeaconScanner「BeaconScanTest」アプリの実行すると、コンソールに次のようにBeacon の内容が表示されます。