Example #1
0
 /// <summary>
 /// Connect to a discovered <see cref="IBlePeripheral" />. Timeout after the default time;
 /// <see cref="BluetoothLowEnergyUtils.DefaultConnectionTimeout" />
 /// </summary>
 public static Task <BleDeviceConnection> ConnectToDevice(this IBluetoothLowEnergyAdapter adapter,
                                                          IBlePeripheral device,
                                                          IProgress <ConnectionProgress> progress = null)
 {
     Contract.Requires <ArgumentNullException>(adapter != null);
     return(ConnectToDevice(adapter, device, BluetoothLowEnergyUtils.DefaultConnectionTimeout, progress));
 }
Example #2
0
        public async Task <bool> BLE_Connect(IBlePeripheral device)
        {
            // Get BLE device
            if ((null != g_BLEAdapterObj) && (null != device))
            {
                // Try connect to ble device
                BlePeripheralConnectionRequest connection = await g_BLEAdapterObj.ConnectToDevice(device, TimeSpan.FromSeconds(15));

                if (connection.IsSuccessful())
                {
                    // Invoke connection state callback
                    OnBLEConnection_StateChange(ConnectionState.Connected);

                    if (true == await SetupBLEConnection(connection))
                    {
                        return(true);
                    }
                    else
                    {
                        await connection.GattServer.Disconnect();
                    }
                }
            }

            return(false);
        }
Example #3
0
 /// <summary>
 /// Connect to a discovered <see cref="IBlePeripheral" />. Timeout after the default time;
 /// <see cref="BluetoothLowEnergyUtils.DefaultConnectionTimeout" />
 /// </summary>
 public static Task <BleDeviceConnection> ConnectToDevice(this IBluetoothLowEnergyAdapter adapter,
                                                          IBlePeripheral device,
                                                          Action <ConnectionProgress> progress)
 {
     Contract.Requires <ArgumentNullException>(adapter != null);
     return(ConnectToDevice(adapter, device, new Progress <ConnectionProgress>(progress)));
 }
Example #4
0
 /// <summary>
 /// Connect to a discovered <see cref="IBlePeripheral" />. Timeout if the connection is not obtained in
 /// the provided time
 /// </summary>
 public static Task <BleDeviceConnection> ConnectToDevice(this IBluetoothLowEnergyAdapter adapter,
                                                          IBlePeripheral device, TimeSpan timeout,
                                                          IProgress <ConnectionProgress> progress = null)
 {
     Contract.Requires <ArgumentNullException>(adapter != null);
     // ReSharper disable once PossibleNullReferenceException
     return(adapter.ConnectToDevice(device, new CancellationTokenSource(timeout).Token, progress));
 }
Example #5
0
        private void OnBLEAdapter_ScanResponses(IBlePeripheral peripheral)
        {
            string deviceName = peripheral.Advertisement.DeviceName;

            if (null != deviceName)
            {
                g_BLEScanDevices.TryAdd(deviceName, peripheral);
            }
        }
        /// <summary>
        /// Connect to a discovered <see cref="IBlePeripheral" />. Timeout after the default time;
        /// <see cref="BluetoothLowEnergyUtils.DefaultConnectionTimeout" />
        /// </summary>
        public static Task <BlePeripheralConnectionRequest> ConnectToDevice(
            [NotNull] this IBluetoothLowEnergyAdapter adapter, IBlePeripheral device, Action <ConnectionProgress> progress)
        {
            if (adapter == null)
            {
                throw new ArgumentNullException(nameof(adapter));
            }

            return(ConnectToDevice(adapter, device, new Progress <ConnectionProgress>(progress)));
        }
Example #7
0
 /// <summary>
 /// Opens a new serial port connection.
 /// </summary>
 /// <param name="blePeripheral">The object that contains the device information.</param>
 /// <remarks></remarks>
 public void Open(IBlePeripheral blePeripheral, bool isBounded = false)
 {
     if (!IsOpen())
     {
         Task.Factory.StartNew(() => ble_port_serial.ConnectoToDevice(blePeripheral, isBounded));
     }
     else
     {
         // TO-DO: mantenemos la misma conexion o cerramos y volvemos a abrir otra
     }
 }
        /// <summary>
        /// Connect to a discovered <see cref="IBlePeripheral" />. Timeout if the connection is not obtained in
        /// the provided time
        /// </summary>
        public static Task <BlePeripheralConnectionRequest> ConnectToDevice(
            [NotNull] this IBluetoothLowEnergyAdapter adapter, IBlePeripheral device, TimeSpan timeout,
            IProgress <ConnectionProgress> progress = null)
        {
            if (adapter == null)
            {
                throw new ArgumentNullException(nameof(adapter));
            }

            return(adapter.ConnectToDevice(device, new CancellationTokenSource(timeout).Token, progress));
        }
Example #9
0
        private async Task <IBlePeripheral> GetRileyLink()
        {
            if (this.Ble.CurrentState.IsDisabledOrDisabling() && this.Ble.AdapterCanBeEnabled)
            {
                Console.WriteLine("Enabling ble adapter");
                await this.Ble.EnableAdapter();
            }

            if (_peripheral != null)
            {
                return(_peripheral);
            }

            using (var cts = new CancellationTokenSource(10000))
            {
                Console.WriteLine("Scanning");
                await this.Ble.ScanForBroadcasts(
                    new ScanSettings()
                {
                    Mode   = ScanMode.LowPower,
                    Filter = new ScanFilter()
                    {
                        AdvertisedServiceIsInList = new List <Guid>()
                        {
                            RileyLinkServiceUUID
                        },
                    },
                    IgnoreRepeatBroadcasts = true
                },
                    (peripheral) =>
                {
                    Console.WriteLine($"Found peripheral at address {peripheral.Address}, name: {peripheral.Advertisement.DeviceName}");

                    _peripheral = peripheral;
                    cts.Cancel();
                }, cts.Token);
            }
            return(_peripheral);
        }
        private async Task ConnectToDevice(IBlePeripheral blePeripheral)
        {
            BleService.gattServer = null;
            ToastConfig toastConfig;

            int  timeout = 5000;
            Task task    = EstablishConnection(blePeripheral);

            ProgressDialogConfig.UseAndroidImmersiveMode = true;
            UserDialogs.Instance.ShowLoading("Connecting", MaskType.Black);
            if (await Task.WhenAny(task, Task.Delay(timeout)) == task)
            {
                UserDialogs.Instance.HideLoading();
                toastConfig = new ToastConfig("Connected to " + blePeripheral.Advertisement.DeviceName);
            }
            else
            {
                UserDialogs.Instance.HideLoading();
                toastConfig = new ToastConfig("Couldnot connect, is the device turned on?");
            }

            toastConfig.SetDuration(3000);
            UserDialogs.Instance.Toast(toastConfig);
        }
        // Event for Menu Item selection, here we are going to handle navigation based
        // on user selection in menu ListView
        private void OnMenuItemSelectedListDevices(object sender, ItemTappedEventArgs e)
        {
            PrintToConsole($"-------------------------------       OnMenuItemSelectedListDevices, thread: { Thread.CurrentThread.ManagedThreadId}");
            try
            {
                printer.Resume();
            }
            catch (Exception e5)
            {
                Console.WriteLine(e5.StackTrace);
            }


            var item = (DeviceItem)e.Item;

            //fondo.Opacity = 0;
            ContentView_DeviceList.Opacity   = 0.5;
            ContentView_DeviceList.IsEnabled = false;

            Device.BeginInvokeOnMainThread(() =>
            {
                #region New Circular Progress bar Animations

                DeviceList.IsRefreshing          = false;
                backdark_bg.IsVisible            = true;
                indicator.IsVisible              = true;
                ContentView_DeviceList.IsEnabled = false;
                #endregion
            });

            bool reassociate = false;
            if (CrossSettings.Current.GetValueOrDefault("session_dynamicpass", string.Empty) != string.Empty &&
                FormsApp.credentialsService.UserName.Equals(CrossSettings.Current.GetValueOrDefault("session_username", string.Empty)) &&
                System.Convert.ToBase64String(item.Peripheral.Advertisement.ManufacturerSpecificData.ElementAt(0).Data.Take(4).ToArray()).Equals(CrossSettings.Current.GetValueOrDefault("session_peripheral_DeviceId", string.Empty)) &&
                item.Peripheral.Advertisement.DeviceName.Equals(CrossSettings.Current.GetValueOrDefault("session_peripheral", string.Empty)))
            {
                reassociate = true;
            }

            FormsApp.ble_interface.Open(item.Peripheral, reassociate);
            peripheral = item.Peripheral;

            Device.BeginInvokeOnMainThread(() =>
            {
                ContentView_Scripting_battery_level.Source = item.deviceBatteryIcon + "_white";
                ContentView_Scripting_rssi_level.Source    = item.deviceRssiIcon + "_white";
            });


            /*
             * Device.BeginInvokeOnMainThread(() =>
             * {
             *  try
             *  {
             *      deviceID.Text = item.deviceName;
             *      macAddress.Text = item.deviceMacAddress;
             *      imageBattery.Source = item.deviceBatteryIcon;
             *      imageRssi.Source = item.deviceRssiIcon;
             *      batteryLevel.Text = item.deviceBattery;
             *      rssiLevel.Text = item.deviceRssi;
             *  }
             *  catch (Exception e4)
             *  {
             *      Console.WriteLine(e4.StackTrace);
             *  }
             * });
             *
             */
        }
Example #12
0
 /// <inheritDoc />
 public Boolean Equals( IBlePeripheral other )
 {
    return !ReferenceEquals( null, other ) && other.DeviceId.Equals( DeviceId );
 }
Example #13
0
 public void RemovePuck()
 {
     this.puck = null;
 }
Example #14
0
 public Puck(
     IBlePeripheral puck)
 {
     this.puck = puck;
 }
 /// <summary>
 /// Connect to a discovered <see cref="IBlePeripheral" />. Timeout if the connection is not obtained in
 /// the provided time
 /// </summary>
 public static Task<IBleGattServer> ConnectToDevice( this IBluetoothLowEnergyAdapter adapter, IBlePeripheral device,
                                                     TimeSpan timeout )
 {
    return adapter.ConnectToDevice( device, new CancellationTokenSource( timeout ).Token );
 }
 /// <summary>
 /// Connect to a discovered <see cref="IBlePeripheral" />. Timeout after the default time;
 /// <see cref="DefaultConnectionTimeout" />
 /// </summary>
 public static Task<IBleGattServer> ConnectToDevice( this IBluetoothLowEnergyAdapter adapter, IBlePeripheral device )
 {
    return ConnectToDevice( adapter, device, DefaultConnectionTimeout );
 }
 /// <inheritDoc />
 public Boolean Equals(IBlePeripheral other)
 {
     return(!ReferenceEquals(null, other) && other.DeviceId.Equals(DeviceId));
 }
        private async Task EstablishConnection(IBlePeripheral blePeripheral)
        {
            BleService.connection = await BleService.bleAdapter.ConnectToDevice(blePeripheral);

            BleService.gattServer = BleService.connection.GattServer;
        }
Example #19
0
 /// <summary>
 /// Connect to a discovered <see cref="IBlePeripheral" />. Timeout after the default time;
 /// <see cref="DefaultConnectionTimeout" />
 /// </summary>
 public static Task <IBleGattServer> ConnectToDevice(this IBluetoothLowEnergyAdapter adapter, IBlePeripheral device)
 {
     return(ConnectToDevice(adapter, device, DefaultConnectionTimeout));
 }
Example #20
0
 public bool Equals(IBlePeripheral other)
 {
     throw new NotImplementedException();
 }
Example #21
0
 public BleDevice(IBlePeripheral abs)
 {
     this.abs = abs;
 }
Example #22
0
 /// <summary>
 /// Connect to a discovered <see cref="IBlePeripheral" />. Timeout if the connection is not obtained in
 /// the provided time
 /// </summary>
 public static Task <IBleGattServer> ConnectToDevice(this IBluetoothLowEnergyAdapter adapter, IBlePeripheral device,
                                                     TimeSpan timeout)
 {
     return(adapter.ConnectToDevice(device, new CancellationTokenSource(timeout).Token));
 }