예제 #1
0
        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);
        }
예제 #2
0
        public async Task StartAdvertising(AdvertisementData adData = null)
        {
            if (this.manager.Advertising)
            {
                throw new ArgumentException("Advertising is already active");
            }

            await this.manager.WhenReady().ToTask();

            this.manager.StartAdvertising(new StartAdvertisingOptions
            {
                LocalName    = adData?.LocalName,
                ServicesUUID = adData?
                               .ServiceUuids?
                               .Select(x => x.ToCBUuid())
                               .ToArray()
            });
        }
예제 #3
0
        public Task StartAdvertising(AdvertisementData adData = null)
        {
            //            //if (!CrossBleAdapter.AndroidConfiguration.IsServerSupported)
            //            //    throw new BleException("BLE Advertiser needs API Level 23+");

            this.adCallbacks = new AdvertisementCallbacks();

            var settings = new AdvertiseSettings.Builder()
                           .SetAdvertiseMode(AdvertiseMode.Balanced)
                           .SetConnectable(true);

            var data = new AdvertiseData.Builder()
                       .SetIncludeDeviceName(true)
                       .SetIncludeTxPowerLevel(true);

            if (adData != null)
            {
                if (adData.ManufacturerData != null)
                {
                    data.AddManufacturerData(adData.ManufacturerData.CompanyId, adData.ManufacturerData.Data);
                }

                if (adData.ServiceUuids != null)
                {
                    foreach (var serviceUuid in adData.ServiceUuids)
                    {
                        data.AddServiceUuid(serviceUuid.ToParcelUuid());
                    }
                }
            }

            this.context
            .Manager
            .Adapter
            .BluetoothLeAdvertiser
            .StartAdvertising(
                settings.Build(),
                data.Build(),
                this.adCallbacks
                );

            return(Task.CompletedTask);
        }