Пример #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);
        }
Пример #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);
        }
Пример #3
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;
        }
Пример #4
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);
        }
Пример #5
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);
        }
Пример #6
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);
        }
Пример #7
0
        public BleServer(Context ctx)
        {
            _bluetoothManager        = (BluetoothManager)ctx.GetSystemService(Context.BluetoothService);
            _bluetoothAdapter        = _bluetoothManager.Adapter;
            _bluettothServerCallback = new BleGattServerCallback();
            _bluetoothServer         = _bluetoothManager.OpenGattServer(ctx, _bluettothServerCallback);

            var service = new BluetoothGattService(UUID.FromString("ffe0ecd2-3d16-4f8d-90de-e89e7fc396a5"),
                                                   GattServiceType.Primary);

            _characteristic = new BluetoothGattCharacteristic(UUID.FromString("d8de624e-140f-4a22-8594-e2216b84a5f2"), GattProperty.Read | GattProperty.Notify | GattProperty.Write, GattPermission.Read | GattPermission.Write);

            _characteristic.AddDescriptor(new BluetoothGattDescriptor(UUID.FromString("28765900-7498-4bd4-aa9e-46c4a4fb7b07"),
                                                                      GattDescriptorPermission.Read | GattDescriptorPermission.Write));

            service.AddCharacteristic(_characteristic);


            _bluetoothServer.AddService(service);

            _bluettothServerCallback.CharacteristicReadRequest += _bluettothServerCallback_CharacteristicReadRequest;
            _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(true);
            byte[] bytes = Encoding.ASCII.GetBytes("jp");
            dataBuilder.AddManufacturerData(1984, bytes);
            dataBuilder.SetIncludeTxPowerLevel(true);

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