수색…


비고

선적 서류 비치

노트

  • Windows 10은 중앙 모드에서만 작동 할 수 있으므로 주변 장치 모드를 지원하는 장치에만 연결할 수 있습니다. 이로 인해 두 개의 Windows 10 장치가 Bluetooth LE를 통해 연결할 수 없습니다.
  • Windows 10은 연결하기 위해 Bluetooth LE 장치와 쌍을 이루어야합니다.

초기 설정

Universal Windows Platform 응용 프로그램에서 Bluetooth 기능을 사용하려면 Package.appxmanifest 에서 Bluetooth 기능을 확인해야합니다.

  1. Package.appxmanifest 열기
  2. Capabilities 탭으로 이동하십시오.
  3. 왼쪽에서 Bluetooth 를 찾고 옆에있는 확인란을 선택하십시오.

Bluetooth LE 광고 만들기

이 예에서는 Windows 10 장치에서 사용자 정의 페이로드를 포 그라운드로 보급하는 방법을 보여줍니다. 페이로드는 구성된 회사 (0xFFFE로 식별 됨)를 사용하고 문자열 Hello World 를 광고에 알립니다.

BluetoothLEAdvertisementPublisher publisher = new BluetoothLEAdvertisementPublisher();

// 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);

publisher.Start();

참고 : 이것은 전경 (앱이 열려있는 동안)의 광고에만 해당됩니다.

Bluetooth LE 광고 듣기

일반 청취

이 예는 특정 광고를 수신하는 방법을 보여줍니다.

BluetoothLEAdvertisementWatcher watcher = new BluetoothLEAdvertisementWatcher();

// Use active listening if you want to receive Scan Response packets as well
// this will have a greater power cost.
watcher.ScanningMode = BluetoothLEScanningMode.Active;

// Register a listener, this will be called whenever the watcher sees an advertisement. 
watcher.Received += OnAdvertisementReceived;

watcher.Start();

Advertisement Filter : 특정 광고 듣기

때로는 특정 광고를 듣고 싶습니다. 이 경우 구성된 회사가있는 페이로드 (0xFFFE로 식별 됨)가 들어 있고 광고에서 Hello World 문자열을 포함하는 보급 알림을 수신합니다. 이것은 하나의 Windows 컴퓨터 광고와 다른 청취를 가지기 위해 Bluetooth LE 광고 작성 예제와 쌍을 이룰 수 있습니다.

참고 : 감시자를 시작하기 전에이 광고 필터를 설정하십시오!

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);

콜백

watcher.Received += OnAdvertisementReceived;
watcher.Stopped += OnAdvertisementWatcherStopped;

private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
{
    // Do whatever you want with the advertisement

    // The received signal strength indicator (RSSI)
    Int16 rssi = eventArgs.RawSignalStrengthInDBm;
}


private async void OnAdvertisementWatcherStopped(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementWatcherStoppedEventArgs eventArgs)
{
    // Watcher was stopped
}

참고 : 이것은 포 그라운드에서 듣기위한 것입니다.

블루투스 LE 광고의 RSSI 기반 심판 거리

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 미만으로보고하는 경향이 있습니다. 시도와 오류는 귀하가 응용 프로그램을 위해 버킷을 원하는 바를 결정하는 데 가장 좋습니다.



Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow