2015-11-20 12 views
7

これまでRFCOMMをサポートするBluetoothデバイスとペアリングしています。 私のアプリが開かれたら、RFCOMMを開いてデバイスに接続しようとします。この方法では、デバイスが範囲内に入ったら自動的に接続します。Windows 10 - Bluetoothデバイスが範囲内にあることを検出する方法

deviceInfoCollection = await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort)); 
     LogData(String.Format("Number of mldp devices is {0}", deviceInfoCollection.Count)); 
     foreach (DeviceInformation deviceInfo in deviceInfoCollection) 
     { 
      LogData(String.Format("ID:{0}, NAME:{1}", deviceInfo.Id, deviceInfo.Name)); 
     } 

はその後、タイマーでこれを実行します。

try 
      { 
       // The first time this method is invoked by a store app, it should be called 
       // from a UI thread in order to display the consent prompt 
       // https://msdn.microsoft.com/en-us/windows.devices.bluetooth.rfcomm.rfcommdeviceservice.fromidasync 
       RfcommDeviceService rfcommService = await RfcommDeviceService.FromIdAsync(deviceInfo.Id); 
       LogData(String.Format("ID:{0}, NAME:{1}", deviceInfo.Id, deviceInfo.Name)); 
      } 
      catch (Exception) 
      { 
       LogData(String.Format("Can not request rfcomm service from device ID:{0}, NAME:{1}", deviceInfo.Id, deviceInfo.Name)); 
      } 

は、デバイスではなく接続しようとするよりも、範囲内にあるときに照会する方法はありますか?私はデバイスが範囲内にあるときだけ接続を試みることを好むだろう。 RFCOMM(BT2.0、BT2.1)については

答えて

2

あなたは、接続の試行であなたの実際の実装は少し良く働くことしかしまたGet bluetooth devices in range

を参照して、定期的にデバイスの列挙を実行することができます。ブルートゥース4.0の場合

、あなたは組み込みデバイス(例えば、いくつかのロボット、またはRFCOMMを使用して自作のアプライアンス)に話をしている場合は、私が怖いもhttps://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BluetoothAdvertisement

を参照して、BTモジュールの広告を聞くことができますあなたがやっていることよりも良い解決策ではありません。

携帯電話(BT4.0とBT2.1の両方をサポート)に行く場合、BT4広告を使用してデバイスの近接を知らせ、その後RFCOMM経由で接続できます。

関連する問題