void IAdvertiseAndDiscoverBluetoothDevice.Advertise()
        {
            try
            {
                Analytics.TrackEvent(Build.Model + " Advertise method called.");

                bluetoothLeAdvertiser = BluetoothAdapter.DefaultAdapter.BluetoothLeAdvertiser;

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

                ParcelUuid pUuid = new ParcelUuid(MY_UUID);

                AdvertiseData data = new AdvertiseData.Builder()
                                     .SetIncludeDeviceName(true)
                                     .AddServiceUuid(pUuid)
                                     .AddServiceData(pUuid, Encoding.UTF8.GetBytes("222"))
                                     .Build();

                myAdvertiseCallback = new MyAdvertiseCallback();

                bluetoothLeAdvertiser.StartAdvertising(settings, data, myAdvertiseCallback);
            }
            catch (System.Exception ex)
            {
                Analytics.TrackEvent(Build.Model + " Something went wrong in Advertise method.");
            }
        }
Example #2
0
        public void getBle(String usuario)
        {
            Context          actvity          = Application.Context;
            BluetoothManager bluetooth        = (BluetoothManager)actvity.GetSystemService(Context.BluetoothService);
            BluetoothAdapter bluetoothAdapter = bluetooth.Adapter;

            if (bluetoothAdapter.IsEnabled)
            {
                bluetoothAdapter.SetName(usuario);
                BluetoothLeAdvertiser bluetoothLeAdvertiser = bluetoothAdapter.BluetoothLeAdvertiser;
                AdvertiseSettings     settings = new AdvertiseSettings.Builder()
                                                 .SetAdvertiseMode(AdvertiseMode.LowLatency).SetTxPowerLevel(AdvertiseTx.PowerHigh).SetConnectable(true).Build();

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

                AdvertiseData.Builder dataBuilder = new AdvertiseData.Builder();
                dataBuilder.SetIncludeDeviceName(true);
                dataBuilder.SetIncludeTxPowerLevel(true);

                bluetoothLeAdvertiser.StartAdvertising(builder.Build(), dataBuilder.Build(), new BleAdvertiseCallback());
                Android.Widget.Toast.MakeText(Application.Context, "Registro de presença automático iniciado.", Android.Widget.ToastLength.Long).Show();
            }
            else
            {
                Android.Widget.Toast.MakeText(Application.Context, "Por gentileza, o Bluetooth deve estar ligado!", Android.Widget.ToastLength.Long).Show();
            }
        }
        public void Advertise()
        {
            try
            {
                bluetoothLeAdvertiser = BluetoothAdapter.DefaultAdapter.BluetoothLeAdvertiser;

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

                ParcelUuid pUuid = new ParcelUuid(MY_UUID);

                AdvertiseData data = new AdvertiseData.Builder()
                                     .SetIncludeDeviceName(true)
                                     .AddServiceUuid(pUuid)
                                     .AddServiceData(pUuid, Encoding.UTF8.GetBytes("Data send"))
                                     .Build();

                myAdvertiseCallback = new MyAdvertiseCallback();

                bluetoothLeAdvertiser.StartAdvertising(settings, data, myAdvertiseCallback);
            }
            catch (System.Exception ex)
            {
            }
        }
Example #4
0
        private void InitBleAd()
        {
            BluetoothManager      mBluetoothManager      = (BluetoothManager)Android.App.Application.Context.GetSystemService(Android.Content.ContextWrapper.BluetoothService);
            BluetoothAdapter      mBluetoothAdapter      = mBluetoothManager.Adapter;
            BluetoothLeAdvertiser mBluetoothLeAdvertiser = mBluetoothAdapter.BluetoothLeAdvertiser;

            _mBleAd = mBluetoothLeAdvertiser;
        }
Example #5
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.Write | GattDescriptorPermission.Read));

            service.AddCharacteristic(_characteristic);


            _characteristic = new BluetoothGattCharacteristic(UUID.FromString("d8de624e-140f-4a22-8594-e2216b84a5f3"), GattProperty.Read | GattProperty.Notify | GattProperty.Write, GattPermission.Read | GattPermission.Write);
            _characteristic.AddDescriptor(new BluetoothGattDescriptor(UUID.FromString("28765900-7498-4bd4-aa9e-46c4a4fb7b08"),
                                                                      GattDescriptorPermission.Write | GattDescriptorPermission.Read));

            service.AddCharacteristic(_characteristic);
            _bluetoothServer.AddService(service);

            _bluettothServerCallback.CharacteristicReadRequest  += _bluettothServerCallback_CharacteristicReadRequest;
            _bluettothServerCallback.DescriptorWriteRequest     += _bluettothServerCallback_DescriptorWriteRequest;
            _bluettothServerCallback.CharacteristicWriteRequest += _bluettothServerCallback_CharacteristicWriteRequest;
            _bluettothServerCallback.NotificationSent           += _bluettothServerCallback_NotificationSent;
            _bluettothServerCallback.ConnectionStateChange      += _bluettothServerCallback_ConnectionStateChange;
            //           MessagingCenter.Send<BLE202.App, string>((BLE202.App)Xamarin.Forms.Application.Current, "Hi", "Server created!");
            //           _bluetoothAdapter.SetName("Concungungoc");
            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);
            //dataBuilder.AddServiceUuid(ParcelUuid.FromString("ffe0ecd2-3d16-4f8d-90de-e89e7fc396a5"));
            dataBuilder.SetIncludeTxPowerLevel(true);
            myBluetoothLeAdvertiser.StartAdvertising(builder.Build(), dataBuilder.Build(), new BleAdvertiseCallback());
        }
Example #6
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());
        }
        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);

            service.AddCharacteristic(_characteristic);
            _bluetoothServer.AddService(service);

            SensorSpeed speed = SensorSpeed.UI;

            OrientationSensor.Start(speed);
            OrientationSensor.ReadingChanged += OrientationSensor_ReadingChanged;

            _bluettothServerCallback.CharacteristicReadRequest += _bluettothServerCallback_CharacteristicReadRequest;
            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);
            dataBuilder.SetIncludeTxPowerLevel(true);

            myBluetoothLeAdvertiser.StartAdvertising(builder.Build(), dataBuilder.Build(), new BleAdvertiseCallback());
        }
Example #8
0
        public void StartLeServer()
        {
            gattServer = bluetoothManager.OpenGattServer(Xamarin.Forms.Forms.Context, gattServerCallback);
            if (gattServer != null)
            {
                gattServer.AddService(CreateTimeService());


                BluetoothLeAdvertiser leAdvertiser = bluetoothManager.Adapter.BluetoothLeAdvertiser;
                AdvertiseSettings     settings     = new AdvertiseSettings.Builder().SetAdvertiseMode(AdvertiseMode.Balanced)
                                                     .SetConnectable(true)
                                                     .SetTimeout(0)
                                                     .SetTxPowerLevel(AdvertiseTx.PowerHigh)
                                                     .Build();

                AdvertiseData data = new AdvertiseData.Builder()
                                     .SetIncludeDeviceName(true)
                                     .SetIncludeTxPowerLevel(true)
                                     .AddServiceUuid(new ParcelUuid(TIME_SERVICE))
                                     .Build();

                leAdvertiser.StartAdvertising(settings, data, new LeAdvertiseCallback());
            }
        }
Example #9
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
            Context appContext = Android.App.Application.Context;

            _manager = (BluetoothManager)appContext.GetSystemService("bluetooth");
            _adapter = _manager.Adapter;
            //		_gattCallback = new GattCallback(this);
            _scanner         = _adapter.BluetoothLeScanner;
            _scannerCallback = new BLEScannerCallback(this);
            _textView        = FindViewById <TextView>(Resource.Id.textView);
            _advertiser      = _adapter.BluetoothLeAdvertiser;
            Button button = FindViewById <Button> (Resource.Id.button);

            button.Click += delegate {
                button.Text = string.Format("{0} clicks!", count++);
                if (!_isScanning)
                {
                    Task.Run(async() =>
                    {
                        await BeginScanningForDevices();
                    });
                }
                else
                {
                    StopScanningForDevices();
                }
            };

            _advertiserCallback = new BLEAdvertiserCallback(this);
            Button button2 = FindViewById <Button>(Resource.Id.button2);

            button2.Click += (sender, e) =>
            {
                if (!_isAdvertizing)
                {
                    AdvertiseSettings settings = new AdvertiseSettings.Builder()
                                                 .SetAdvertiseMode(AdvertiseMode.Balanced)
                                                 .SetConnectable(true)
                                                 .SetTimeout(0)
                                                 .SetTxPowerLevel(AdvertiseTx.PowerMedium)
                                                 .Build();

                    AdvertiseData data = new AdvertiseData.Builder()
                                         .SetIncludeDeviceName(true)
                                         .SetIncludeTxPowerLevel(true)
                                         .AddServiceUuid(THERM_SERVICE)
                                         //.AddServiceData(THERM_SERVICE, buildTempPacket())
                                         .Build();

                    _advertiser.StartAdvertising(settings, data, _advertiserCallback);
                    button2.Text = "Stop Advertizing";
                    UpdateTextView("Advertizing");
                    _isAdvertizing = true;
                }
                else
                {
                    _advertiser.StopAdvertising(_advertiserCallback);
                    button2.Text = "Start Advertizing";
                    UpdateTextView("Stopped Advertizing");
                    _isAdvertizing = false;
                }
            };

            _listView            = FindViewById <ListView>(Resource.Id.listView1);
            _listView.ItemClick += (object sender, AdapterView.ItemClickEventArgs e) =>
            {
                UpdateTextView("ListView clicked item, Id: " + e.Id + " Pos: " + e.Position);
            };

            UpdateTextView("OnCreate Complete");
        }
Example #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)
            {
            }
        }