LoginSignup
11
13

More than 5 years have passed since last update.

Windows 10 で、 iBeacon 互換信号を送信するプログラムを書いてみた。

Last updated at Posted at 2015-08-14

概要

Raspberry pi 2用のWindows 10が正式版が公開され、Bluetooth USBドングルがサポートされたので、早速、iBeacon互換信号を送信するWindows 10 universal アプリで実装してみた。

動作デバイス環境

  • Raspberry Pi 2 + Windows 10 IoT (Build 10.0.10240.16384)
  • Bluetooth USB Dongle(GH-BHDA42) ちなみに、CSR8510 A10と認識されるデバイス

開発環境環境

  • Windows 10 + Visual Studio 2015

はまった点

Bluetoothを使ったアプリケーションを作成する際は、Package.appmanifestに、Bluetoothを使うよと宣言しないと、関連APIが実行時にエラーになる。エラーも意味不明なので、混乱する。
さらに、GUIには、設定項目は無いので、コードを表示して、最後の方にある <DeviceCapability>を追記する必要がある。MSDNにも、それらしい記述があった。

Package.appxmanifest
<?xml version="1.0" encoding="utf-8"?>

<Package
  xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
  xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
  xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
  IgnorableNamespaces="uap mp">

  <Identity
    Name="24b7dfd7-6161-46e5-b890-b18e31e6e39d"
    Publisher="CN=kazuyuki"
    Version="1.0.0.0" />

  <mp:PhoneIdentity PhoneProductId="24b7dfd7-6161-46e5-b890-b18e31e6e39d" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>

  <Properties>
    <DisplayName>Beacon</DisplayName>
    <PublisherDisplayName>kazuyuki</PublisherDisplayName>
    <Logo>Assets\StoreLogo.png</Logo>
  </Properties>

  <Dependencies>
    <TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" />
  </Dependencies>

  <Resources>
    <Resource Language="x-generate"/>
  </Resources>

  <Applications>
    <Application Id="App"
      Executable="$targetnametoken$.exe"
      EntryPoint="Beacon.App">
      <uap:VisualElements
        DisplayName="Beacon"
        Square150x150Logo="Assets\Square150x150Logo.png"
        Square44x44Logo="Assets\Square44x44Logo.png"
        Description="Beacon"
        BackgroundColor="transparent">
        <uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png"/>
        <uap:SplashScreen Image="Assets\SplashScreen.png" />
      </uap:VisualElements>
    </Application>
  </Applications>

  <Capabilities>
    <DeviceCapability Name="bluetooth" />
  </Capabilities>
</Package>

プログラム

BluetoothLEAdvertisementPublisherを使えば、BLEのアドバタイズを行うことができます。現時点(2015/08/14)では、ManufacturerData以外は送信できないので、Eddystoneは実装不可でした。

MainPage.xaml.cs
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Devices.Bluetooth.Advertisement;
using Windows.UI.Xaml.Controls;

// 空白ページのアイテム テンプレートについては、http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 を参照してください

namespace Beacon
{
    /// <summary>
    /// それ自体で使用できる空白ページまたはフレーム内に移動できる空白ページ。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private BluetoothLEAdvertisementPublisher publisher;

        public MainPage()
        {
            this.InitializeComponent();

            publisher = new BluetoothLEAdvertisementPublisher();

            var manufacturerData = new BluetoothLEManufacturerData();

            manufacturerData.CompanyId = 0x004c;

            byte[] dataArray = new byte[] {
                // お決まり
                0x02, 0x15,
                // UUID
                0x01, 0x02, 0x03, 0x04,
                0x05, 0x06, 0x07, 0x08,
                0x09, 0x0a, 0x0b, 0x0c,
                0x0d, 0x0e, 0x0f, 0x10,
                // Major
                0x01, 0x00,
                // Minor
                0x00, 0x01,
                // TX power
                0xc5
            };

            manufacturerData.Data = dataArray.AsBuffer();

            publisher.Advertisement.ManufacturerData.Add(manufacturerData);

            publisher.Start();
        }
    }
}

以上、ご参考までに

11
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
13