Exemple #1
0
 public GattServer()
 {
     this.manager     = (BluetoothManager)Application.Context.GetSystemService(Context.BluetoothService);
     this.adCallbacks = new AdvertisementCallbacks();
     this.context     = new GattContext();
     this.runningSubj = new Subject <bool>();
 }
Exemple #2
0
        public override void Start(AdvertisementData adData)
        {
            if (!CrossBleAdapter.AndroidConfiguration.IsServerSupported)
            {
                throw new BleException("BLE Advertiser needs API Level 23+");
            }

            this.manager     = (BluetoothManager)Application.Context.GetSystemService(Context.BluetoothService);
            this.adCallbacks = new AdvertisementCallbacks();

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

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

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

            foreach (var serviceUuid in adData.ServiceUuids)
            {
                data.AddServiceUuid(serviceUuid.ToParcelUuid());
            }

            if (string.IsNullOrEmpty(adData.LocalName) || adData.AndroidUseDeviceName)
            {
                this.manager
                .Adapter
                .BluetoothLeAdvertiser
                .StartAdvertising(
                    settings.Build(),
                    data.Build(),
                    this.adCallbacks
                    );
            }
            else
            {
                this.manager
                .Adapter.SetName(adData.LocalName);
                var scanResponse = new AdvertiseData.Builder()
                                   .SetIncludeDeviceName(true);
                this.manager
                .Adapter
                .BluetoothLeAdvertiser
                .StartAdvertising(
                    settings.Build(),
                    data.Build(),
                    scanResponse.Build(),
                    this.adCallbacks
                    );
            }

            base.Start(adData);
        }
Exemple #3
0
 public void StopAdvertising()
 {
     this.context
     .Manager
     .Adapter
     .BluetoothLeAdvertiser
     .StopAdvertising(this.adCallbacks);
     this.adCallbacks = null;
 }
Exemple #4
0
 public Advertiser()
 {
     this.manager     = (BluetoothManager)Application.Context.GetSystemService(Context.BluetoothService);
     this.adCallbacks = new AdvertisementCallbacks
     {
         Failed  = e => IsStarted = false,
         Started = () => IsStarted = true
     };
 }
Exemple #5
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);
        }
Exemple #6
0
 public Advertiser()
 {
     this.manager     = (BluetoothManager)Application.Context.GetSystemService(Context.BluetoothService);
     this.adCallbacks = new AdvertisementCallbacks();
 }