Ejemplo n.º 1
0
		public Adapter ()
		{
			var appContext = Application.Context;
			// get a reference to the bluetooth system service
			this._manager = (BluetoothManager) appContext.GetSystemService("bluetooth");
			this._adapter = this._manager.Adapter;

			if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
			{
				this.setLollipopProperty();
			}

			this._gattCallback = new GattCallback (this);

			this._gattCallback.DeviceConnected += (object sender, DeviceConnectionEventArgs e) => {
				Console.WriteLine("Device Connected: "+ e.Device.Name);

				this._connectedDevices.Add ( e.Device);
				this.DeviceConnected (this, e);
			};

			this._gattCallback.DeviceDisconnected += (object sender, DeviceConnectionEventArgs e) => {
				// TODO: remove the disconnected device from the _connectedDevices list
				// i don't think this will actually work, because i'm created a new underlying device here.
				if(this._connectedDevices.Contains(e.Device))
				{
					this._connectedDevices.Remove(e.Device);
				}
				this.DeviceDisconnected (this, e);
			};

		}
Ejemplo n.º 2
0
        public Adapter()
        {
            var appContext = Application.Context;

            // get a reference to the bluetooth system service
            this._manager = (BluetoothManager)appContext.GetSystemService("bluetooth");
            this._adapter = this._manager.Adapter;

            if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
            {
                this.setLollipopProperty();
            }

            this._gattCallback = new GattCallback(this);

            this._gattCallback.DeviceConnected += (object sender, DeviceConnectionEventArgs e) => {
                Console.WriteLine("Device Connected: " + e.Device.Name);

                this._connectedDevices.Add(e.Device);
                this.DeviceConnected(this, e);
            };

            this._gattCallback.DeviceDisconnected += (object sender, DeviceConnectionEventArgs e) => {
                // TODO: remove the disconnected device from the _connectedDevices list
                // i don't think this will actually work, because i'm created a new underlying device here.
                if (this._connectedDevices.Contains(e.Device))
                {
                    this._connectedDevices.Remove(e.Device);
                }
                this.DeviceDisconnected(this, e);
            };
        }
Ejemplo n.º 3
0
		public Device (BluetoothDevice nativeDevice, BluetoothGatt gatt, 
			GattCallback gattCallback, int rssi) : base ()
		{
			this._nativeDevice = nativeDevice;
			this._gatt = gatt;
			this._gattCallback = gattCallback;
			this._rssi = rssi;

			// when the services are discovered on the gatt callback, cache them here
			if (this._gattCallback != null) {
				this._gattCallback.ServicesDiscovered += (s, e) => {
					var services = this._gatt.Services;
					this._services = new List<IService> ();
					foreach (var item in services) {
						this._services.Add (new Service (item, this._gatt, this._gattCallback));
						foreach (var itm in item.Characteristics)
						{
							Console.WriteLine("Characteristic: " + itm.Uuid.ToString());
						}
					}
					this.ServicesDiscovered (this, e);
				};


			}
		}
Ejemplo n.º 4
0
		public Characteristic (BluetoothGattCharacteristic nativeCharacteristic, BluetoothGatt gatt, GattCallback gattCallback)
		{
			this._nativeCharacteristic = nativeCharacteristic;
			this._gatt = gatt;
			this._gattCallback = gattCallback;

			if (this._gattCallback != null) {
				// wire up the characteristic value updating on the gattcallback
				this._gattCallback.CharacteristicValueUpdated += (object sender, CharacteristicReadEventArgs e) => {
					// it may be other characteristics, so we need to test
					Console.WriteLine("Value updated for Characteristic ID-: "+ e.Characteristic.ID);
					//if(e.Characteristic.ID == this.ID) {
						// update our underlying characteristic (this one will have a value)
						//TODO: is this necessary? probably the underlying reference is the same.
						//this._nativeCharacteristic = e.Characteristic;

						this.ValueUpdated (this, e);
					//}
				};
			}
		}
Ejemplo n.º 5
0
        public Characteristic(BluetoothGattCharacteristic nativeCharacteristic, BluetoothGatt gatt, GattCallback gattCallback)
        {
            this._nativeCharacteristic = nativeCharacteristic;
            this._gatt         = gatt;
            this._gattCallback = gattCallback;

            if (this._gattCallback != null)
            {
                // wire up the characteristic value updating on the gattcallback
                this._gattCallback.CharacteristicValueUpdated += (object sender, CharacteristicReadEventArgs e) => {
                    // it may be other characteristics, so we need to test
                    Console.WriteLine("Value updated for Characteristic ID-: " + e.Characteristic.ID);
                    //if(e.Characteristic.ID == this.ID) {
                    // update our underlying characteristic (this one will have a value)
                    //TODO: is this necessary? probably the underlying reference is the same.
                    //this._nativeCharacteristic = e.Characteristic;

                    this.ValueUpdated(this, e);
                    //}
                };
            }
        }
Ejemplo n.º 6
0
        public Device(BluetoothDevice nativeDevice, BluetoothGatt gatt,
                      GattCallback gattCallback, int rssi) : base()
        {
            this._nativeDevice = nativeDevice;
            this._gatt         = gatt;
            this._gattCallback = gattCallback;
            this._rssi         = rssi;

            // when the services are discovered on the gatt callback, cache them here
            if (this._gattCallback != null)
            {
                this._gattCallback.ServicesDiscovered += (s, e) => {
                    var services = this._gatt.Services;
                    this._services = new List <IService> ();
                    foreach (var item in services)
                    {
                        this._services.Add(new Service(item, this._gatt, this._gattCallback));
                    }
                    this.ServicesDiscovered(this, e);
                };
            }
        }
Ejemplo n.º 7
0
		public Service (BluetoothGattService nativeService, BluetoothGatt gatt, GattCallback _gattCallback)
		{
			this._nativeService = nativeService;
			this._gatt = gatt;
			this._gattCallback = _gattCallback;
		}
Ejemplo n.º 8
0
 public Service(BluetoothGattService nativeService, BluetoothGatt gatt, GattCallback _gattCallback)
 {
     this._nativeService = nativeService;
     this._gatt          = gatt;
     this._gattCallback  = _gattCallback;
 }