Exemple #1
0
        public override void Start(AdvertisementData adData)
        {
            var settings = new AdvertiseSettings.Builder()
                           .SetAdvertiseMode(AdvertiseMode.Balanced)
                           .SetConnectable(true);

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

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

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

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

            base.Start(adData);
        }
        public Task StartAdvertising(string localName, List <IService> services)
        {
            var settings = new AdvertiseSettings.Builder()
                           .SetAdvertiseMode(AdvertiseMode.LowLatency)
                           .SetTxPowerLevel(AdvertiseTx.PowerHigh)
                           .SetTimeout(0)
                           .SetConnectable(true)
                           .Build();

            if (localName != null)
            {
                _adapter.SetName(localName);
            }

            var advertiseDataBuilder = new AdvertiseData.Builder()
                                       .SetIncludeTxPowerLevel(false)
                                       .SetIncludeDeviceName(false);

            if (services != null)
            {
                foreach (Service service in services)
                {
                    var serviceUuid = service.Uuid;
                    var uuid        = UUID.FromString(serviceUuid);
                    var parcelUuid  = new ParcelUuid(uuid);
                    advertiseDataBuilder.AddServiceUuid(parcelUuid);
                }
            }
            var advertiseData = advertiseDataBuilder.Build();

            _adapter.BluetoothLeAdvertiser.StartAdvertising(settings, advertiseData, _advertiseCallback);
            return(Task.FromResult(true));
        }
Exemple #3
0
        public void StartAdvertising(string localName)
        {
            var settings = new AdvertiseSettings.Builder()
                           .SetAdvertiseMode(AdvertiseMode.Balanced)
                           .SetTxPowerLevel(AdvertiseTx.PowerHigh)
                           .SetConnectable(true)
                           .Build();


            _adapter.SetName(localName);

            var advertiseDataBuilder = new AdvertiseData.Builder()
                                       .SetIncludeDeviceName(true);

            foreach (var service in _server.Services)
            {
                var parcelUuid = new ParcelUuid(UUID.FromString("1234".ToGuid().ToString()));
                advertiseDataBuilder.AddServiceUuid(parcelUuid);
            }


            var advertiseData = advertiseDataBuilder.Build();

            _adapter.BluetoothLeAdvertiser.StartAdvertising(settings, advertiseData, _advertiseCallback);
        }
        protected AdvertiseData BuildAdvertiseData(TracingInformation tracingInformation)
        {
            AdvertiseData.Builder dataBuilder = new AdvertiseData.Builder();
            var uuid = ParcelUuid.FromString(tracingInformation.ServiceId);

            dataBuilder.AddServiceUuid(uuid);
            return(dataBuilder.Build());
        }
Exemple #5
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 #6
0
        public async Task StartAdvertising(AdvertisementOptions?options = null)
        {
            if (!this.context.Context.IsMinApiLevel(23))
            {
                throw new ApplicationException("BLE Advertiser needs API Level 23+");
            }

            options ??= new AdvertisementOptions();
            var tcs = new TaskCompletionSource <object>();

            this.adCallbacks = new AdvertisementCallbacks(
                () => tcs.SetResult(null),
                ex => tcs.SetException(ex)
                );

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

            var data = new AdvertiseData.Builder()
                       .SetIncludeDeviceName(options.AndroidIncludeDeviceName)
                       .SetIncludeTxPowerLevel(options.AndroidIncludeTxPower);

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

            var serviceUuids = options.UseGattServiceUuids
                ? this.services.Keys.ToList()
                : options.ServiceUuids;

            foreach (var uuid in serviceUuids)
            {
                data.AddServiceUuid(new Android.OS.ParcelUuid(UUID.FromString(uuid)));
            }

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

            await tcs.Task;
        }
Exemple #7
0
        public override void Start(AdvertisementData adData)
        {
            if (!CrossBleAdapter.AndroidConfiguration.IsServerSupported)
            {
                throw new BleException("BLE Advertiser needs API Level 21+");
            }

            if (this.IsStarted)
            {
                throw new BleException("BLE Advertiser is already started");
            }

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

            var data = new AdvertiseData.Builder()
                       .SetIncludeDeviceName(adData.AndroidIncludeDeviceName)
                       .SetIncludeTxPowerLevel(adData.AndroidIncludeTxPower);

            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))
            {
                data.SetIncludeDeviceName(true);
                this.manager.Adapter.SetName(adData.LocalName);
            }

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

            base.Start(adData);
        }
Exemple #8
0
        public Task StartAdvertising(AdvertisementData?adData = null)
        {
            if (!this.context.Context.IsMinApiLevel(23))
            {
                throw new ApplicationException("BLE Advertiser needs API Level 23+");
            }

            this.adCallbacks = new AdvertisementCallbacks();

            var settings = new AdvertiseSettings.Builder()
                           .SetAdvertiseMode(AdvertiseMode.Balanced)
                           .SetConnectable(true); // TODO: configurable

            var data = new AdvertiseData.Builder()
                       .SetIncludeDeviceName(true)    // TODO: configurable
                       .SetIncludeTxPowerLevel(true); // TODO: configurable

            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 #9
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 #10
0
        // use predefined Bluetooth SIG definitions for well know characteristics on UWP- Windows project.
        //public static Guid ManufacturerName = GattCharacteristicUuids.ManufacturerNameString;

        public BleServer(Context ctx)
        {
            _isRunning = false;

            try
            {
                _bluetoothManager = (BluetoothManager)ctx.GetSystemService(Context.BluetoothService);

                _bluetoothAdapter = _bluetoothManager.Adapter;

                _bluettothServerCallback = new BleGattServerCallback();
                _bluetoothServer         = _bluetoothManager.OpenGattServer(ctx, _bluettothServerCallback);

                var service = new BluetoothGattService(UUID.FromString(App.ServiceId), GattServiceType.Primary);

                //_characteristicDataExchange = new BluetoothGattCharacteristic(UUID.FromString(App.DataExchange), GattProperty.Read | GattProperty.Notify | GattProperty.Write, GattPermission.Read | GattPermission.Write);
                _characteristicInitData = new BluetoothGattCharacteristic(UUID.FromString(App.InitData), GattProperty.Read | GattProperty.Notify | GattProperty.Write, GattPermission.Read | GattPermission.Write);

                _characteristicFirmwareVersion  = new BluetoothGattCharacteristic(UUID.FromString(App.FirmwareVersion), GattProperty.Read, GattPermission.Read);
                _characteristicDeviceNickname   = new BluetoothGattCharacteristic(UUID.FromString(App.DeviceNickname), GattProperty.Read, GattPermission.Read);
                _descriptorDeviceNickname       = new BluetoothGattDescriptor(UUID.FromString(App.DeviceNicknameDescriptor), GattDescriptorPermission.Read);
                _characteristicDeviceContact    = new BluetoothGattCharacteristic(UUID.FromString(App.DeviceContact), GattProperty.Read, GattPermission.Read);
                _characteristicDeviceInfo       = new BluetoothGattCharacteristic(UUID.FromString(App.DeviceInfo), GattProperty.Read, GattPermission.Read);
                _characteristicInfected         = new BluetoothGattCharacteristic(UUID.FromString(App.IsInfected), GattProperty.Read, GattPermission.Read);
                _characteristicManufacturerName = new BluetoothGattCharacteristic(UUID.FromString(App.ManufacturerName), GattProperty.Read, GattPermission.Read);
                _characteristicMAC = new BluetoothGattCharacteristic(UUID.FromString(App.DeviceMAC), GattProperty.Read, GattPermission.Read);

                //_characteristicDataExchange.SetValue("Data exchange");
                //service.AddCharacteristic(_characteristicDataExchange);

                _characteristicFirmwareVersion.SetValue(App.FirmwareVersionValue);
                service.AddCharacteristic(_characteristicFirmwareVersion);

                _characteristicDeviceNickname.SetValue(App.DeviceNicknameValue);
                service.AddCharacteristic(_characteristicDeviceNickname);

                _characteristicDeviceContact.SetValue(App.DeviceContactValue);
                service.AddCharacteristic(_characteristicDeviceContact);

                _characteristicDeviceInfo.SetValue(App.DeviceInfoValue);
                service.AddCharacteristic(_characteristicDeviceInfo);

                _characteristicMAC.SetValue(App.DeviceMACValue);
                service.AddCharacteristic(_characteristicMAC);

                //_characteristicInitData.SetValue("SPACE - Copyright 2020");
                //service.AddCharacteristic(_characteristicInitData);

                _characteristicInfected.SetValue(App.IsInfectedValue);
                service.AddCharacteristic(_characteristicInfected);

                _characteristicManufacturerName.SetValue(App.ManufacturerNameValue);

                service.AddCharacteristic(_characteristicManufacturerName);

                _bluetoothServer.AddService(service);

                _bluettothServerCallback.CharacteristicReadRequest  += _bluettothServerCallback_CharacteristicReadRequest;
                _bluettothServerCallback.CharacteristicWriteRequest += _bluettothServerCallback_CharacteristicWriteRequest;
                //_bluettothServerCallback.NotificationSent += _bluettothServerCallback_NotificationSent;

                Console.WriteLine("Server created!");

                BluetoothLeAdvertiser myBluetoothLeAdvertiser = _bluetoothAdapter.BluetoothLeAdvertiser;

                var builder = new AdvertiseSettings.Builder();
                builder.SetAdvertiseMode(AdvertiseMode.LowLatency);
                builder.SetConnectable(true);
                builder.SetTimeout(0);
                builder.SetTxPowerLevel(AdvertiseTx.PowerHigh);


                AdvertiseData.Builder dataBuilder = new AdvertiseData.Builder();
                //dataBuilder.SetIncludeDeviceName(false);
                dataBuilder.AddServiceUuid(ParcelUuid.FromString(App.ServiceId));
                dataBuilder.SetIncludeTxPowerLevel(true);
                //byte[] data = Encoding.UTF8.GetBytes(App.ManufacturerNameValue);
                //dataBuilder.AddManufacturerData(0x00E0, data);


                AdvertiseData.Builder dataResponseBuilder = new AdvertiseData.Builder();
                dataResponseBuilder.SetIncludeDeviceName(true);

                //AdvertiseData.Builder dataManufacturerBuilder = new AdvertiseData.Builder();
                //byte[] data = Encoding.UTF8.GetBytes(App.ManufacturerNameValue);
                //dataManufacturerBuilder.AddManufacturerData(49177, data);

                myBluetoothLeAdvertiser.StartAdvertising(builder.Build(), dataBuilder.Build(), dataResponseBuilder.Build(), new BleAdvertiseCallback());

                _isRunning = true;
            }
            catch (System.Exception ex)
            {
            }
        }