WriteUInt16() public méthode

public WriteUInt16 ( ushort value ) : void
value ushort
Résultat void
Exemple #1
0
        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();
        }
        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);
        }
        /// <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);
        }
        /// <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 = 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:
            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"),
                uuidData.ToString("X"));

            // 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 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.
        }
 /// <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>
        /// 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();
        }
        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");
        }
        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");
        }
Exemple #10
0
        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();
        }
Exemple #11
0
        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();

        }
Exemple #12
0
        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);


        }