Bluetooth LE Advertisements

이 항목은 UWP(Universal Windows Platform) 앱용 Bluetooth Low Energy (LE) Advertisement 알림에 대한 개요를 제공합니다.

중요

이 기능을 사용하려면 Package.appxmanifest에서 "블루투스" 기능을 선언해야 합니다.

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

중요 API

지원되는 기능

LE Advertisement API는 두 가지 기본 기능을 지원합니다.

  • Advertisement Watcher: 근처의 알림을 듣고 페이로드 또는 근접성을 기준으로 필터링합니다.
  • Advertisement Publisher: Windows에서 보급할 페이로드를 개발자를 대신하여 정의합니다.

예시

Bluetooth LE Advertisements 기능의 전체 예시는 Github의 Bluetooth 광고 샘플을 참조하세요.

기본 설정

Universal Windows Platform 앱에서 기본 Bluetooth LE 기능을 사용하려면 Package.appxmanifest에서 Bluetooth 기능을 검사 합니다.

  1. Package.appxmanifest 열기
  2. 기능 탭으로 이동하기
  3. 왼쪽 목록에서 Bluetooth를 찾아 옆에 있는 상자를 체크합니다.

광고 게시하기

Bluetooth LE Advertisements를 사용하면 장치가 광고라고 하는 특정 페이로드를 지속적으로 비콘할 수 있습니다. 특정 광고를 수신하도록 설정된 경우 인근의 Bluetooth LE 지원 장치에서 광고를 볼 수 있습니다.

참고 항목

사용자 개인 정보 보호를 위해 광고의 수명은 앱의 수명과 관련이 있습니다. BluetoothLEAdvertisementPublisher를 만들고 백그라운드에서 광고를 위해 백그라운드 작업에서 시작을 호출할 수 있습니다. 백그라운드 작업에 대한 자세한 정보는 시작, 다시 시작 및 백그라운드 작업을 참조하세요.

광고에 데이터를 추가하기 위한 여러 가지 방법이 있습니다. 이 예시는 회사별 광고를 만드는 일반적인 방법을 보여줍니다.

먼저 장치가 특정 광고를 비추고 있는지 여부를 제어하는 광고 게시자를 만듭니다.

BluetoothLEAdvertisementPublisher publisher = new BluetoothLEAdvertisementPublisher();

그다음 사용자 지정 데이터 섹션을 만듭니다. 이 예시는 할당되지 않은 CompanyId 값인 0xFFFE를 사용하고 Hello World 텍스트를 광고에 추가합니다.

// Add custom data to the advertisement
var manufacturerData = new BluetoothLEManufacturerData();
manufacturerData.CompanyId = 0xFFFE;

var writer = new DataWriter();
writer.WriteString("Hello World");

// Make sure that the buffer length can fit within an advertisement payload (~20 bytes). 
// Otherwise you will get an exception.
manufacturerData.Data = writer.DetachBuffer();

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

게시자가 만들어지고 설정되었으므로 Start를 호출하여 광고를 시작할 수 있습니다.

publisher.Start();

광고 시청하기

다음의 코드는 Bluetooth LE Advertisement Watcher를 만들고, 콜백을 설정하고, 모든 LE 광고를 시청하는 방법을 보여 줍니다.

BluetoothLEAdvertisementWatcher watcher = new BluetoothLEAdvertisementWatcher();
watcher.Received += OnAdvertisementReceived;
watcher.Start();
private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
{
    // Do whatever you want with the advertisement
}

활성 검사

검사 응답 광고도 받으려면 Watcher를 만든 후 다음과 같이 설정합니다. 이로 인해 전원 소모가 늘어나고 백그라운드 모드에서는 사용할 수 없습니다.

watcher.ScanningMode = BluetoothLEScanningMode.Active;

특정 광고 패턴 감시하기

특정 광고를 수신 대기하려는 경우가 있습니다. 이 경우 구성된 회사(0xFFFE 식별됨)와 광고의 Hello World 문자열이 포함된 페이로드가 포함된 광고를 수신 대기합니다. 이는 기본 게시 예시와 쌍을 이뤄 하나의 Windows 컴퓨터 광고와 다른 시청을 수행할 수 있습니다. 이 광고 필터를 Watcher를 시작하기 전에 설정하세요!

var manufacturerData = new BluetoothLEManufacturerData();
manufacturerData.CompanyId = 0xFFFE;

// Make sure that the buffer length can fit within an advertisement payload (~20 bytes). 
// Otherwise you will get an exception.
var writer = new DataWriter();
writer.WriteString("Hello World");
manufacturerData.Data = writer.DetachBuffer();

watcher.AdvertisementFilter.Advertisement.ManufacturerData.Add(manufacturerData);

주변 광고 시청하기

경우에 따라 장치 광고가 범위에 있을 때만 Watcher를 트리거하려고 합니다. 고유한 범위를 정의할 수 있으며, 값은 0에서 -128 사이로 잘립니다.

// Set the in-range threshold to -70dBm. This means advertisements with RSSI >= -70dBm 
// will start to be considered "in-range" (callbacks will start in this range).
watcher.SignalStrengthFilter.InRangeThresholdInDBm = -70;

// Set the out-of-range threshold to -75dBm (give some buffer). Used in conjunction 
// with OutOfRangeTimeout to determine when an advertisement is no longer 
// considered "in-range".
watcher.SignalStrengthFilter.OutOfRangeThresholdInDBm = -75;

// Set the out-of-range timeout to be 2 seconds. Used in conjunction with 
// OutOfRangeThresholdInDBm to determine when an advertisement is no longer 
// considered "in-range"
watcher.SignalStrengthFilter.OutOfRangeTimeout = TimeSpan.FromMilliseconds(2000);

측정 거리

Bluetooth LE Watcher의 콜백이 트리거되면 수신된 신호 강도(Bluetooth 신호가 얼마나 강한지)를 알려주는 RSSI 값이 eventArgs에 포함됩니다.

private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
{
   // The received signal strength indicator (RSSI)
   Int16 rssi = eventArgs.RawSignalStrengthInDBm;
}

대략적으로 거리로 변환할 수 있지만 각 개별 라디오가 다르기 때문에 실제 거리를 측정하는 데 사용하지 않아야 합니다. 다양한 환경 요인으로 인해 거리를 측정하기 어려울 수 있습니다(벽, 라디오 주변 케이스 또는 공기 습도 등).

순수 거리를 판단하기 위한 대안은 "버킷"을 정의하는 것입니다. 라디오는 매우 가까울 때 0~-50DBm, 중간 거리일 경우 -50~-90, 멀리 떨어져 있을 때는 -90 미만을 보고하는 경향이 있습니다. 평가판 및 오류는 앱에 대해 이러한 버킷을 원하는 항목을 결정하는 데 가장 적합합니다.