Example #1
0
		public Device (CBPeripheral nativeDevice, NSString localName = null)
		{
			this._nativeDevice = nativeDevice;

			this._nativeDevice.DiscoveredService += (object sender, NSErrorEventArgs e) => {
				// why we have to do this check is beyond me. if a service has been discovered, the collection
				// shouldn't be null, but sometimes it is. le sigh, apple.
				if (this._nativeDevice.Services != null) {
					foreach (CBService s in this._nativeDevice.Services) {
						Console.WriteLine ("Device.Discovered Service: " + s.Description);
						if(!ServiceExists(s)) {
							this._services.Add (new Service(s, this._nativeDevice));
						}
					}
					this.ServicesDiscovered(this, new EventArgs());
				}
			};

			#if __UNIFIED__
			// fixed for Unified https://bugzilla.xamarin.com/show_bug.cgi?id=14893
			this._nativeDevice.DiscoveredCharacteristic += (object sender, CBServiceEventArgs e) => {
			#else
			//BUGBUG/TODO: this event is misnamed in our SDK
			this._nativeDevice.DiscoverCharacteristic += (object sender, CBServiceEventArgs e) => {
			#endif
				Console.WriteLine ("Device.Discovered Characteristics.");
				//loop through each service, and update the characteristics
				foreach (CBService srv in ((CBPeripheral)sender).Services) {
					// if the service has characteristics yet
					if(srv.Characteristics != null) {

						// locate the our new service
						foreach (var item in this.Services) {
							// if we found the service
							if (item.ID == Service.ServiceUuidToGuid(srv.UUID) ) {
								item.Characteristics.Clear();

								// add the discovered characteristics to the particular service
								foreach (var characteristic in srv.Characteristics) {
									Console.WriteLine("Characteristic: " + characteristic.Description);
									Characteristic newChar = new Characteristic(characteristic, _nativeDevice);
									item.Characteristics.Add(newChar);
								}
								// inform the service that the characteristics have been discovered
								// TODO: really, we shoul just be using a notifying collection.
								(item as Service).OnCharacteristicsDiscovered();
							}
						}
					}
				}			
			};

			_name = localName == null ? _nativeDevice.Name : localName.ToString();


		}
		public Task<ICharacteristic> ReadAsync() 
		{
			var tcs = new TaskCompletionSource<ICharacteristic>();

			if (!CanRead) {
				throw new InvalidOperationException ("Characteristic does not support READ");
			}
			EventHandler<CBCharacteristicEventArgs> updated = null;
			updated = (object sender, CBCharacteristicEventArgs e) => {
				Console.WriteLine(".....UpdatedCharacterteristicValue");
				var c = new Characteristic(e.Characteristic, _parentDevice);
				tcs.SetResult(c);
				_parentDevice.UpdatedCharacterteristicValue -= updated;
			};

			_parentDevice.UpdatedCharacterteristicValue += updated;
			Console.WriteLine(".....ReadAsync");
			_parentDevice.ReadValue (_nativeCharacteristic);

			return tcs.Task;
		}
Example #3
0
        public Device(CBPeripheral nativeDevice, NSString localName = null)
        {
            this._nativeDevice = nativeDevice;

            this._nativeDevice.DiscoveredService += (object sender, NSErrorEventArgs e) => {
                // why we have to do this check is beyond me. if a service has been discovered, the collection
                // shouldn't be null, but sometimes it is. le sigh, apple.
                if (this._nativeDevice.Services != null)
                {
                    foreach (CBService s in this._nativeDevice.Services)
                    {
                        Console.WriteLine("Device.Discovered Service: " + s.Description);
                        if (!ServiceExists(s))
                        {
                            this._services.Add(new Service(s, this._nativeDevice));
                        }
                    }
                    this.ServicesDiscovered(this, new EventArgs());
                }
            };

                        #if __UNIFIED__
            // fixed for Unified https://bugzilla.xamarin.com/show_bug.cgi?id=14893
            this._nativeDevice.DiscoveredCharacteristic += (object sender, CBServiceEventArgs e) => {
                        #else
            //BUGBUG/TODO: this event is misnamed in our SDK
            this._nativeDevice.DiscoverCharacteristic += (object sender, CBServiceEventArgs e) => {
                        #endif
                Console.WriteLine("Device.Discovered Characteristics.");
                //loop through each service, and update the characteristics
                foreach (CBService srv in ((CBPeripheral)sender).Services)
                {
                    // if the service has characteristics yet
                    if (srv.Characteristics != null)
                    {
                        // locate the our new service
                        foreach (var item in this.Services)
                        {
                            // if we found the service
                            if (item.ID == Service.ServiceUuidToGuid(srv.UUID))
                            {
                                item.Characteristics.Clear();

                                // add the discovered characteristics to the particular service
                                foreach (var characteristic in srv.Characteristics)
                                {
                                    Console.WriteLine("Characteristic: " + characteristic.Description);
                                    Characteristic newChar = new Characteristic(characteristic, _nativeDevice);
                                    item.Characteristics.Add(newChar);
                                }
                                // inform the service that the characteristics have been discovered
                                // TODO: really, we shoul just be using a notifying collection.
                                (item as Service).OnCharacteristicsDiscovered();
                            }
                        }
                    }
                }
            };

            _name = localName == null ? _nativeDevice.Name : localName.ToString();
        }