protected override void OnNavigatedTo(NavigationEventArgs e) { if (this.Frame.CanGoBack) { SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible; SystemNavigationManager.GetForCurrentView().BackRequested += Publish_BackRequested; } this.publisher = new BluetoothLEAdvertisementPublisher(); ushort id = 0x1234; var manufacturerDataWriter = new DataWriter(); manufacturerDataWriter.WriteUInt16(id); var manufacturerData = new BluetoothLEManufacturerData { CompanyId = 0xFFFE, Data = manufacturerDataWriter.DetachBuffer() }; publisher.Advertisement.ManufacturerData.Add(manufacturerData); this.Manufacturer = "12-34"; publisher.Start(); }
private void publisher_Setup(ushort companyId, string beaconId) { // Add a manufacturer-specific section: // First, let create a manufacturer data section var manufacturerData = new BluetoothLEManufacturerData(); // Then, set the company ID for the manufacturer data. Here we picked an unused value: 0xFFFE // manufacturerData.CompanyId = 0xFFFE; manufacturerData.CompanyId = companyId; // 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(); //UInt16 uuidData = 0x1234; //writer.WriteUInt16(uuidData); string deviceData = beaconId; writer.UnicodeEncoding = UnicodeEncoding.Utf8; writer.WriteString(deviceData); // Make sure that the buffer length can fit within an advertisement payload. Otherwise you will get an exception. manufacturerData.Data = writer.DetachBuffer(); // Add the manufacturer data to the advertisement publisher: publisher.Advertisement.ManufacturerData.Clear(); publisher.Advertisement.ManufacturerData.Add(manufacturerData); // Display the information about the published payload PublisherPayloadBlock.Text = string.Format("Published payload information: CompanyId=0x{0}, ManufacturerData='{1}'", manufacturerData.CompanyId.ToString("X"), //uuidData.ToString("X")); deviceData); }
/// custom advertisements according to: /// https://docs.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.advertisement.bluetoothleadvertisementpublisher public bool StartAdvertisingCustom(ushort companyid, IBuffer data) { isCurrentlyStarting = true; // Add custom data to the advertisement var manufacturerData = new BluetoothLEManufacturerData(); manufacturerData.CompanyId = companyid; // Apple manufacturerData.Data = data; // Clear previous values just in case publisher.Advertisement.ManufacturerData.Clear(); publisher.Advertisement.DataSections.Clear(); // Add the manufacturer data to the advertisement publisher: publisher.Advertisement.ManufacturerData.Add(manufacturerData); try { publisher.Start(); isCurrentlyStarting = false; return(true); } catch { System.Diagnostics.Debug.WriteLine("ADVERTISING PUBLISHER EXCEPTION!"); isCurrentlyStarting = false; return(false); } }
/// <summary> /// Start to broadcast a Bluetooth advertisement. /// </summary> /// <returns></returns> public async Task SetupBluetoothAdvertisement() { var myIpAddress = GetMyIPAddress(); OutputBluetoothConnectionMessage("Broadcasting from " + myIpAddress); this._connectionMadeTask = new TaskCompletionSource <bool>(); this._socketListener = new StreamSocketListener(); await this._socketListener.BindEndpointAsync( new HostName(myIpAddress), "8000"); this._socketListener.ConnectionReceived += OnConnectionReceived; _publisher = new BluetoothLEAdvertisementPublisher(); var manufactureData = new BluetoothLEManufacturerData(); manufactureData.CompanyId = 0xFFFE; manufactureData.Data = WriteToBuffer( IPAddress.Parse(myIpAddress), 8000); _publisher.Advertisement.ManufacturerData.Add(manufactureData); _publisher.Start(); await Task.WhenAny(this._connectionMadeTask.Task, Task.Delay(-1)); }
/// <summary> /// Initializes the singleton application object. This is the first line of authored code /// executed, and as such is the logical equivalent of main() or WinMain(). /// </summary> public Scenario3_BackgroundWatcher() { this.InitializeComponent(); // Create and initialize a new trigger to configure it. trigger = new BluetoothLEAdvertisementWatcherTrigger(); // Configure the advertisement filter to look for the data advertised by the publisher in Scenario 2 or 4. // You need to run Scenario 2 on another Windows platform within proximity of this one for Scenario 3 to // take effect. // Unlike the APIs in Scenario 1 which operate in the foreground. This API allows the developer to register a background // task to process advertisement packets in the background. It has more restrictions on valid filter configuration. // For example, exactly one single matching filter condition is allowed (no more or less) and the sampling interval // For determining the filter restrictions programatically across APIs, use the following properties: // MinSamplingInterval, MaxSamplingInterval, MinOutOfRangeTimeout, MaxOutOfRangeTimeout // Part 1A: Configuring the advertisement filter to watch for a particular advertisement payload // First, let create a manufacturer data section we wanted to match for. These are the same as the one // created in Scenario 2 and 4. Note that in the background only a single filter pattern is allowed per trigger. var manufacturerData = new BluetoothLEManufacturerData(); // Then, set the company ID for the manufacturer data. Here we picked an unused value: 0xFFFE manufacturerData.CompanyId = 0xFFFE; // Finally set the data payload within the manufacturer-specific section // Here, use a 16-bit UUID: 0x1234 -> {0x34, 0x12} (little-endian) DataWriter writer = new DataWriter(); writer.WriteUInt16(0x1234); // Make sure that the buffer length can fit within an advertisement payload. Otherwise you will get an exception. manufacturerData.Data = writer.DetachBuffer(); // Add the manufacturer data to the advertisement filter on the trigger: trigger.AdvertisementFilter.Advertisement.ManufacturerData.Add(manufacturerData); // Part 1B: Configuring the signal strength filter for proximity scenarios // Configure the signal strength filter to only propagate events when in-range // Please adjust these values if you cannot receive any advertisement // Set the in-range threshold to -70dBm. This means advertisements with RSSI >= -70dBm // will start to be considered "in-range". trigger.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" trigger.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" trigger.SignalStrengthFilter.OutOfRangeTimeout = TimeSpan.FromMilliseconds(2000); // By default, the sampling interval is set to be disabled, or the maximum sampling interval supported. // The sampling interval set to MaxSamplingInterval indicates that the event will only trigger once after it comes into range. // Here, set the sampling period to 1 second, which is the minimum supported for background. trigger.SignalStrengthFilter.SamplingInterval = TimeSpan.FromMilliseconds(1000); }
private async Task <bool> SendCustomizedMessage(int msec, string message) { BluetoothLEAdvertisementPublisher publisher = new BluetoothLEAdvertisementPublisher(); var manufacturerData = new BluetoothLEManufacturerData(); // Then, set the company ID for the manufacturer data. Here we picked an unused value: 0xFFFE manufacturerData.CompanyId = 0xFFFF; // 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(); //UInt16 uuidData = 0x1234; //writer.WriteUInt16(uuidData); string deviceData = message; writer.UnicodeEncoding = UnicodeEncoding.Utf8; writer.WriteString(deviceData); // Make sure that the buffer length can fit within an advertisement payload. Otherwise you will get an exception. manufacturerData.Data = writer.DetachBuffer(); // Add the manufacturer data to the advertisement publisher: publisher.Advertisement.ManufacturerData.Add(manufacturerData); publisher.Start(); await Task.Delay(msec); publisher.Stop(); return(true); }
public void Receive() { // Create and initialize a new watcher instance. watcher = new BluetoothLEAdvertisementWatcher(); var manufacturerData = new BluetoothLEManufacturerData(); manufacturerData.CompanyId = 0xFFFE; // 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.WriteUInt16(0x1234); // Make sure that the buffer length can fit within an advertisement payload. Otherwise you will get an exception. manufacturerData.Data = writer.DetachBuffer(); // Add the manufacturer data to the advertisement filter on the watcher: watcher.AdvertisementFilter.Advertisement.ManufacturerData.Add(manufacturerData); watcher.SignalStrengthFilter.InRangeThresholdInDBm = -70; watcher.SignalStrengthFilter.OutOfRangeThresholdInDBm = -75; watcher.SignalStrengthFilter.OutOfRangeTimeout = TimeSpan.FromMilliseconds(2000); watcher.Received += OnAdvertisementReceived; watcher.Start(); }
protected override void OnNavigatedTo(NavigationEventArgs e) { this.watcher = new BluetoothLEAdvertisementWatcher(); var manufacturerDataWriter = new DataWriter(); manufacturerDataWriter.WriteUInt16(0x1234); var manufacturerData = new BluetoothLEManufacturerData { CompanyId = 0xFFFE, Data = manufacturerDataWriter.DetachBuffer() }; //this.watcher.AdvertisementFilter.Advertisem.ManufacturerData.Add(manufacturerData); watcher.Received += _watcher_Received2; //To receive scan response advertisements as well, set the following after creating the watcher. Note that this will cause greater power drain and is not available while in background modes. //https://docs.microsoft.com/en-us/windows/uwp/devices-sensors/ble-beacon watcher.ScanningMode = BluetoothLEScanningMode.Active; //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); watcher.Start(); }
public void PublishInfo() { var manufacturerData = new BluetoothLEManufacturerData(); manufacturerData.CompanyId = 0xFFFE; // 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(); UInt16 uuidData = 0x1234; writer.WriteUInt16(uuidData); writer.WriteString("HeyDude"); manufacturerData.Data = writer.DetachBuffer(); publisher.Advertisement.ManufacturerData.Add(manufacturerData); // Display the information about the published payload myTextBox.Text = string.Format("Published payload information: CompanyId=0x{0}, ManufacturerData=0x{1}", manufacturerData.CompanyId.ToString("X"), uuidData.ToString("X")); // Display the current status of the publisher tbStatus.Text = string.Format("Published Status: {0}, Error: {1}", publisher.Status, BluetoothError.Success); publisher.Start(); }
public static void iBeaconSetAdvertisement(this BluetoothLEAdvertisement Advertisment, iBeaconData data) { BluetoothLEManufacturerData manufacturerData = new BluetoothLEManufacturerData(); // Set Apple as the manufacturer data manufacturerData.CompanyId = 76; var writer = new DataWriter(); writer.WriteUInt16(0x0215); //bytes 0 and 1 of the iBeacon advertisment indicator //if (data!=null& data.UUID!= Guid.Empty) //{ // //If UUID is null scanning for all iBeacons // writer.WriteBytes( data.UUID.ToByteArray()); // if (data.Major!=0) // { // //If Major not null searching with UUID and Major // writer.WriteBytes(BitConverter.GetBytes(data.Major).Reverse().ToArray()); // if (data.Minor != 0) // { // //If Minor not null we are looking for a specific beacon not a class of beacons // writer.WriteBytes(BitConverter.GetBytes(data.Minor).Reverse().ToArray()); // if (data.TxPower != 0) // writer.WriteBytes(BitConverter.GetBytes(data.TxPower)); // } // } //} manufacturerData.Data = writer.DetachBuffer(); Advertisment.ManufacturerData.Clear(); Advertisment.ManufacturerData.Add(manufacturerData); }
public Task StartAdvertising(AdvertisementData adData = null) { this.publisher.Advertisement.Flags = BluetoothLEAdvertisementFlags.ClassicNotSupported; this.publisher.Advertisement.ManufacturerData.Clear(); this.publisher.Advertisement.ServiceUuids.Clear(); if (adData?.ManufacturerData != null) { using (var writer = new DataWriter()) { writer.WriteBytes(adData.ManufacturerData.Data); var md = new BluetoothLEManufacturerData(adData.ManufacturerData.CompanyId, writer.DetachBuffer()); this.publisher.Advertisement.ManufacturerData.Add(md); } } if (adData?.ServiceUuids != null) { foreach (var serviceUuid in adData.ServiceUuids) { this.publisher.Advertisement.ServiceUuids.Add(serviceUuid); } } this.publisher.Start(); return(Task.CompletedTask); }
public Task StartAdvertising(AdvertisementOptions?options = null) { options ??= new AdvertisementOptions(); if (!options.LocalName.IsEmpty()) { this.publisher.Advertisement.LocalName = options.LocalName; } this.publisher.Advertisement.Flags = BluetoothLEAdvertisementFlags.ClassicNotSupported; this.publisher.Advertisement.ManufacturerData.Clear(); this.publisher.Advertisement.ServiceUuids.Clear(); if (options.ManufacturerData != null) { using (var writer = new DataWriter()) { writer.WriteBytes(options.ManufacturerData.Data); var md = new BluetoothLEManufacturerData(options.ManufacturerData.CompanyId, writer.DetachBuffer()); this.publisher.Advertisement.ManufacturerData.Add(md); } } var serviceUuids = options.UseGattServiceUuids ? this.services.Keys.ToList() : options.ServiceUuids; foreach (var serviceUuid in serviceUuids) { this.publisher.Advertisement.ServiceUuids.Add(Guid.Parse(serviceUuid)); } this.publisher.Start(); return(Task.CompletedTask); }
public static void iBeaconSetAdvertisement(this BluetoothLEAdvertisement Advertisment, iBeaconData data) { BluetoothLEManufacturerData manufacturerData = new BluetoothLEManufacturerData(); // Set Apple as the manufacturer data manufacturerData.CompanyId = 76; var writer = new DataWriter(); writer.WriteUInt16(0x0215); //bytes 0 and 1 of the iBeacon advertisment indicator if (data!=null& data.UUID!= Guid.Empty) { //If UUID is null scanning for all iBeacons writer.WriteBytes( data.UUID.ToByteArray()); if (data.Major!=0) { //If Major not null searching with UUID and Major writer.WriteBytes(BitConverter.GetBytes(data.Major).Reverse().ToArray()); if (data.Minor != 0) { //If Minor not null we are looking for a specific beacon not a class of beacons writer.WriteBytes(BitConverter.GetBytes(data.Minor).Reverse().ToArray()); if (data.TxPower != 0) writer.WriteBytes(BitConverter.GetBytes(data.TxPower)); } } } manufacturerData.Data = writer.DetachBuffer(); Advertisment.ManufacturerData.Clear(); Advertisment.ManufacturerData.Add(manufacturerData); }
public Section(BluetoothLEManufacturerData section) { CompanyId = section.CompanyId; Buffer = new byte[section.Data.Length]; using (var reader = DataReader.FromBuffer(section.Data)) { reader.ReadBytes(Buffer); } }
public void StartWatchingAdvertisement() { BluetoothLEManufacturerData manufacturerData = new BluetoothLEManufacturerData(); manufacturerData.CompanyId = 0xFFFF; watcher.AdvertisementFilter.Advertisement.ManufacturerData.Add(manufacturerData); watcher.Received += Watcher_Received; watcher.Start(); }
private void PublishiBeacon(byte[] dataArray) { var manufactureData = new BluetoothLEManufacturerData(); //0x004C Apple, Inc. manufactureData.CompanyId = 0x004c; manufactureData.Data = dataArray.AsBuffer(); _blePublisher.Advertisement.ManufacturerData.Add(manufactureData); //開始發佈 _blePublisher.Start(); }
/// <summary> /// Creates a BluetoothLEManufacturerData instance based on the given manufacturer ID and /// beacon code. The returned instance can be used as a filter for a BLE advertisement /// watcher. /// </summary> /// <param name="manufacturerId">The manufacturer ID.</param> /// <param name="beaconCode">The beacon code.</param> /// <returns>BluetoothLEManufacturerData instance based on given arguments.</returns> public static BluetoothLEManufacturerData BeaconManufacturerData(UInt16 manufacturerId, UInt16 beaconCode) { BluetoothLEManufacturerData manufacturerData = new BluetoothLEManufacturerData(); manufacturerData.CompanyId = manufacturerId; DataWriter writer = new DataWriter(); writer.WriteUInt16(beaconCode); manufacturerData.Data = writer.DetachBuffer(); return(manufacturerData); }
public BLEManufacturerData(BluetoothLEManufacturerData args) { var data = new byte[args.Data.Length]; using (var reader = DataReader.FromBuffer(args.Data)) { reader.ReadBytes(data); } this.Data = string.Format("{0}", BitConverter.ToString(data)); this.CompanyId = args.CompanyId; }
/// <summary> /// Initializes the singleton application object. This is the first line of authored code /// executed, and as such is the logical equivalent of main() or WinMain(). /// </summary> public Scenario5_AggregatingWatcher() { InitializeComponent(); // Create and initialize a new trigger to configure it. trigger = new BluetoothLEAdvertisementWatcherTrigger(); var manufacturerData = new BluetoothLEManufacturerData { CompanyId = 76 }; trigger.AdvertisementFilter.Advertisement.ManufacturerData.Add(manufacturerData); _tracker = new Tracker(); }
public void Start() { // Create and initialize a new publisher instance. this._publisher = new BluetoothLEAdvertisementPublisher(); // Attach a event handler to monitor the status of the publisher, which // can tell us whether the advertising has been serviced or is waiting to be serviced // due to lack of resources. It will also inform us of unexpected errors such as the Bluetooth // radio being turned off by the user. this._publisher.StatusChanged += OnPublisherStatusChanged; // We need to add some payload to the advertisement. A publisher without any payload // or with invalid ones cannot be started. We only need to configure the payload once // for any publisher. // Add a manufacturer-specific section: // First, let create a manufacturer data section var manufacturerData = new BluetoothLEManufacturerData(); // Then, set the company ID for the manufacturer data. Here we picked an unused value: 0xFFFE manufacturerData.CompanyId = ManufacturerId; // 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(); UInt16 uuidData = 0x1234; writer.WriteUInt16(uuidData); // Make sure that the buffer length can fit within an advertisement payload. Otherwise you will get an exception. manufacturerData.Data = writer.DetachBuffer(); // Add the manufacturer data to the advertisement publisher: this._publisher.Advertisement.ManufacturerData.Add(manufacturerData); // From old code (Which is not commented) var data = new BluetoothLEAdvertisementDataSection { Data = SolicitationData.AsBuffer() }; this._publisher.Advertisement.DataSections.Add(data); try { // Calling publisher start will start the advertising if resources are available to do so this._publisher.Start(); } catch (Exception ex) { throw ex; } }
private void PublishiBeacon(byte[] dataArray) { var manufactureData = new BluetoothLEManufacturerData(); //0x004C Apple, Inc. manufactureData.CompanyId = 0x004c; //using System.Runtime.InteropServices.WindowsRuntime; manufactureData.Data = dataArray.AsBuffer(); _blePublisher = new BluetoothLEAdvertisementPublisher(); _blePublisher.Advertisement.ManufacturerData.Add(manufactureData); //開始發佈 _blePublisher.Start(); }
public MainPage() { this.InitializeComponent(); imageChocola.Visibility = Visibility.Collapsed; watcher = new BluetoothLEAdvertisementWatcher(); var manufacturerData = new BluetoothLEManufacturerData(); manufacturerData.CompanyId = 0xFFFE; watcher.AdvertisementFilter.Advertisement.ManufacturerData.Add(manufacturerData); watcher.SignalStrengthFilter.SamplingInterval = TimeSpan.FromMilliseconds(2000); watcher.Received += OnAdvertisementReceived; watcher.Start(); }
/// <summary> /// Constructs the collection for beacons that we detect and the BLE beacon advertisement /// watcher. /// /// Hooks the events of the watcher and sets the watcher filter based on /// the manufacturer ID and beacon code. /// </summary> private void InitializeScanner() { BeaconCollection = new ObservableCollection <Beacon>(); _bluetoothLEAdvertisemenetWatcher = new BluetoothLEAdvertisementWatcher(); _bluetoothLEAdvertisemenetWatcher.Stopped += OnWatcherStoppedAsync; _bluetoothLEAdvertisemenetWatcher.Received += OnAdvertisemenetReceivedAsync; BluetoothLEManufacturerData manufacturerData = BeaconFactory.BeaconManufacturerData(ManufacturerId, BeaconCode); _bluetoothLEAdvertisemenetWatcher.AdvertisementFilter.Advertisement.ManufacturerData.Add(manufacturerData); }
public void Emit() { publisher = new BluetoothLEAdvertisementPublisher(); var manufacturerData = new BluetoothLEManufacturerData(); manufacturerData.CompanyId = 0xFFFE; var writer = new DataWriter(); UInt16 uuidData = 0x1234; writer.WriteUInt16(uuidData); manufacturerData.Data = writer.DetachBuffer(); publisher.Advertisement.ManufacturerData.Add(manufacturerData); publisher.Start(); }
public Advertiser() { var manufacturerData = new BluetoothLEManufacturerData(ManufacturerId, (new byte[] { 0x12, 0x34 }).AsBuffer()); var advertisment = new BluetoothLEAdvertisement(); var data = new BluetoothLEAdvertisementDataSection {Data = SolicitationData.AsBuffer()}; advertisment.DataSections.Add(data); advertisment.ManufacturerData.Add(manufacturerData); this._publisher = new BluetoothLEAdvertisementPublisher(advertisment); }
void Awake() { #if UNITY_UWP watcher = new BluetoothLEAdvertisementWatcher(); var manufacturerData = new BluetoothLEManufacturerData { CompanyId = BEACON_ID, }; watcher.AdvertisementFilter.Advertisement.ManufacturerData.Add(manufacturerData); watcher.Received += Watcher_Received; watcher.Start(); #endif }
void Awake() { decoder = GameObject.Find("GameManager").GetComponent <Decoder>(); #if ENABLE_WINMD_SUPPORT watcher = new BluetoothLEAdvertisementWatcher(); var manufacturerData = new BluetoothLEManufacturerData { CompanyId = BEACON_ID }; watcher.AdvertisementFilter.Advertisement.ManufacturerData.Add(manufacturerData); watcher.Received += Watcher_Received; watcher.Start(); #endif }
public Advertiser() { var manufacturerData = new BluetoothLEManufacturerData(ManufacturerId, (new byte[] { 0x12, 0x34 }).AsBuffer()); var advertisment = new BluetoothLEAdvertisement(); var data = new BluetoothLEAdvertisementDataSection { Data = SolicitationData.AsBuffer() }; advertisment.DataSections.Add(data); advertisment.ManufacturerData.Add(manufacturerData); this._publisher = new BluetoothLEAdvertisementPublisher(advertisment); }
private void Scan_Click(object sender, RoutedEventArgs e) { if (watcher != null) { watcher.Stop(); } watcher = new BluetoothLEAdvertisementWatcher(); var manufacturerData = new BluetoothLEManufacturerData { CompanyId = CUSTOM_ID }; watcher.AdvertisementFilter.Advertisement.ManufacturerData.Add(manufacturerData); watcher.Received += Watcher_Received; watcher.Start(); }
void Awake() { eventProcessor = GameObject.FindObjectOfType <EventProcessor>(); #if NETFX_CORE watcher = new BluetoothLEAdvertisementWatcher(); var manufacturerData = new BluetoothLEManufacturerData { CompanyId = BEACON_ID }; watcher.AdvertisementFilter.Advertisement.ManufacturerData.Add(manufacturerData); watcher.Received += Watcher_Received; watcher.Start(); #endif }
private void initWatcher() { watcher = new BluetoothLEAdvertisementWatcher(); // let's set up filters so we only watch for specific beacons var manufacturerData = new BluetoothLEManufacturerData(); manufacturerData.CompanyId = COMPANY_ID; watcher.AdvertisementFilter.Advertisement.ManufacturerData.Add(manufacturerData); watcher.SignalStrengthFilter.InRangeThresholdInDBm = -50; watcher.SignalStrengthFilter.OutOfRangeThresholdInDBm = -80; watcher.SignalStrengthFilter.OutOfRangeTimeout = TimeSpan.FromMilliseconds(2000); }
/// <summary> /// Start to watch a bluetooth advertisement /// </summary> /// <returns></returns> public async Task SetupBluetoothWatcher() { this._connectionMadeTask = new TaskCompletionSource <bool>(); _watcher = new BluetoothLEAdvertisementWatcher(); _watcher.Received += BluetoothWatcher_Connected; var manufacturerData = new BluetoothLEManufacturerData(); manufacturerData.CompanyId = 0xFFFE; _watcher.AdvertisementFilter.Advertisement.ManufacturerData.Add(manufacturerData); _watcher.Start(); await Task.WhenAny(this._connectionMadeTask.Task, Task.Delay(-1)); }
//--------------------------------------------------------------------- // Init and ShutDown //--------------------------------------------------------------------- // Initializes the publisher and begins publishing advertisements public void Start() { // // Step 1 // Create the publisher // publisher = new BluetoothLEAdvertisementPublisher(); // // Step 2 // Add a payload to the advertisement. It must be less than 20 // bytes or an exception will occur. // BluetoothLEManufacturerData manufacturerData = new BluetoothLEManufacturerData(); // Add a manufacturer ID manufacturerData.CompanyId = 0xDEDE; // Another reference to King DeDeDe // Add a string DataWriter writer = new DataWriter(); writer.WriteString("IPv6ToBle"); manufacturerData.Data = writer.DetachBuffer(); // Add the manufacturer data to the publisher publisher.Advertisement.ManufacturerData.Add(manufacturerData); // Register a status changed handler in case something happens to // the publisher publisher.StatusChanged += OnPublisherStatusChanged; // Start the publisher if resources are available to do so. It may // take a second to get started. This cannot run if other code is // using the Bluetooth radio, such as the GATT server. publisher.Start(); if (publisher.Status == BluetoothLEAdvertisementPublisherStatus.Started) { Debug.WriteLine("Packet receiver advertisement publisher started."); } else { Debug.WriteLine("An error occurred when starting the advertisement" + "publisher. Status: " + publisher.Status.ToString() ); } }
/// <summary> /// Initializes the singleton application object. This is the first line of authored code /// executed, and as such is the logical equivalent of main() or WinMain(). /// </summary> public Scenario2_Publisher() { this.InitializeComponent(); // Create and initialize a new publisher instance. publisher = new BluetoothLEAdvertisementPublisher(); // We need to add some payload to the advertisement. A publisher without any payload // or with invalid ones cannot be started. We only need to configure the payload once // for any publisher. // Add a manufacturer-specific section: // First, let create a manufacturer data section var manufacturerData = new BluetoothLEManufacturerData(); // Then, set the company ID for the manufacturer data. Here we picked an unused value: 0xFFFE manufacturerData.CompanyId = 0x004C; // 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(); //UInt16 uuidData = 0x1234; //writer.WriteUInt16(uuidData); writer.WriteBytes(BleData.ReceivedData); // Make sure that the buffer length can fit within an advertisement payload. Otherwise you will get an exception. manufacturerData.Data = writer.DetachBuffer(); // Add the manufacturer data to the advertisement publisher: publisher.Advertisement.ManufacturerData.Add(manufacturerData); // Display the information about the published payload PublisherPayloadBlock.Text = string.Format("Published payload information: CompanyId=0x{0}, ManufacturerData=0x{1}", manufacturerData.CompanyId.ToString("X"), BitConverter.ToString(BleData.ReceivedData)); // Display the current status of the publisher PublisherStatusBlock.Text = string.Format("Published Status: {0}, Error: {1}", publisher.Status, BluetoothError.Success); }
/// <summary> /// Initializes the singleton application object. This is the first line of authored code /// executed, and as such is the logical equivalent of main() or WinMain(). /// </summary> public Scenario4_BackgroundPublisher() { this.InitializeComponent(); // Create and initialize a new trigger to configure it. trigger = new BluetoothLEAdvertisementPublisherTrigger(); // We need to add some payload to the advertisement. A publisher without any payload // or with invalid ones cannot be started. We only need to configure the payload once // for any publisher. // Add a manufacturer-specific section: // First, create a manufacturer data section var manufacturerData = new BluetoothLEManufacturerData(); // Then, set the company ID for the manufacturer data. Here we picked an unused value: 0xFFFE manufacturerData.CompanyId = 0xFFFE; // 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(); UInt16 uuidData = 0x1234; writer.WriteUInt16(uuidData); // Make sure that the buffer length can fit within an advertisement payload. Otherwise you will get an exception. manufacturerData.Data = writer.DetachBuffer(); // Add the manufacturer data to the advertisement publisher: trigger.Advertisement.ManufacturerData.Add(manufacturerData); // Display the information about the published payload PublisherPayloadBlock.Text = string.Format("Published payload information: CompanyId=0x{0}, ManufacturerData=0x{1}", manufacturerData.CompanyId.ToString("X"), uuidData.ToString("X")); // Reset the displayed status of the publisher PublisherStatusBlock.Text = ""; }
private void BeaconWatchButton_Click(object sender, RoutedEventArgs e) { _watcher = new BluetoothLEAdvertisementWatcher(); // Manufacturer specific data to customize var writer = new DataWriter(); const ushort uuidData = 0x1234; writer.WriteUInt16(uuidData); var manufacturerData = new BluetoothLEManufacturerData { CompanyId = 0xFFFE, Data = writer.DetachBuffer() }; _watcher.AdvertisementFilter.Advertisement.ManufacturerData.Add(manufacturerData); // Start watching _watcher.Received += WatcherOnReceived; _watcher.Start(); SetStatusOutput("Watching for Bluetooth Beacons"); }
private void initPublisher() { publisher = new BluetoothLEAdvertisementPublisher(); var manufacturerData = new BluetoothLEManufacturerData(); manufacturerData.CompanyId = COMPANY_ID; var writer = new DataWriter(); UInt16 uuidData = 0x0001; writer.WriteUInt16(uuidData); manufacturerData.Data = writer.DetachBuffer(); publisher.Advertisement.ManufacturerData.Add(manufacturerData); }
/// <summary> /// Registers the Bluetooth LE watcher background task, assuming Bluetooth is available. /// </summary> private async Task EnableWatcherAsync() { if (_taskRegistration != null) { return; } _trigger = new BluetoothLEAdvertisementWatcherTrigger(); // Add manufacturer data. var manufacturerData = new BluetoothLEManufacturerData(); manufacturerData.CompanyId = 0xFFFE; DataWriter writer = new DataWriter(); writer.WriteUInt16(0x1234); manufacturerData.Data = writer.DetachBuffer(); _trigger.AdvertisementFilter.Advertisement.ManufacturerData.Add(manufacturerData); // Add signal strength filters and sampling interval. _trigger.SignalStrengthFilter.InRangeThresholdInDBm = -65; _trigger.SignalStrengthFilter.OutOfRangeThresholdInDBm = -70; _trigger.SignalStrengthFilter.OutOfRangeTimeout = TimeSpan.FromSeconds(2); _trigger.SignalStrengthFilter.SamplingInterval = TimeSpan.FromSeconds(1); // Create the task. BackgroundAccessStatus backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync(); var builder = new BackgroundTaskBuilder() { Name = _taskName, TaskEntryPoint = "BackgroundTasks.AdvertisementWatcherTask" }; builder.SetTrigger(_trigger); builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable)); _taskRegistration = builder.Register(); }
public MainPage() { this.InitializeComponent(); Current = this; localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; items = new List<List_item>(); my_info.id = localSettings.Values["id"].ToString(); // Create and initialize a new watcher instance. watcher = new BluetoothLEAdvertisementWatcher(); // Create and initialize a new trigger to configure it. trigger = new BluetoothLEAdvertisementPublisherTrigger(); // Add a manufacturer-specific section: // First, create a manufacturer data section var manufacturerData_publisher = new BluetoothLEManufacturerData(); var manufacturerData_watcher = new BluetoothLEManufacturerData(); // Then, set the company ID for the manufacturer data. Here we picked an unused value: 0xFF00 manufacturerData_publisher.CompanyId = 0xFFAA; manufacturerData_watcher.CompanyId = 0xFFAA; // 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(); string id = my_info.id; writer.WriteString(id); // Make sure that the buffer length can fit within an advertisement payload. Otherwise you will get an exception. manufacturerData_publisher.Data = writer.DetachBuffer(); //(publisher) // Add the manufacturer data to the advertisement publisher: trigger.Advertisement.ManufacturerData.Add(manufacturerData_publisher); // 여기 필터추가 watcher.AdvertisementFilter.Advertisement.ManufacturerData.Add(manufacturerData_watcher); // Configure the signal strength filter to only propagate events when in-range // Please adjust these values if you cannot receive any advertisement // 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(1000); // By default, the sampling interval is set to zero, which means there is no sampling and all // the advertisement received is returned in the Received event // End of watcher configuration. There is no need to comment out any code beyond this point. }
/// <summary> /// Starts the watcher and hooks its events to callbacks. /// </summary> /// <param name="manufacturerId">The manufacturer ID.</param> /// <param name="beaconCode">The beacon code.</param> /// <param name="beaconExitTimeoutInMiliseconds">Time in miliseconds after beacon will be trated as lost.</param> /// <param name="rssiEnterThreshold">Optional rssi threshold which will trigger beacon discover event. Value must be between -128 and 127.</param> /// <param name="enterDistanceThreshold">Optional minimal distance in meters that will trigger beacon discover event.</param> public void StartWatcher(ushort manufacturerId, ushort beaconCode, ulong beaconExitTimeoutInMiliseconds, short? rssiEnterThreshold = null, ulong? enterDistanceThreshold = null) { _beaconExitTimeout = beaconExitTimeoutInMiliseconds; _enterDistanceThreshold = enterDistanceThreshold; _beaconExitTimeout = 30000; if (_beaconExitTimeout < 1000) { _beaconExitTimeout = 1000; } if (Status != ScannerStatus.Started) { if (_bluetoothLeAdvertisementWatcher == null) { _bluetoothLeManufacturerData = BeaconFactory.BeaconManufacturerData(manufacturerId, beaconCode); _bluetoothLeAdvertisementWatcher = new BluetoothLEAdvertisementWatcher(); if (rssiEnterThreshold != null && rssiEnterThreshold.Value >= -128 && rssiEnterThreshold.Value <= 127) { _bluetoothLeAdvertisementWatcher.SignalStrengthFilter = new BluetoothSignalStrengthFilter() { InRangeThresholdInDBm = rssiEnterThreshold.Value }; } _bluetoothLeAdvertisementWatcher.AdvertisementFilter.Advertisement.ManufacturerData.Add(_bluetoothLeManufacturerData); _bluetoothLeAdvertisementWatcher.SignalStrengthFilter.SamplingInterval = TimeSpan.FromMilliseconds(0); _bluetoothLeAdvertisementWatcher.SignalStrengthFilter.OutOfRangeTimeout = TimeSpan.FromMilliseconds(_beaconExitTimeout); _bluetoothLeAdvertisementWatcher.ScanningMode = BluetoothLEScanningMode.Active; } _bluetoothLeAdvertisementWatcher.Received += OnAdvertisementReceived; _bluetoothLeAdvertisementWatcher.Stopped += OnWatcherStopped; ServiceManager.LayoutManager?.VerifyLayoutAsync(); _bluetoothLeAdvertisementWatcher.Start(); Status = ScannerStatus.Started; Logger.Debug("Scanner.StartWatcher(): Watcher started"); } }
public void SetAdvertisingPayload(Beacon.BeaconTypeEnum protocol) { if (protocol == Beacon.BeaconTypeEnum.iBeacon) { var writer = new DataWriter(); var payload = new byte[] { 0x02, 0x15, 0x02, 0x15, 0x0A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xC2 }; foreach (byte b in payload) writer.WriteByte(b); var manufacturerData = new BluetoothLEManufacturerData(); manufacturerData.CompanyId = 0x004C; // iBeacon manufacturerData.Data = writer.DetachBuffer(); _publisher.Advertisement.ManufacturerData.Clear(); _publisher.Advertisement.ManufacturerData.Add(manufacturerData); } }
private void BeaconPublishButton_Click(object sender, RoutedEventArgs e) { _publisher = new BluetoothLEAdvertisementPublisher(); // Manufacturer specific data to customize var writer = new DataWriter(); const ushort uuidData = 0x1234; // Custom payload writer.WriteUInt16(uuidData); var manufacturerData = new BluetoothLEManufacturerData { CompanyId = 0xFFFE, // Custom manufacturer Data = writer.DetachBuffer() }; _publisher.Advertisement.ManufacturerData.Add(manufacturerData); // Start publishing _publisher.Start(); SetStatusOutput("Publishing Bluetooth Beacon"); }
/// <summary> /// Creates a BluetoothLEManufacturerData instance based on the given manufacturer ID and /// beacon code. The returned instance can be used as a filter for a BLE advertisement /// watcher. /// </summary> /// <param name="manufacturerId">The manufacturer ID.</param> /// <param name="beaconCode">The beacon code.</param> /// <returns>BluetoothLEManufacturerData instance based on given arguments.</returns> public static BluetoothLEManufacturerData BeaconManufacturerData(ushort manufacturerId, ushort beaconCode) { BluetoothLEManufacturerData manufacturerData = new BluetoothLEManufacturerData(); manufacturerData.CompanyId = manufacturerId; DataWriter writer = new DataWriter(); writer.WriteUInt16(beaconCode); manufacturerData.Data = writer.DetachBuffer(); return manufacturerData; }
/// <summary> /// Initializes the singleton application object. This is the first line of authored code /// executed, and as such is the logical equivalent of main() or WinMain(). /// </summary> public Scenario1_Watcher() { this.InitializeComponent(); // Create and initialize a new watcher instance. watcher = new BluetoothLEAdvertisementWatcher(); // Begin of watcher configuration. Configure the advertisement filter to look for the data advertised by the publisher // in Scenario 2 or 4. You need to run Scenario 2 on another Windows platform within proximity of this one for Scenario 1 to // take effect. The APIs shown in this Scenario are designed to operate only if the App is in the foreground. For background // watcher operation, please refer to Scenario 3. // Please comment out this following section (watcher configuration) if you want to remove all filters. By not specifying // any filters, all advertisements received will be notified to the App through the event handler. You should comment out the following // section if you do not have another Windows platform to run Scenario 2 alongside Scenario 1 or if you want to scan for // all LE advertisements around you. // For determining the filter restrictions programatically across APIs, use the following properties: // MinSamplingInterval, MaxSamplingInterval, MinOutOfRangeTimeout, MaxOutOfRangeTimeout // Part 1A: Configuring the advertisement filter to watch for a particular advertisement payload // First, let create a manufacturer data section we wanted to match for. These are the same as the one // created in Scenario 2 and 4. var manufacturerData = new BluetoothLEManufacturerData(); // Then, set the company ID for the manufacturer data. Here we picked an unused value: 0xFFFE manufacturerData.CompanyId = 0xFFFE; // 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.WriteUInt16(0x1234); // Make sure that the buffer length can fit within an advertisement payload. Otherwise you will get an exception. manufacturerData.Data = writer.DetachBuffer(); // Add the manufacturer data to the advertisement filter on the watcher: watcher.AdvertisementFilter.Advertisement.ManufacturerData.Add(manufacturerData); // Part 1B: Configuring the signal strength filter for proximity scenarios // Configure the signal strength filter to only propagate events when in-range // Please adjust these values if you cannot receive any advertisement // 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); // By default, the sampling interval is set to zero, which means there is no sampling and all // the advertisement received is returned in the Received event // End of watcher configuration. There is no need to comment out any code beyond this point. }