コード例 #1
0
        public BleGattCharacteristicViewModel(Guid service, Guid characteristic, IBleGattServer device,
                                              IUserDialogs dialogs)
        {
            m_device         = device;
            m_dialogs        = dialogs;
            m_service        = service;
            m_characteristic = characteristic;

            RefreshValueCommand         = new Command(async() => { await ReadValue(); });
            EnableNotificationsCommand  = new Command(EnableNotifications);
            DisableNotificationsCommand = new Command(DisableNotifications);
            ToggleNotificationsCommand  = new Command(ToggleNotifications);
            WriteBytesCommand           = new Command(async() => { await WriteCurrentBytes(); });

            ValueAsHex    = String.Empty;
            ValueAsString = String.Empty;

            m_device.ReadCharacteristicProperties(m_service, m_characteristic).ContinueWith(
                x =>
            {
                Device.BeginInvokeOnMainThread(
                    () =>
                {
                    //Log.Trace( "Reading properties for characteristic. id={0}", m_characteristic );
                    m_props = x.Result;
                    RaisePropertyChanged(nameof(CanNotify));
                    RaisePropertyChanged(nameof(CanRead));
                    RaisePropertyChanged(nameof(CanWrite));
                });
            });
        }
コード例 #2
0
 public BleGattServiceViewModel(Guid service, IBleGattServer gattServer, IUserDialogs dialogManager)
 {
     m_serviceGuid   = service;
     Characteristic  = new ObservableCollection <BleGattCharacteristicViewModel>();
     m_gattServer    = gattServer;
     m_dialogManager = dialogManager;
 }
コード例 #3
0
 public BleGattServiceViewModel(Guid service, IBleGattServer device, IUserDialogs dialogs)
 {
     m_serviceId    = service;
     Characteristic = new ObservableCollection <BleGattCharacteristicViewModel>();
     m_device       = device;
     m_dialogs      = dialogs;
 }
コード例 #4
0
 /// <summary>
 /// Listen for NOTIFY events on this characteristic.
 /// </summary>
 public static IDisposable NotifyCharacteristicValue(this IBleGattServer server, Guid service,
                                                     Guid characteristic, Action <Tuple <Guid, Byte[]> > onNotify,
                                                     Action <Exception> onError = null)
 {
     Contract.Requires <ArgumentNullException>(server != null);
     // ReSharper disable once PossibleNullReferenceException
     return(server.NotifyCharacteristicValue(service, characteristic, Observer.Create(onNotify, null, onError)));
 }
コード例 #5
0
 /// <summary>
 /// Listen for NOTIFY events on this characteristic.
 /// </summary>
 public static IDisposable NotifyCharacteristicValue(this IBleGattServer server, Guid service, Guid characteristic,
                                                     Action <Byte[]> onNotify, Action <Exception> onError = null)
 {
     return(server.NotifyCharacteristicValue(
                service,
                characteristic,
                Observer.Create((Tuple <Guid, Byte[]> tuple) => onNotify(tuple.Item2), null, onError)));
 }
コード例 #6
0
        public override async void OnAppearing()
        {
            // if we're budy or have a valid connection, then no-op
            if (IsBusy || (m_device != null && m_device.State != ConnectionState.Disconnected))
            {
                return;
            }

            IsBusy = true;
            CloseConnection();
            Log.Debug("Connecting to device. address={0}", m_peripheral.Address);
            m_device = await m_ble.ConnectToDevice(m_peripheral.Model);

            RaisePropertyChanged(nameof(DeviceConnectionState));
            m_device.Subscribe(
                Observer.Create(
                    ( ConnectionState c ) =>
            {
                Log.Info("Device state changed. address={0} status={1}", m_peripheral.Address, c);
                RaisePropertyChanged(nameof(DeviceConnectionState));
            }));
            Log.Debug("Connected to device. address={0} status={1}", m_peripheral.Address, m_device.State);
            if (m_device.State == ConnectionState.Connected || m_device.State == ConnectionState.Connecting)
            {
                var services = (await m_device.ListAllServices()).ToList();
                foreach (var serviceId in services)
                {
                    if (Services.Any(viewModel => viewModel.Guid.Equals(serviceId)))
                    {
                        continue;
                    }
                    Services.Add(new BleGattServiceViewModel(serviceId, m_device, m_dialog));
                }
                if (services.Count == 0)
                {
                    m_dialog.Toast("No services found");
                }
            }
            else
            {
                Log.Warn(
                    "Error connecting to device. address={0} state={1}",
                    m_device.Address.EncodeToBase16String(),
                    m_device.State);
                m_dialog.Toast("Error connecting to device");
            }
            Log.Debug("Read services. address={0} status={1}", m_peripheral.Address, m_device.State);
            IsBusy = false;
            RaisePropertyChanged(nameof(DeviceConnectionState));
        }
コード例 #7
0
        public BleGattCharacteristicViewModel(Guid serviceGuid, Guid characteristicGuid, IBleGattServer gattServer,
                                              IUserDialogs dialogManager)
        {
            m_gattServer         = gattServer;
            m_dialogManager      = dialogManager;
            m_serviceGuid        = serviceGuid;
            m_characteristicGuid = characteristicGuid;

            RefreshValueCommand         = new Command(async() => { await ReadCharacteristicValue(); });
            EnableNotificationsCommand  = new Command(EnableNotifications);
            DisableNotificationsCommand = new Command(DisableNotifications);
            ToggleNotificationsCommand  = new Command(() => { NotifyEnabled = !NotifyEnabled; });
            WriteBytesCommand           = new Command(async() => { await WriteCurrentBytes(); });

            ValueAsHex    = String.Empty;
            ValueAsString = String.Empty;

            m_gattServer.ReadCharacteristicProperties(m_serviceGuid, m_characteristicGuid).ContinueWith(
                x =>
            {
                Device.BeginInvokeOnMainThread(
                    () =>
                {
                    if (x.IsFaulted)
                    {
                        m_dialogManager.Toast(x.Exception.GetBaseException().Message);
                    }
                    else
                    {
                        Log.Trace("Reading properties for characteristic. id={0}", m_characteristicGuid);
                        m_props = x.Result;
                        RaisePropertyChanged(nameof(CanNotify));
                        RaisePropertyChanged(nameof(CanRead));
                        RaisePropertyChanged(nameof(CanWrite));
                    }
                });
            });
        }
コード例 #8
0
        public async Task OpenConnection()
        {
            // if we're busy or have a valid connection, then no-op
            if (IsBusy || m_gattServer != null && m_gattServer.State != ConnectionState.Disconnected)
            {
                //Log.Debug( "OnAppearing. state={0} isbusy={1}", m_gattServer?.State, IsBusy );
                return;
            }

            CloseConnection();
            IsBusy = true;

            var connection = await m_bleAdapter.ConnectToDevice(
                device : m_peripheral.Model,
                timeout : TimeSpan.FromSeconds(CONNECTION_TIMEOUT_SECONDS),
                progress : progress => { Connection = progress.ToString(); });

            if (connection.IsSuccessful())
            {
                m_gattServer = connection.GattServer;
                Connection   = "Reading Services";
                Log.Debug("Connected to device. id={0} status={1}", m_peripheral.Id, m_gattServer.State);

                m_gattServer.Subscribe(
                    c =>
                {
                    if (c == ConnectionState.Disconnected)
                    {
                        m_dialogManager.Toast("Device disconnected");
                        CloseConnection();
                    }
                    Connection = c.ToString();
                });

                // small possibility the device could disconnect between connecting and getting services and throw somewhere along here
                var services = (await m_gattServer.ListAllServices()).ToList();
                foreach (var serviceId in services)
                {
                    if (Services.Any(viewModel => viewModel.Guid.Equals(serviceId)))
                    {
                        continue;
                    }
                    Services.Add(new BleGattServiceViewModel(serviceId, m_gattServer, m_dialogManager));
                }
                if (Services.Count == 0)
                {
                    m_dialogManager.Toast("No services found");
                }
                Connection = m_gattServer.State.ToString();
            }
            else
            {
                String errorMsg;
                if (connection.ConnectionResult == ConnectionResult.ConnectionAttemptCancelled)
                {
                    errorMsg = "Connection attempt cancelled after {0} seconds (see {1})".F(
                        CONNECTION_TIMEOUT_SECONDS,
                        GetType().Name + ".cs");
                }
                else
                {
                    errorMsg = "Error connecting to device: {0}".F(connection.ConnectionResult);
                }
                Log.Info(errorMsg);
                m_dialogManager.Toast(errorMsg, TimeSpan.FromSeconds(5));
            }
            IsBusy = false;
        }
コード例 #9
0
 /// <summary>
 /// </summary>
 public BleDeviceConnection(ConnectionResult connectionResult, IBleGattServer gattServer)
 {
     ConnectionResult = connectionResult;
     GattServer       = gattServer;
 }