private async Task <bool> RegisterBlAdvertisementTask() { try { var blAdvTrigger = new BluetoothLEAdvertisementPublisherTrigger(); DataWriter writer = new DataWriter(); ushort suiid = 0xFEED; writer.WriteBytes(BitConverter.GetBytes(suiid)); //UInt16 someValues = 0x1234; var advData = (ushort)DateTime.Now.Minute; writer.WriteUInt16(advData); var ds = blAdvTrigger.Advertisement.GetSectionsByType(BluetoothLEAdvertisementDataTypes.ServiceData16BitUuids).FirstOrDefault(); if (ds != null) { ds.Data = writer.DetachBuffer(); } else { blAdvTrigger.Advertisement.DataSections.Insert(0, new BluetoothLEAdvertisementDataSection(BluetoothLEAdvertisementDataTypes.ServiceData16BitUuids, writer.DetachBuffer())); } string BLAdvTaskName = "TaskMediaPlay.TileSongsTask"; string BLAdvBTaskEntryPoint = "TaskMediaPlay.TileSongsTask"; foreach (var task in BackgroundTaskRegistration.AllTasks) { if (task.Value.Name == BLAdvTaskName) { task.Value.Unregister(true); Debug.WriteLine($"{task.Value.Name} Unregistered"); } } var builder = new BackgroundTaskBuilder(); builder.Name = BLAdvTaskName; builder.TaskEntryPoint = BLAdvBTaskEntryPoint; builder.SetTrigger(blAdvTrigger); var result = builder.Register(); Debug.WriteLine("Adv Task registered with modified service data"); } catch (Exception ex) { Debug.WriteLine(ex.Message); return(false); } return(true); }
/// <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 = 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 = "Device050"; 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: 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")); deviceData); // Reset the displayed status of the publisher PublisherStatusBlock.Text = ""; }
/// <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 = ""; }
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. }