コード例 #1
0
        public Device(BluetoothDevice nativeDevice, BluetoothGatt gatt,
                      GattCallback gattCallback, int rssi) : base()
        {
            _nativeDevice = nativeDevice;
            _gatt         = gatt;
            _gattCallback = gattCallback;
            _rssi         = rssi;

            // when the services are discovered on the gatt callback, cache them here
            if (_gattCallback != null)
            {
                _gattCallback.ServicesDiscovered += (s, e) =>
                {
                    if (_gatt != null)
                    {
                        var services = _gatt.Services;

                        // Make a copy of the services
                        _services = new List <IService>();
                        foreach (var item in services)
                        {
                            _services.Add(new Service(item, _gatt, _gattCallback));
                        }

                        ServicesDiscovered(this, e);
                    }
                };
            }
        }
コード例 #2
0
ファイル: Device.cs プロジェクト: fburel/Monkey.Robotics
		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) => {
					this._services = new List<IService> ();

					try{  
						
						var services = this._gatt.Services;

						foreach (var item in services) {
							this._services.Add (new Service (item, this._gatt, this._gattCallback));
						}
						this.ServicesDiscovered (this, e); 

					} catch { 
					
					} 

				};
			}
		}
コード例 #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) => {
                    this._services = new List <IService> ();

                    try{
                        var services = this._gatt.Services;

                        foreach (var item in services)
                        {
                            this._services.Add(new Service(item, this._gatt, this._gattCallback));
                        }
                        this.ServicesDiscovered(this, e);
                    } catch {
                    }
                };
            }
        }
コード例 #4
0
        public Adapter(Context appContext)
        {
            ScanTimeout = TimeSpan.FromSeconds(10); // default timeout is 10 seconds
            _appContext = appContext;
            // get a reference to the bluetooth system service
            this._manager = appContext.GetSystemService("bluetooth").JavaCast<BluetoothManager>();
            this._adapter = this._manager.Adapter;

            this._gattCallback = new GattCallback(this);

            this._gattCallback.DeviceConnected += (object sender, DeviceConnectionEventArgs e) =>
            {
                if (ConnectedDevices.Find((BluetoothDevice)e.Device.NativeDevice) == null)
                {
                    _connectedDevices.Add(e.Device);
                    this.DeviceConnected(this, e);
                }
            };

            this._gattCallback.DeviceDisconnected += (object sender, DeviceConnectionEventArgs e) =>
            {
                var device = ConnectedDevices.Find((BluetoothDevice)e.Device.NativeDevice);
                if (device != null)
                {
                    _connectedDevices.Remove(device);
                    this.DeviceDisconnected(this, e);
                }
            };
        }
コード例 #5
0
ファイル: Device.cs プロジェクト: gabornemeth/Monkey.Robotics
		internal Device (BluetoothDevice nativeDevice, BluetoothGatt gatt, 
			GattCallback gattCallback, int rssi) : base ()
		{
			this._nativeDevice = nativeDevice;
            _gatt = gatt;
            SetGattCallback(gattCallback);
			this._rssi = rssi;
		}
コード例 #6
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
					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);
					}
				};
			}
		}
コード例 #7
0
ファイル: Adapter.cs プロジェクト: fburel/Monkey.Robotics
		public Adapter ()
		{
			var appContext = Android.App.Application.Context;
			// get a reference to the bluetooth system service
			this._manager = (BluetoothManager) appContext.GetSystemService("bluetooth");
			this._adapter = this._manager.Adapter;

			this._gattCallback = new GattCallback (this);

			this._gattCallback.DeviceConnected += (object sender, DeviceConnectionEventArgs e) => {
				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(
				this.DeviceDisconnected (this, e);
			};
		}
コード例 #8
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
                    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);
                    }
                };
            }
        }
コード例 #9
0
        public Adapter()
        {
            var appContext = Android.App.Application.Context;

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

            this._gattCallback = new GattCallback(this);

            this._gattCallback.DeviceConnected += (object sender, DeviceConnectionEventArgs e) => {
                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(
                this.DeviceDisconnected(this, e);
            };
        }
コード例 #10
0
ファイル: Device.cs プロジェクト: gabornemeth/Monkey.Robotics
        internal async void Connect(Context context, GattCallback callback)
        {
            if (_gatt != null)
            {
                // connection attempt is already in progress, abort it
                Disconnect();
                await Task.Delay(1500);
            }

            SetGattCallback(callback);
            _gatt = _nativeDevice.ConnectGatt (context, true, callback);
        }
コード例 #11
0
ファイル: Device.cs プロジェクト: gabornemeth/Monkey.Robotics
 internal void SetGattCallback(GattCallback callback)
 {
     // remove event handler from the old callback
     if (_gattCallback != null)
         _gattCallback.ServicesDiscovered -= gattCallback_ServicesDiscovered;
     _gattCallback = callback; // set the current callback
     // when the services are discovered on the gatt callback, cache them here
     if (_gattCallback != null)
         _gattCallback.ServicesDiscovered += gattCallback_ServicesDiscovered;
 }
コード例 #12
0
 public Service(BluetoothGattService nativeService, BluetoothGatt gatt, GattCallback _gattCallback)
 {
     this._nativeService = nativeService;
     this._gatt          = gatt;
     this._gattCallback  = _gattCallback;
 }
コード例 #13
0
ファイル: Service.cs プロジェクト: Roddoric/Monkey.Robotics
		public Service (BluetoothGattService nativeService, BluetoothGatt gatt, GattCallback _gattCallback)
		{
			this._nativeService = nativeService;
			this._gatt = gatt;
			this._gattCallback = _gattCallback;
		}