Exemplo n.º 1
0
        public override void OnCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, GattStatus status)
        {
            base.OnCharacteristicRead(gatt, characteristic, status);

            BLECharacteristic bleCharacteristic = null;

            foreach (BLEService bles in services)
            {
                foreach (BLECharacteristic blec in bles.Characteristics)
                {
                    if (blec.UUID.StringValue == bleCharacteristic.UUID.ToString())
                    {
                        bleCharacteristic = blec;

                        break;
                    }
                }
            }

            EventHandler <BLECharacteristicEventArgs> handler = UpdatedCharacteristicValue;

            if (handler != null)
            {
                BLECharacteristicEventArgs args = new BLECharacteristicEventArgs();
                args.Characteristic = bleCharacteristic;

                if (status != GattStatus.Success)
                {
                    args.Error = status.ToString();
                }

                handler(this, args);
            }
        }
        public async Task <byte> ReadBatteryLevelAsync(BluetoothCacheMode cacheMode = BluetoothCacheMode.Uncached)
        {
            GattStatus gattStatus = GattStatus.Unsupported;

            var batteryLevelCharacteristic = this.GetCharacteristic(GattCharacteristicUuids.BatteryLevel);

            if (batteryLevelCharacteristic == null)
            {
                gattStatus = GattStatus.UnknownCharacteristic;
            }
            else
            {
                GattReadResult readResult = await batteryLevelCharacteristic.ReadValueAsync(cacheMode);

                gattStatus = (GattStatus)readResult.Status;
                if (gattStatus == GattStatus.Success)
                {
                    using (DataReader reader = DataReader.FromBuffer(readResult.Value))
                    {
                        return(reader.ReadByte());
                    }
                }
            }

            OnError("ReadBatteryLevelAsync:" + gattStatus.ToString());
            return(0);
        }
Exemplo n.º 3
0
        public override void OnServicesDiscovered(BluetoothGatt gatt, GattStatus status)
        {
            base.OnServicesDiscovered(gatt, status);

            foreach (BluetoothGattService bgs in gatt.Services)
            {
                BLEService service = new BLEService(bgs);

                services.Add(service);
            }

            EventHandler <BLEErrorEventArgs> handler = DiscoveredService;

            if (handler != null)
            {
                BLEErrorEventArgs args = new BLEErrorEventArgs();

                if (status != GattStatus.Success)
                {
                    args.Error = status.ToString();
                }

                handler(this, args);
            }
        }
Exemplo n.º 4
0
        public override void OnReadRemoteRssi(BluetoothGatt gatt, int rssi, GattStatus status)
        {
            base.OnReadRemoteRssi(gatt, rssi, status);

            Mvx.Trace("OnReadRemoteRssi: device {0} status {1} value {2}", gatt.Device.Name, status, rssi);

            //ToDo add device id, or some link between this callback and the corresponding device
            var args = new RssiReadEventArgs()
            {
                Rssi = rssi
            };

            switch (status)
            {
            case GattStatus.Failure:
            case GattStatus.InsufficientAuthentication:
            case GattStatus.InsufficientEncryption:
            case GattStatus.InvalidAttributeLength:
            case GattStatus.InvalidOffset:
            case GattStatus.ReadNotPermitted:
            case GattStatus.RequestNotSupported:
            case GattStatus.WriteNotPermitted:
                args.Error = new Exception(status.ToString());
                break;

            case GattStatus.Success:
                break;
            }

            RemoteRssiRead(this, args);
        }
Exemplo n.º 5
0
        public override void OnServicesDiscovered(BluetoothGatt gatt, GattStatus status)
        {
            base.OnServicesDiscovered(gatt, status);

            Mvx.Trace("OnServicesDiscovered: {0}", status.ToString());

            ServicesDiscovered(this, new ServicesDiscoveredEventArgs());
        }
Exemplo n.º 6
0
        public override void OnServicesDiscovered(BluetoothGatt gatt, GattStatus status)
        {
            base.OnServicesDiscovered(gatt, status);

            Console.WriteLine("OnServicesDiscovered: " + status.ToString());

            this.ServicesDiscovered(this, new ServicesDiscoveredEventArgs());
        }
Exemplo n.º 7
0
        public override void OnServicesDiscovered(BluetoothGatt gatt, GattStatus status)
        {
            base.OnServicesDiscovered(gatt, status);

            Trace.Message("OnServicesDiscovered: {0}", status.ToString());

            ServicesDiscovered?.Invoke(this, new ServicesDiscoveredCallbackEventArgs());
        }
Exemplo n.º 8
0
		public override void OnServicesDiscovered (BluetoothGatt gatt, GattStatus status)
		{
			base.OnServicesDiscovered (gatt, status);

			Console.WriteLine ("OnServicesDiscovered: " + status.ToString ());

			this.ServicesDiscovered (this, new ServicesDiscoveredEventArgs ());
		}
Exemplo n.º 9
0
        private Exception GetExceptionFromGattStatus(GattStatus status)
        {
            Exception exception = null;

            switch (status)
            {
            case GattStatus.Failure:
            case GattStatus.InsufficientAuthentication:
            case GattStatus.InsufficientEncryption:
            case GattStatus.InvalidAttributeLength:
            case GattStatus.InvalidOffset:
            case GattStatus.ReadNotPermitted:
            case GattStatus.RequestNotSupported:
            case GattStatus.WriteNotPermitted:
                exception = new Exception(status.ToString());
                break;

            case GattStatus.Success:
                break;
            }

            return(exception);
        }
Exemplo n.º 10
0
        public override void OnReadRemoteRssi(BluetoothGatt gatt, int rssi, GattStatus status)
        {
            base.OnReadRemoteRssi(gatt, rssi, status);

            Trace.Message("OnReadRemoteRssi: device {0} status {1} value {2}", gatt.Device.Name, status, rssi);

            IDevice device;

            if (!_adapter.ConnectedDeviceRegistry.TryGetValue(gatt.Device.Address, out device))
            {
                device = new Device(_adapter, gatt.Device, gatt, this, rssi);
                Trace.Message("Rssi updated for device not in connected list. This should not happen.");
            }

            Exception error = null;

            switch (status)
            {
            case GattStatus.Failure:
            case GattStatus.InsufficientAuthentication:
            case GattStatus.InsufficientEncryption:
            case GattStatus.InvalidAttributeLength:
            case GattStatus.InvalidOffset:
            case GattStatus.ReadNotPermitted:
            case GattStatus.RequestNotSupported:
            case GattStatus.WriteNotPermitted:
                error = new Exception(status.ToString());
                break;

            case GattStatus.Success:
                break;
            }

            var args = new RssiReadCallbackEventArgs(device, error, rssi);

            RemoteRssiRead?.Invoke(this, args);
        }
Exemplo n.º 11
0
            public override void OnServicesDiscovered(BluetoothGatt gatt, GattStatus status)
            {
                base.OnServicesDiscovered(gatt, status);

                Console.WriteLine("OnServicesDiscovered: " + status.ToString());

                //TODO: somehow, we need to tie this directly to the device, rather than for all
                // google's API deisgners are children.

                //TODO: not sure if this gets called after all services have been enumerated or not
                if (!this._parent._services.ContainsKey(gatt.Device))
                {
                    this._parent.Services.Add(gatt.Device, this._parent._connectedDevices [gatt.Device].Services);
                }
                else
                {
                    this._parent._services[gatt.Device] = this._parent._connectedDevices [gatt.Device].Services;
                }

                this._parent.ServiceDiscovered(this, new ServiceDiscoveredEventArgs()
                {
                    Gatt = gatt
                });
            }
Exemplo n.º 12
0
        public override void OnNotificationSent(BluetoothDevice device, [GeneratedEnum] GattStatus status)
        {
            base.OnNotificationSent(device, status);

            SERVERC("OnNotificationSent device=" + device.Address + " status=" + status.ToString());
        }
Exemplo n.º 13
0
        public override void OnPhyUpdate(BluetoothDevice device, [GeneratedEnum] ScanSettingsPhy txPhy, [GeneratedEnum] ScanSettingsPhy rxPhy, [GeneratedEnum] GattStatus status)
        {
            base.OnPhyUpdate(device, txPhy, rxPhy, status);

            SERVERC("OnPhyUpdate device=" + device.Address + " status=" + status.ToString());
        }
Exemplo n.º 14
0
        public override void OnServicesDiscovered(BluetoothGatt gatt, GattStatus status)
        {
            base.OnServicesDiscovered(gatt, status);

            Trace.Message("OnServicesDiscovered: {0}", status.ToString());

            ServicesDiscovered?.Invoke(this, new ServicesDiscoveredCallbackEventArgs());
        }
Exemplo n.º 15
0
        private Exception GetExceptionFromGattStatus(GattStatus status)
        {
            Exception exception = null;
            switch (status)
            {
                case GattStatus.Failure:
                case GattStatus.InsufficientAuthentication:
                case GattStatus.InsufficientEncryption:
                case GattStatus.InvalidAttributeLength:
                case GattStatus.InvalidOffset:
                case GattStatus.ReadNotPermitted:
                case GattStatus.RequestNotSupported:
                case GattStatus.WriteNotPermitted:
                    exception = new Exception(status.ToString());
                    break;
                case GattStatus.Success:
                    break;
            }

            return exception;
        }
Exemplo n.º 16
0
			public override void OnServicesDiscovered (BluetoothGatt gatt, GattStatus status)
			{
				base.OnServicesDiscovered (gatt, status);

				Console.WriteLine ("OnServicesDiscovered: " + status.ToString ());

				//TODO: somehow, we need to tie this directly to the device, rather than for all
				// google's API deisgners are children.

				//TODO: not sure if this gets called after all services have been enumerated or not
				if(!this._parent._services.ContainsKey(gatt.Device))
					this._parent.Services.Add(gatt.Device, this._parent._connectedDevices [gatt.Device].Services);
				else
					this._parent._services[gatt.Device] = this._parent._connectedDevices [gatt.Device].Services;

				this._parent.ServiceDiscovered (this, new ServiceDiscoveredEventArgs () {
					Gatt = gatt
				});
			}
Exemplo n.º 17
0
 public static DroidGattStatus ToNative(this GattStatus status)
 {
     return((DroidGattStatus)Enum.Parse(typeof(DroidGattStatus), status.ToString()));
 }
Exemplo n.º 18
0
        public override void OnServiceAdded([GeneratedEnum] GattStatus status, BluetoothGattService service)
        {
            base.OnServiceAdded(status, service);

            SERVERC("OnServiceAdded service=" + service.Uuid.ToString() + " status=" + status.ToString());
        }