示例#1
0
		public Device (CBPeripheral nativeDevice)
		{
			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) 
					{
						if(!ServiceExists(s)) 
						{
							Console.WriteLine ("Device.Discovered Service: " + s.Description);
							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("Device: 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 should just be using a notifying collection.
								(item as Service).OnCharacteristicsDiscovered();
							}
						}
					}
				}			
			};
		}
示例#2
0
		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: " + e.Characteristic.Value);
				var c = new Characteristic(e.Characteristic, _parentDevice);
				tcs.SetResult(c);
				_parentDevice.UpdatedCharacterteristicValue -= updated;
			};

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

			return tcs.Task;
		}