이 항목에서는 Windows 앱용 Bluetooth LE(저에너지) 광고 비콘에 대한 개요를 제공합니다.
Important
이 기능을 사용하려면 Package.appxmanifest 에서 "bluetooth" 기능을 선언해야 합니다.
<Capabilities> <DeviceCapability Name="bluetooth" /> </Capabilities>
지원되는 기능
LE 광고 API에서 지원하는 두 가지 주요 기능이 있습니다.
- 광고 감시자: 근처의 비콘을 듣고 페이로드 또는 근접성을 기준으로 필터링합니다.
- Advertisement Publisher: 개발자를 대신해 Windows에서 광고할 페이로드를 정의합니다.
기본 설정
유니버설 Windows 플랫폼 앱에서 기본 Bluetooth LE 기능을 사용하려면 Package.appxmanifest에서 Bluetooth 기능을 확인해야 합니다.
- Package.appxmanifest 열기
- 기능 탭으로 이동
- 왼쪽 목록에서 Bluetooth를 찾아 옆에 있는 확인란을 선택합니다.
광고 게시
Bluetooth LE 광고를 사용하면 디바이스가 광고라고 하는 특정 페이로드를 지속적으로 비콘할 수 있습니다. 이 광고는 이 특정 광고를 수신하도록 설정된 경우 근처의 Bluetooth LE 지원 장치에서 볼 수 있습니다.
메모
사용자 개인 정보 보호를 위해 광고의 수명은 앱의 수명과 연결됩니다. BluetoothLEAdvertisementPublisher를 만들고 백그라운드에서 광고를 위해 백그라운드 작업에서 시작을 호출할 수 있습니다. 백그라운드 작업에 대한 자세한 내용은 시작, 다시 시작 및 백그라운드 작업을 참조하세요.
광고에 데이터를 추가하는 방법에는 여러 가지가 있습니다. 이 예제에서는 회사별 광고를 만드는 일반적인 방법을 보여줍니다.
먼저 디바이스가 특정 광고를 비추고 있는지 여부를 제어하는 광고 게시자를 만듭니다.
BluetoothLEAdvertisementPublisher publisher = new BluetoothLEAdvertisementPublisher();
둘째, 사용자 지정 데이터 섹션을 만듭니다. 이 예제에서는 할당되지 않은 CompanyId 값 0xFFFE을 사용하고 광고에 텍스트 헬로 월드를 추가합니다.
// 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 광고 감시자를 만들고, 콜백을 설정하고, 모든 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.ScanningMode = BluetoothLEScanningMode.Active;
특정 광고 패턴 감시
특정 광고를 수신 대기하려는 경우가 있습니다. 이 경우, 가상의 회사(0xFFFE로 식별됨)가 포함된 페이로드와 광고 내의 헬로 월드 문자열을 포함하는 광고 패킷을 수신 대기합니다. 이는 Basic Publishing 예제와 함께 사용하여 한 대의 Windows 머신은 자신의 존재를 알리고 다른 한 대는 이를 감시하도록 할 수 있습니다. 감시자를 시작하기 전에 이 광고 필터를 설정해야 합니다.
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);
주변 광고 보기
경우에 따라 디바이스 광고가 범위에 있을 때만 감시자를 트리거하려고 합니다. 고유한 범위를 정의할 수 있습니다. 값이 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의 콜백이 트리거되면 eventArgs에는 수신된 신호 강도(Bluetooth 신호가 얼마나 강한지)를 알려주는 RSSI 값이 포함됩니다.
private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
{
// The received signal strength indicator (RSSI)
Int16 rssi = eventArgs.RawSignalStrengthInDBm;
}
대략적으로 거리로 변환할 수 있지만 각 개별 라디오가 다르기 때문에 실제 거리를 측정하는 데 사용하면 안 됩니다. 다양한 환경 요인으로 인해 거리를 측정하기 어려울 수 있습니다(예: 벽, 라디오 주변 케이스 또는 공기 습도).
순수 거리를 판단하는 대안은 "버킷"을 정의하는 것입니다. 무선 장치는 매우 가까이 있을 때는 일반적으로 0~-50 dBm, 중간 거리에 있을 때는 -50~-90 dBm, 멀리 떨어져 있을 때는 -90 dBm 미만을 보고합니다. 앱에서 이러한 버킷을 어떻게 설정할지 결정하려면 시행착오를 거치는 것이 가장 좋은 방법입니다.
Example
Bluetooth LE 광고의 완전한 기능 예제는 Github의 Bluetooth 광고 샘플 참조하세요.
Windows developer