本主題概述了 Windows 應用程式的藍牙低功耗(LE)廣告信標。
Important
你必須在 Package.appxmanifest 中宣告「藍牙」功能才能使用此功能。
<Capabilities> <DeviceCapability Name="bluetooth" /> </Capabilities>
支援的功能
LE 廣告 API 支援兩大主要功能:
基本設定
要在 通用 Windows 平台 應用程式中使用基本的藍牙 LE 功能,必須在 Package.appxmanifest 中檢查藍牙功能。
- 開啟 Package.appxmanifest
- 前往「能力」標籤
- 在左側的清單中找到藍牙,並勾選旁邊的方框。
發布廣告
藍牙LE廣告讓您的裝置持續發出特定的有效載荷,稱為廣告。 只要附近任何支援藍牙 LE 的裝置已設定為偵聽這個特定廣播,就都能偵測到此廣播。
Note
為了用戶隱私,你的廣告壽命與應用程式的壽命綁定。 你可以建立 BluetoothLEAdvertisementPublisher,並在背景任務中呼叫 Start 來進行廣告。 欲了解更多背景任務資訊,請參閱 啟動、繼續及背景任務。
有許多不同的方式可以為廣告加入資料。 這個例子展示了一種常見的製作公司專屬廣告的方法。
首先,建立一個廣告發布者,來控制裝置是否會發出特定廣告。
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();
注意廣告
以下程式碼示範如何建立藍牙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),且廣播封包中包含字串 Hello World。 這可以搭配 Basic Publishing 範例,讓一台 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);
注意附近廣告
有時候,你只想在裝置開始廣播並進入可偵測範圍時,才觸發監看程式。 你可以自己定義範圍,但要注意數值會被裁剪成 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 會包含一個 RSSI 值,表示接收訊號強度(也就是藍牙訊號有多強)。
private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
{
// The received signal strength indicator (RSSI)
Int16 rssi = eventArgs.RawSignalStrengthInDBm;
}
這大致可以換算成距離,但不應用來測量真實距離,因為每台無線電都不同。 不同的環境因素會讓距離難以判斷(例如牆壁、收音機周圍的機殼,甚至空氣濕度)。
作為單純以距離判定的替代方式,可以定義「區間」。 無線電在非常接近時通常報告 0 到 -50 dBm,中距離時 -50 到 -90,遠時則低於 -90。 最好透過反覆嘗試,來決定你的應用程式應如何劃分這些類別。
範例
欲了解完整功能的藍牙 LE 廣告範例,請參見 Github 上的 Bluetooth Advertisement Sample。