2015-07-19 108 views
2

讀取iPhone燈塔按照Bluetooth Advertisement sample,我需要設置CompanyIDUINT16)和數據(IBuffer,一個UINT16樣品中)以開始觀看廣告客戶。我可以用Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData

在iPhone中,我可以將信標UUID設置爲4B503F1B-C09C-4AEE-972F-750E9D346784。並在互聯網上閱讀,我found蘋果公司的ID是0x004C,所以我試着0x004C0x4C00

所以,這是我到目前爲止的代碼,但當然,它不工作。

var manufacturerData = new BluetoothLEManufacturerData(); 

// Then, set the company ID for the manufacturer data. Here we picked an unused value: 0xFFFE 
manufacturerData.CompanyId = 0x4C00; 

// Finally set the data payload within the manufacturer-specific section 
// Here, use a 16-bit UUID: 0x1234 -> {0x34, 0x12} (little-endian) 
var writer = new DataWriter(); 
writer.WriteGuid(Guid.Parse("4B503F1B-C09C-4AEE-972F-750E9D346784")); 

// Make sure that the buffer length can fit within an advertisement payload. Otherwise you will get an exception. 
manufacturerData.Data = writer.DetachBuffer(); 

我也試過在UUID反轉字節:

writer.WriteGuid(Guid.Parse("504B1B3F-9CC0-EE4A-2F97-0E75349D8467")); 

不是成功爲止。我混合兩種完全不同的技術嗎?

回答

4

在Windows 10上檢測信標最重要的是使用新的BluetoothLeAdvertisementWatcher類。

問題中的代碼似乎專注於設置過濾器以僅查找與廣告中包含的公司代碼和UUID匹配的特定藍牙LE廣告。雖然這是一種方法,但並非絕對必要 - 您可以簡單地查找所有Bluetooth LE廣告,然後解碼它們以查看它們是否爲信標廣告。

我粘貼了下面的一些代碼,顯示了我認爲你想要做的事情。 重要提示:我沒有自己測試過這個代碼,因爲我沒有Windows 10開發環境。如果您自己嘗試並進行更正,請讓我知道,我會更新我的答案。

private BluetoothLEAdvertisementWatcher bluetoothLEAdvertisementWatcher; 

public LookForBeacons() { 
    bluetoothLEAdvertisementWatcher = new BluetoothLEAdvertisementWatcher(); 
    bluetoothLEAdvertisementWatcher.Received += OnAdvertisementReceived; 
    bluetoothLEAdvertisementWatcher.Start(); 
} 


private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs) { 

    var manufacturerSections = eventArgs.Advertisement.ManufacturerData; 
    if (manufacturerSections.Count > 0) { 
     var manufacturerData = manufacturerSections[0]; 
     var data = new byte[manufacturerData.Data.Length]; 
     using (var reader = DataReader.FromBuffer(manufacturerData.Data)) { 
      reader.ReadBytes(data); 

      // If we arrive here we have detected a Bluetooth LE advertisement 
      // Add code here to decode the the bytes in data and read the beacon identifiers 

     } 
    } 
} 

下一個明顯的問題是如何解碼廣告的字節?搜索網絡很容易,找出各種信標類型的字節序列,甚至是專有信號類型。爲了保持這個答案應該簡短並且出知識產權叢林的緣故,我將簡單介紹一下如何在開源AltBeacon廣告的字節解碼:

18 01 be ac 2f 23 44 54 cf 6d 4a 0f ad f2 f4 91 1b a9 ff a6 00 01 00 02 c5 00 

這被解碼爲:

  • 頭兩個字節是公司代碼(0x0118 =半徑網絡)
  • 接下來的兩個字節是信標類型代碼(0xacbe = AltBeacon)
  • 接下來的16個字節是所述第一標識符2F234454-CF6D-4A0F- ADF2-F4911BA9FFA6
  • 接下來的2個字節是所述第二標識符爲0x0001
  • 接下來的2個字節是所述第三標識符爲0x0002
  • 以下字節是功率校準值0xC5 - > -59 dBm的
+0

即絕對正確。我只是將'watcher.AdvertisementFilter.Advertisement.ManufacturerData'註釋掉了。Add',返回的樣本是'type = ConnectableUndirected,rssi = -50,name =,manufacturerData = [0x4C:02-15-0F-4E-1E-48-26-F7-4A-47-A0-6C-D9 -B8-42-7D-30-3F-00-01-00-00-09]'。所以,我猜,'02-15'必須是燈塔類型。 – kiewic

+0

太棒了!我很高興聽到它的工作。 – davidgyoung

+0

我已經測試過它,它工作的很好,非常感謝,它幫助了我很多! – Tom