Ejemplo n.º 1
0
		/// <Docs>GATT client</Docs>
		/// <summary>
		/// Raises the connection state change event.
		/// </summary>
		/// <param name="gatt">Gatt.</param>
		/// <param name="status">Status.</param>
		/// <param name="newState">New state.</param>
		public override void OnConnectionStateChange(BluetoothGatt gatt, GattStatus status, ProfileState newState)
		{
			base.OnConnectionStateChange(gatt, status, newState);

			if (status != GattStatus.Success)
				return;

            var device = new Device(gatt.Device, gatt, this, 0, null);
            switch (newState)
			{
				case ProfileState.Disconnected:
                    device.State = DeviceState.Disconnected;

                    try
                    {
                        gatt.Close();
                        gatt = null;
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Unable to close connection to gatt. Exception: {0}", ex.Message);
                    }
                    finally
                    {
                        DeviceDisconnected(this, new DeviceConnectionEventArgs(device));
                    }

					break;
				case ProfileState.Connected:
					device.State = DeviceState.Connected;
					
                    DeviceConnected(this, new DeviceConnectionEventArgs(device));   
					break;
			}
		}
 public void Dispose()
 {
     try
     {
         if (_gatt != null)
         {
             _gatt.Close();
             _gatt = null;
         }
     }
     catch (Exception exception)
     {
         Xamarin.Forms.Internals.Log.Warning("Dispose::Exception", exception.Message);
     }
 }
 public void Dispose()
 {
     try
     {
         if (_gatt != null)
         {
             _gatt.Close();
             _gatt = null;
         }
     }
     catch (Exception exception)
     {
         Log.Warning("Dispose::Exception", exception.Message);
     }
 }
Ejemplo n.º 4
0
        public override void OnConnectionStateChange(BluetoothGatt gatt, GattStatus status, ProfileState newState)
        {
            base.OnConnectionStateChange(gatt, status, newState);
            IDevice device = null;

            if (status != GattStatus.Success)
            {
                Mvx.TaggedError("OnConnectionStateChange", "GattCallback error: {0}", status);
                DeviceConnectionError(this, new DeviceConnectionEventArgs(){ Device = device });
                // We don't return. Allowing to fall-through to the SWITCH, which will assume a disconnect, close GATT and clean up.
                // The above error event handles the case where the error happened during a Connect call, which will close out any waiting asyncs.
            }
            else
            {
                Mvx.Trace("GattCallback state: {0}", newState.ToString());
            }
            
            switch (newState)
            {
                // disconnected
                case ProfileState.Disconnected:

                    if (DeviceOperationRegistry.TryGetValue(gatt.Device.Address, out device))
                    {
                        Mvx.Trace("Disconnected by user");

                        //Found so we can remove it
                        DeviceOperationRegistry.Remove(gatt.Device.Address);
                        ConnectedDeviceRegistry.Remove(gatt.Device.Address);
                        gatt.Close();

                        DeviceDisconnected(this, new DeviceConnectionEventArgs { Device = device });
                        break;
                    }

                    //connection must have been lost, bacause our device was not found in the registry but was still connected
                    if (ConnectedDeviceRegistry.TryGetValue(gatt.Device.Address, out device))
                    {
                        Mvx.Trace("Disconnected by lost connection");

                        ConnectedDeviceRegistry.Remove(gatt.Device.Address);
                        gatt.Close();

                        DeviceConnectionLost(this, new DeviceConnectionEventArgs() { Device = device });
                        break;
                    }

                    gatt.Close(); // Close GATT regardless, else we can accumulate zombie gatts.
                    Mvx.Trace("Disconnect. Device not found in registry. Not raising disconnect/lost event.");

                    break;
                // connecting
                case ProfileState.Connecting:
                    Mvx.Trace("Connecting");
                    break;
                // connected
                case ProfileState.Connected:
                    Mvx.Trace("Connected");

                    //Try to find the device in the registry so that the same instance is updated
                    if (DeviceOperationRegistry.TryGetValue(gatt.Device.Address, out device))
                    {
                        ((Device)device).Update(gatt.Device, gatt, this);

                        //Found so we can remove it
                        DeviceOperationRegistry.Remove(gatt.Device.Address);
                    }
                    else
                    {
                        //only for on auto-reconnect (device is not in operation registry)
                        device = new Device(gatt.Device, gatt, this, 0);
                    }

                    ConnectedDeviceRegistry[gatt.Device.Address] = device;
                    DeviceConnected(this, new DeviceConnectionEventArgs() { Device = device });

                    break;
                // disconnecting
                case ProfileState.Disconnecting:
                    Mvx.Trace("Disconnecting");
                    break;
            }
        }
Ejemplo n.º 5
0
        public override void OnConnectionStateChange(BluetoothGatt gatt, GattStatus status, ProfileState newState)
        {
            base.OnConnectionStateChange(gatt, status, newState);
            IDevice device;


            Trace.Message($"OnConnectionStateChange: GattStatus: {status}");

            switch (newState)
            {
                // disconnected
                case ProfileState.Disconnected:

                    if (_adapter.DeviceOperationRegistry.TryGetValue(gatt.Device.Address, out device))
                    {
                        Trace.Message("Disconnected by user");

                        //Found so we can remove it
                        _adapter.DeviceOperationRegistry.Remove(gatt.Device.Address);
                        _adapter.ConnectedDeviceRegistry.Remove(gatt.Device.Address);
                        gatt.Close();


                        if (status != GattStatus.Success)
                        {
                            // The above error event handles the case where the error happened during a Connect call, which will close out any waiting asyncs.
                            // Android > 5.0 uses this switch branch when an error occurs during connect
                            Trace.Message($"Error while connecting '{device.Name}'. Not raising disconnect event.");
                            _adapter.HandleConnectionFail(device, $"GattCallback error: {status}");

                        }
                        else
                        {
                            //we already hadled device error so no need th raise disconnect event(happens when device not in range)
                            _adapter.HandleDisconnectedDevice(true, device);
                        }
                        break;
                    }

                    //connection must have been lost, bacause our device was not found in the registry but was still connected
                    if (_adapter.ConnectedDeviceRegistry.TryGetValue(gatt.Device.Address, out device))
                    {
                        Trace.Message($"Disconnected '{device.Name}' by lost connection");

                        _adapter.ConnectedDeviceRegistry.Remove(gatt.Device.Address);
                        gatt.Close();

                        _adapter.HandleDisconnectedDevice(false, device);
                        break;
                    }

                    gatt.Close(); // Close GATT regardless, else we can accumulate zombie gatts.
                    Trace.Message("Disconnect. Device not found in registry. Not raising disconnect/lost event.");

                    break;
                // connecting
                case ProfileState.Connecting:
                    Trace.Message("Connecting");
                    break;
                // connected
                case ProfileState.Connected:
                    Trace.Message("Connected");

                    //Try to find the device in the registry so that the same instance is updated
                    if (_adapter.DeviceOperationRegistry.TryGetValue(gatt.Device.Address, out device))
                    {
                        ((Device)device).Update(gatt.Device, gatt, this);

                        //Found so we can remove it
                        _adapter.DeviceOperationRegistry.Remove(gatt.Device.Address);
                    }
                    else
                    {
                        //only for on auto-reconnect (device is not in operation registry)
                        device = new Device(_adapter, gatt.Device, gatt, this, 0);
                    }

                    if (status != GattStatus.Success)
                    {
                        // The aboe error event handles the case where the error happened during a Connect call, which will close out any waiting asyncs.
                        // Android <= 4.4 uses this switch branch when an error occurs during connect
                        Trace.Message($"Error while connecting '{device.Name}'. GattStatus: {status}. ");
                        _adapter.HandleConnectionFail(device, $"GattCallback error: {status}");

                        gatt.Close();
                    }
                    else
                    {
                        _adapter.ConnectedDeviceRegistry[gatt.Device.Address] = device;
                        _adapter.HandleConnectedDevice(device);
                    }

                    break;
                // disconnecting
                case ProfileState.Disconnecting:
                    Trace.Message("Disconnecting");
                    break;
            }
        }