bool IsRepeatAdvertisement(BluetoothLEStreamSocketAdvertisement advertisement)
 {
     return(
         this.uniqueAdvertisements.Exists(
             e => ((e.Port == advertisement.Port) &&
                   (e.Address.ToString() == advertisement.Address.ToString()))));
 }
        void OnBluetoothAdvertisementSpotted(
            BluetoothLEAdvertisementWatcher sender,
            BluetoothLEAdvertisementReceivedEventArgs args)
        {
            foreach (var item in args.Advertisement.GetManufacturerDataByCompanyId(
                         BluetoothLEStreamSocketAdvertisement.MS_BLUETOOTH_LE_ID))
            {
                var advertisement = BluetoothLEStreamSocketAdvertisement.ReadFromBuffer(item.Data);

                if (advertisement != null)
                {
                    if ((advertisement.Address.ToString() != this.localAddress.ToString()) &&
                        !IsRepeatAdvertisement(advertisement))
                    {
                        this.StreamSocketDiscovered?.Invoke(this,
                                                            new BluetoothLEStreamSocketDiscoveredEventArgs()
                        {
                            Advertisement = advertisement
                        });

                        this.uniqueAdvertisements.Add(advertisement);
                    }
                }
            }
        }
        public static BluetoothLEStreamSocketAdvertisement ReadFromBuffer(IBuffer buffer)
        {
            BluetoothLEStreamSocketAdvertisement returnValue = null;

            var dataReader = DataReader.FromBuffer(buffer);

            if (dataReader.UnconsumedBufferLength >= Marshal.SizeOf <UInt32>())
            {
                var magicHeader = dataReader.ReadUInt32();

                if (magicHeader == MAGIC_HEADER)
                {
                    returnValue = new BluetoothLEStreamSocketAdvertisement();

                    var bitLength = dataReader.ReadUInt16();
                    var bits      = new byte[bitLength];
                    dataReader.ReadBytes(bits);
                    returnValue.Address = new IPAddress(bits);
                    returnValue.Port    = dataReader.ReadUInt16();
                }
            }
            return(returnValue);
        }
Beispiel #4
0
        public async Task StartAsync()
        {
            if (this.address == null)
            {
                await this.CreateForLocalInternetProfileAsync();
            }
            this.SocketListener.ConnectionReceived += OnConnectionReceived;

            // Advertise it over Bluetooth LE.
            var advert = new BluetoothLEStreamSocketAdvertisement(
                address,
                ushort.Parse(this.SocketListener.Information.LocalPort));

            var advertBuffer = advert.WriteToBuffer();

            var manufacturerData = new BluetoothLEManufacturerData(
                BluetoothLEStreamSocketAdvertisement.MS_BLUETOOTH_LE_ID, advertBuffer);

            this.bluetoothPublisher = new BluetoothLEAdvertisementPublisher();

            this.bluetoothPublisher.Advertisement.ManufacturerData.Add(manufacturerData);

            this.bluetoothPublisher.Start();
        }