예제 #1
0
        protected override bool UpdateConnectionIntervalNative(ConnectionInterval interval)
        {
            if (_gatt == null || _gattCallback == null)
            {
                Trace.Message("You can't update a connection interval for disconnected devices. Device is {0}", State);
                return(false);
            }

            if (Build.VERSION.SdkInt < BuildVersionCodes.Lollipop)
            {
                Trace.Message($"Update connection interval paramter in this Android API level");
                return(false);
            }

            try
            {
                // map to android gattConnectionPriorities
                // https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#CONNECTION_PRIORITY_BALANCED
                return(_gatt.RequestConnectionPriority((GattConnectionPriority)(int)interval));
            }
            catch (Exception ex)
            {
                throw new Exception($"Update Connection Interval fails with error. {ex.Message}");
            }
        }
예제 #2
0
        public override void OnConnectionStateChange(BluetoothGatt gatt, GattStatus status, ProfileState newState)
        {
            Console.WriteLine("OnConnectionStateChange: ");
            base.OnConnectionStateChange(gatt, status, newState);

            //TODO: need to pull the cached RSSI in here, or read it (requires the callback)
            Device device = new Device(gatt.Device, gatt, this, 0);

            switch (newState)
            {
            // disconnected
            case ProfileState.Disconnected:
                Console.WriteLine("disconnected");
                if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
                {
                    Console.WriteLine("Changing connection priority to balanced");
                    gatt.RequestConnectionPriority(BluetoothGatt.ConnectionPriorityBalanced);
                }
                this.DeviceDisconnected(this, new DeviceConnectionEventArgs()
                {
                    Device = device
                });
                break;

            // connecting
            case ProfileState.Connecting:
                Console.WriteLine("Connecting");
                break;

            // connected
            case ProfileState.Connected:
                Console.WriteLine("Connected");
                if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
                {
                    Console.WriteLine("Changing connection priority to high");
                    gatt.RequestConnectionPriority(BluetoothGatt.ConnectionPriorityHigh);
                }
                this.DeviceConnected(this, new DeviceConnectionEventArgs()
                {
                    Device = device
                });
                break;

            // disconnecting
            case ProfileState.Disconnecting:
                Console.WriteLine("Disconnecting");
                break;
            }
        }
예제 #3
0
        public void ConnectGatt(BluetoothDevice device)
        {
            if (device == null)
            {
                if (Gatt != null)
                {
                    Gatt.Close();
                    Gatt = null;
                }

                return;
            }

            Gatt = device.ConnectGatt(this, false, new DT1WatchDogGattCallback(this));
            Gatt.RequestConnectionPriority(GattConnectionPriority.High);
        }
예제 #4
0
 public void SetConnectionPriority(ConnectionPriority connectionPriority)
 {
     _gatt?.RequestConnectionPriority(connectionPriority.ToGattConnectionPriority());
 }