2017-10-12 19 views
0

BLE広告をスキャンできるプログラムを作成しようとしています。私は、Windows Universalサンプル、より正確にはBluetoothAdvertisementというサンプルを見てきました。私は、BLE広告をスキャンしてリストボックスに表示できるシンプルなUWPアプリケーションを作成したいと考えています。しかし、私のアプリケーションは全く何も見つけることができず、私は完全に失われています。BLE広告UWPアプリケーション

namespace BleDiscAdv2 
{ 

public sealed partial class MainPage : Page 
{ 
    // The Bluetooth LE advertisement watcher class is used to control and customize Bluetooth LE scanning. 
    private BluetoothLEAdvertisementWatcher watcher; 

    public MainPage() 
    { 
     this.InitializeComponent(); 

     // Create and initialize a new watcher instance. 
     watcher = new BluetoothLEAdvertisementWatcher(); 

     //Set the in-range threshold to -70dBm. This means advertisements with RSSI >= -70dBm 
     //will start to be considered "in-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); 

    } 

    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     // Attach a handler to process the received advertisement. 
     // The watcher cannot be started without a Received handler attached 
     watcher.Received += OnAdvertisementReceived; 
    } 

     private void btStart_Click(object sender, RoutedEventArgs e) 
    { 
     watcher.Start(); 
    } 

    private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs) 
    { 
     DateTimeOffset timestamp = eventArgs.Timestamp; 
     string localName = eventArgs.Advertisement.LocalName; 

     await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,() => 
     { 
      lbModtaget.Items.Add("Name of device: " + localName + "\t" + "Time for advertisement: " + timestamp.ToString("hh\\:mm\\:ss\\.fff")); 
     }); 
    } 
} 
} 

誰かが間違っていることを教えてもらえますか? 私はBLEを初めて使用しているので、しばらくコーディングしていません。

敬具 クリスチャン

答えて

0

しかし、私のアプリケーションはすべてで何かを見つけることができませんし、私は完全に失われています。

  • あなたのアプリがPackage.appxmanifestでBluetoothの機能を有効にする持っていることを確認してください。詳細は、Basic Setupを参照してください。
  • 実行中のデバイスのBluetoothラジオがオンになっていることを確認してください。
  • 広告を表示してフィルタを満たすデバイスがあります。別のデバイスでBluetooth advertisement official sampleのシナリオ2を実行して、そのことを確認できます。

私の側でテストすることで、コードスニペットでBLE広告をうまくスキャンできます。あなたのコードスニペットでは、ウォッチャーのStoppedイベントハンドルを聞きませんでした。これは、広告主のためにBluetooth LEがスキャンされたことをアプリケーションに通知するためのもので、アプリケーションによって、またはエラーのために中止または中止されました。ウォッチャーが強制停止されている場合、ウォッチャーは広告を取得しません。

Stoppedイベントハンドルを追加して、BluetoothErrorがあるかどうかを確認できます。例えば

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    // Attach a handler to process the received advertisement. 
    // The watcher cannot be started without a Received handler attached 
    watcher.Received += OnAdvertisementReceived; 
    watcher.Stopped += OnAdvertisementWatcherStopped; 
} 

private async void OnAdvertisementWatcherStopped(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementWatcherStoppedEventArgs args) 
{ 
    await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,() => 
    { 
     txtresult.Text = string.Format("Watcher stopped or aborted: {0}", args.Error.ToString()); 
    }); 
} 

RadioNotAvailableが有効にしないブルートゥース、OtherErrorは、Bluetooth機能によって引き起こされることが可能ていない実行しているデバイスによって引き起こされ得ます。ウォッチャーが停止しておらず、広告が表示されている場合は、アプリが動作するはずです。

+0

「DisabledByUser」というエラーは何を意味していますか?フォアグラウンドウォッチャーの例を実行しようとすると、これが表示されます。 – TedMilker

+0

他の人に自分自身の質問に答える:「設定されていないデバイスと通信する」が「設定」 - >「プライバシー」 - >「他のデバイス」でオフになっていました – TedMilker

関連する問題