private async void Watcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args) { await this.Dispatcher.InvokeAsync(() => { // http://sonic.blue/it/605 // Windows10デバイスでiBeaconの全データを取得する方法 iBeacon bcon = new iBeacon(args); if (bcon.UUID != null) { // iBeacon DateTimeOffset timestamp = args.Timestamp; string retBeaconData; retBeaconData = "{"; retBeaconData += string.Format("vendor:'{0}',", bcon.iBeaconVendor); retBeaconData += string.Format("uuid:'{0}',", bcon.UUID); retBeaconData += string.Format("major:{0},", bcon.Major.ToString("D")); retBeaconData += string.Format("minor:{0},", bcon.Minor.ToString("D")); retBeaconData += string.Format("measuredPower:{0},", bcon.MeasuredPower.ToString("D")); retBeaconData += string.Format("rssi:{0},", bcon.Rssi.ToString("D")); retBeaconData += string.Format("accuracy:{0},", bcon.Accuracy.ToString("F6")); retBeaconData += string.Format("proximity:'{0}',", bcon.Proximity); retBeaconData += string.Format("BluetoothAddress:'{0}',", bcon.BluetoothAddress); retBeaconData += string.Format("RawSignalStrengthInDBm:{0}", bcon.RawSignalStrengthInDBm); retBeaconData += "}"; this.textBox.Text = this.textBox.Text + timestamp.ToString("HH\\:mm\\:ss\\.fff") + ":" + retBeaconData + "\r\n"; } }); }
//コンストラクタ2 public iBeacon(BluetoothLEAdvertisementReceivedEventArgs eventArgs) { BluetoothAddress = eventArgs.BluetoothAddress.ToString("X"); RawSignalStrengthInDBm = eventArgs.RawSignalStrengthInDBm; //出力されているbyteデータから各値を抽出する var manufacturerSections = eventArgs.Advertisement.ManufacturerData; Timestamp = eventArgs.Timestamp; AdvertisementType = eventArgs.AdvertisementType; if (manufacturerSections.Count > 0) { var manufacturerData = manufacturerSections[0]; var data = new byte[manufacturerData.Data.Length]; iBeacon bcon = new iBeacon(); using (var reader = DataReader.FromBuffer(manufacturerData.Data)) { reader.ReadBytes(data); } //長さをチェック if (data == null || data.Length < MinimumLengthInBytes + AdjustedLengthInBytes) { return; } //イベントから取得 Rssi = eventArgs.RawSignalStrengthInDBm; Name = eventArgs.Advertisement.LocalName; ManufacturerId = manufacturerData.CompanyId; //バイトデータから抽出 //公式での出力値(Windowsでは2byteずれているので補正が必要) // Byte(s) WinByte(s) Name // -------------------------- // 0-1 none Manufacturer ID (16-bit unsigned integer, big endian) // 2-3 0-1 Beacon code (two 8-bit unsigned integers, but can be considered as one 16-bit unsigned integer in little endian) // 4-19 2-17 ID1 (UUID) // 20-21 18-19 ID2 (16-bit unsigned integer, big endian) // 22-23 20-21 ID3 (16-bit unsigned integer, big endian) // 24 22 Measured Power (signed 8-bit integer) // 25 23 Reserved for use by the manufacturer to implement special features (optional) //BigEndianの値を取得 { //UUID = BitConverter.ToString(data, 4 + AdjustedLengthInBytes, 16); // Bytes 2-17 //00000000 - 0000 - 0000 - 0000 - 000000000000 int index = 4 + AdjustedLengthInBytes; UUID = BitConverter.ToString(data, index, 4).Replace("-", "") + "-"; index = index + 4; UUID = UUID + BitConverter.ToString(data, index, 2).Replace("-", "") + "-"; index = index + 2; UUID = UUID + BitConverter.ToString(data, index, 2).Replace("-", "") + "-"; index = index + 2; UUID = UUID + BitConverter.ToString(data, index, 2).Replace("-", "") + "-"; index = index + 2; UUID = UUID + BitConverter.ToString(data, index, 6).Replace("-", ""); index = index + 6; } MeasuredPower = Convert.ToSByte(BitConverter.ToString(data, 24 + AdjustedLengthInBytes, 1), 16); // Byte 22 //もし追加データがあればここで取得 if (data.Length >= MinimumLengthInBytes + AdjustedLengthInBytes + 1) { ManufacturerReserved = data[25 + AdjustedLengthInBytes]; // Byte 23 } //.NET FramewarkのEndianはCPUに依存するらしい if (BitConverter.IsLittleEndian) { //LittleEndianの値を取得 byte[] revData; revData = new byte[] { data[20 + AdjustedLengthInBytes], data[21 + AdjustedLengthInBytes] };// Bytes 18-19 Array.Reverse(revData); Major = BitConverter.ToUInt16(revData, 0); revData = new byte[] { data[22 + AdjustedLengthInBytes], data[23 + AdjustedLengthInBytes] };// Bytes 20-21 Array.Reverse(revData); Minor = BitConverter.ToUInt16(revData, 0); } else { //BigEndianの値を取得 Major = BitConverter.ToUInt16(data, 20 + AdjustedLengthInBytes); // Bytes 18-19 Minor = BitConverter.ToUInt16(data, 22 + AdjustedLengthInBytes); // Bytes 20-21 } } else { new iBeacon(); } }