ibeacon で実装

parent e400b870
<Window x:Class="WpfBleSampleApp.MainWindow"
<Window x:Class="iBeaconScanner.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfBleSampleApp"
xmlns:local="clr-namespace:iBeaconScanner"
mc:Ignorable="d"
Title="MainWindow"
Height="500"
Width="566.338"
Loaded="Window_Loaded"
Closed="Window_Closed">
<Grid Margin="0,62,13.5,186.5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="9*"/>
<ColumnDefinition Width="8*"/>
<ColumnDefinition Width="257*"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="TextBlockokList" Margin="24,-31,-11,0" Height="188" VerticalAlignment="Top" RenderTransformOrigin="0.495,1.012" Grid.Column="2" />
<TextBlock x:Name="TextBlockRSSI" Margin="10,162,-1,-128" Height="190" VerticalAlignment="Top" Grid.ColumnSpan="3" RenderTransformOrigin="0.492,1.471" />
Title="MainWindow" Height="350" Width="1130.975">
<Grid>
<TextBox x:Name="textBox" Margin="0" TextWrapping="Wrap" Text=""/>
</Grid>
</Window>
\ No newline at end of file
This diff is collapsed.
......@@ -70,6 +70,7 @@
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="iBeacon.cs" />
<Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
......
using System;
using Windows.Devices.Bluetooth.Advertisement;
using Windows.Storage.Streams;
public class iBeacon
{
private const int MinimumLengthInBytes = 25;//最小の長さ
private const int AdjustedLengthInBytes = -2;//CompanyID分の2桁ずれている為読み取り位置補正
//プロパティ
public string Name { get; set; }
public DateTimeOffset Timestamp { get; set; }
public BluetoothLEAdvertisementType AdvertisementType { get; set; }
public int ManufacturerId { get; set; }
public int Major { get; set; }
public int Minor { get; set; }
public string UUID { get; set; }
public short Rssi { get; set; }
public short MeasuredPower { get; set; }
public double ManufacturerReserved { get; set; }
public ulong BluetoothAddress { get; set; }
public int RawSignalStrengthInDBm { get; }
//精度(accuracy)
public double Accuracy
{
get { return calcAccuracy(MeasuredPower, Rssi); }
}
//近接度(Proximity):近接(immidiate)、1m以内(near)、1m以遠(far)、不明(Unknown)
public string Proximity
{
get
{
string _Proximity = "Unknown";
//Rssi未取得ならUnknown
if (Rssi == 0) { return _Proximity; }
//rssi値からProximityを判別
if (Rssi > -40)
{
_Proximity = "immidiate";//近接
}
else if (Rssi > -59)
{
_Proximity = "near";//1m以内
}
else
{
_Proximity = "far";//1m以遠
}
return _Proximity;
}
}
//コンストラクタ
public iBeacon()
{
ManufacturerId = -1;
Major = -1;
Minor = -1;
Rssi = 0;
UUID = "";
MeasuredPower = -1;
ManufacturerReserved = -1.0;
}
//コンストラクタ2
public iBeacon(BluetoothLEAdvertisementReceivedEventArgs eventArgs)
{
//出力されている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.CompanyId];
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
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();
}
}
//精度を計算する
protected static double calcAccuracy(short measuredPower, short rssi)
{
if (rssi == 0)
{
return -1.0; //nodata return -1.
}
double ratio = rssi * 1.0 / measuredPower;
if (ratio < 1.0)
{
return Math.Pow(ratio, 10);
}
else
{
double accuracy = (0.89976) * Math.Pow(ratio, 7.7095) + 0.111;
return accuracy;
}
}
}
#pragma checksum "..\..\MainWindow.xaml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "56F052CFB7FE06C2C911627D754264C69B1D7797"
#pragma checksum "..\..\MainWindow.xaml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "83B8DB4DECAA2131589EE936FAC7F27E5B12F855"
//------------------------------------------------------------------------------
// <auto-generated>
// このコードはツールによって生成されました。
......@@ -29,10 +29,10 @@ using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Shell;
using WpfBleSampleApp;
using iBeaconScanner;
namespace WpfBleSampleApp {
namespace iBeaconScanner {
/// <summary>
......@@ -41,17 +41,9 @@ namespace WpfBleSampleApp {
public partial class MainWindow : System.Windows.Window, System.Windows.Markup.IComponentConnector {
#line 19 "..\..\MainWindow.xaml"
#line 10 "..\..\MainWindow.xaml"
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
internal System.Windows.Controls.TextBlock TextBlockokList;
#line default
#line hidden
#line 20 "..\..\MainWindow.xaml"
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
internal System.Windows.Controls.TextBlock TextBlockRSSI;
internal System.Windows.Controls.TextBox textBox;
#line default
#line hidden
......@@ -87,24 +79,7 @@ namespace WpfBleSampleApp {
switch (connectionId)
{
case 1:
#line 11 "..\..\MainWindow.xaml"
((WpfBleSampleApp.MainWindow)(target)).Loaded += new System.Windows.RoutedEventHandler(this.Window_Loaded);
#line default
#line hidden
#line 12 "..\..\MainWindow.xaml"
((WpfBleSampleApp.MainWindow)(target)).Closed += new System.EventHandler(this.Window_Closed);
#line default
#line hidden
return;
case 2:
this.TextBlockokList = ((System.Windows.Controls.TextBlock)(target));
return;
case 3:
this.TextBlockRSSI = ((System.Windows.Controls.TextBlock)(target));
this.textBox = ((System.Windows.Controls.TextBox)(target));
return;
}
this._contentLoaded = true;
......
#pragma checksum "..\..\MainWindow.xaml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "56F052CFB7FE06C2C911627D754264C69B1D7797"
#pragma checksum "..\..\MainWindow.xaml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "83B8DB4DECAA2131589EE936FAC7F27E5B12F855"
//------------------------------------------------------------------------------
// <auto-generated>
// このコードはツールによって生成されました。
......@@ -29,10 +29,10 @@ using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Shell;
using WpfBleSampleApp;
using iBeaconScanner;
namespace WpfBleSampleApp {
namespace iBeaconScanner {
/// <summary>
......@@ -41,17 +41,9 @@ namespace WpfBleSampleApp {
public partial class MainWindow : System.Windows.Window, System.Windows.Markup.IComponentConnector {
#line 19 "..\..\MainWindow.xaml"
#line 10 "..\..\MainWindow.xaml"
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
internal System.Windows.Controls.TextBlock TextBlockokList;
#line default
#line hidden
#line 20 "..\..\MainWindow.xaml"
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
internal System.Windows.Controls.TextBlock TextBlockRSSI;
internal System.Windows.Controls.TextBox textBox;
#line default
#line hidden
......@@ -87,24 +79,7 @@ namespace WpfBleSampleApp {
switch (connectionId)
{
case 1:
#line 11 "..\..\MainWindow.xaml"
((WpfBleSampleApp.MainWindow)(target)).Loaded += new System.Windows.RoutedEventHandler(this.Window_Loaded);
#line default
#line hidden
#line 12 "..\..\MainWindow.xaml"
((WpfBleSampleApp.MainWindow)(target)).Closed += new System.EventHandler(this.Window_Closed);
#line default
#line hidden
return;
case 2:
this.TextBlockokList = ((System.Windows.Controls.TextBlock)(target));
return;
case 3:
this.TextBlockRSSI = ((System.Windows.Controls.TextBlock)(target));
this.textBox = ((System.Windows.Controls.TextBox)(target));
return;
}
this._contentLoaded = true;
......
45d4cf267363dbdf9b29ddb1b7cb274a98f2f6e1
3d0f4bf2707f6919b6b1c9310b5d98aed3a08f53
......@@ -82,3 +82,24 @@ C:\Users\Kaoru\source\repos\catchBLE\WpfApp1\obj\Debug\WpfApp1.csproj.CoreCompil
C:\Users\Kaoru\source\repos\catchBLE\WpfApp1\obj\Debug\WpfApp1.csproj.CopyComplete
C:\Users\Kaoru\source\repos\catchBLE\WpfApp1\obj\Debug\WpfApp1.exe
C:\Users\Kaoru\source\repos\catchBLE\WpfApp1\obj\Debug\WpfApp1.pdb
C:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\bin\Debug\WpfApp1.exe.config
C:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\bin\Debug\WpfApp1.exe
C:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\bin\Debug\WpfApp1.pdb
C:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\bin\Debug\System.Runtime.WindowsRuntime.dll
C:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\bin\Debug\System.Runtime.WindowsRuntime.UI.Xaml.dll
C:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\bin\Debug\Windows.WinMD
C:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\bin\Debug\System.Runtime.WindowsRuntime.xml
C:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\bin\Debug\System.Runtime.WindowsRuntime.UI.Xaml.xml
C:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\obj\Debug\WpfApp1.csprojAssemblyReference.cache
C:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\obj\Debug\MainWindow.g.cs
C:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\obj\Debug\App.g.cs
C:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\obj\Debug\WpfApp1_MarkupCompile.cache
C:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\obj\Debug\WpfApp1_MarkupCompile.lref
C:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\obj\Debug\MainWindow.baml
C:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\obj\Debug\WpfApp1.g.resources
C:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\obj\Debug\WpfApp1.Properties.Resources.resources
C:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\obj\Debug\WpfApp1.csproj.GenerateResource.cache
C:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\obj\Debug\WpfApp1.csproj.CoreCompileInputs.cache
C:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\obj\Debug\WpfApp1.csproj.CopyComplete
C:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\obj\Debug\WpfApp1.exe
C:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\obj\Debug\WpfApp1.pdb
......@@ -12,9 +12,9 @@ DEBUG;TRACE
C:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\App.xaml
11151548125
9403354300
6-1683440482
71148438499
MainWindow.xaml;
True
False
......@@ -4,17 +4,17 @@
winexe
C#
.cs
C:\Users\Kaoru\source\repos\WpfApp1\WpfApp1\obj\Debug\
C:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\obj\Debug\
WpfApp1
none
false
DEBUG;TRACE
C:\Users\Kaoru\source\repos\WpfApp1\WpfApp1\App.xaml
C:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\App.xaml
11151548125
9403354300
10737660320
71148438499
MainWindow.xaml;
False
True

FC:\Users\Kaoru\source\repos\catchBLE2\WpfApp1\MainWindow.xaml;;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment