示例#1
0
		public override void OnConnectionStateChange (BluetoothGatt gatt, GattStatus status, ProfileState newState)
		{
			Console.WriteLine ("OnConnectionStateChange: ");
			base.OnConnectionStateChange (gatt, status, newState);

			//TODO: need to pull the cached RSSI in here, or read it (requires the callback)
			Device device = new Device (gatt.Device, gatt, this, 0);

			switch (newState) {
			// disconnected
			case ProfileState.Disconnected:
				Console.WriteLine ("disconnected");
				this.DeviceDisconnected (this, new DeviceConnectionEventArgs () { Device = device });
				break;
				// connecting
			case ProfileState.Connecting:
				Console.WriteLine ("Connecting");
				break;
				// connected
			case ProfileState.Connected:
				Console.WriteLine ("Connected");
				this.DeviceConnected (this, new DeviceConnectionEventArgs () { Device = device });
				break;
				// disconnecting
			case ProfileState.Disconnecting:
				Console.WriteLine ("Disconnecting");
				break;
			}
		}
示例#2
0
		protected Adapter ()
		{
			this._central = new CBCentralManager (DispatchQueue.CurrentQueue);

			_central.DiscoveredPeripheral += (object sender, CBDiscoveredPeripheralEventArgs e) => {
				Console.WriteLine ("DiscoveredPeripheral: " + e.Peripheral.Name);
				Device d = new Device(e.Peripheral);
				if(!ContainsDevice(this._discoveredDevices, e.Peripheral ) ){
					this._discoveredDevices.Add (d);
					this.DeviceDiscovered(this, new DeviceDiscoveredEventArgs() { Device = d });
				}
			};

			_central.UpdatedState += (object sender, EventArgs e) => {
				Console.WriteLine ("UpdatedState: " + _central.State);
				stateChanged.Set ();
			};


			_central.ConnectedPeripheral += (object sender, CBPeripheralEventArgs e) => {
				Console.WriteLine ("ConnectedPeripheral: " + e.Peripheral.Name);

				// when a peripheral gets connected, add that peripheral to our running list of connected peripherals
				if(!ContainsDevice(this._connectedDevices, e.Peripheral ) ){
					Device d = new Device(e.Peripheral);
					this._connectedDevices.Add (new Device(e.Peripheral));
					// raise our connected event
					this.DeviceConnected ( sender, new DeviceConnectionEventArgs () { Device = d } );
				}			
			};

			_central.DisconnectedPeripheral += (object sender, CBPeripheralErrorEventArgs e) => {
				Console.WriteLine ("DisconnectedPeripheral: " + e.Peripheral.Name);

				// when a peripheral disconnects, remove it from our running list.
				IDevice foundDevice = null;
				foreach (var d in this._connectedDevices) {
					if (d.ID == Guid.ParseExact(e.Peripheral.Identifier.AsString(), "d"))
						foundDevice = d;
				}
				if (foundDevice != null)
					this._connectedDevices.Remove(foundDevice);

				// raise our disconnected event
				this.DeviceDisconnected (sender, new DeviceConnectionEventArgs() { Device = new Device(e.Peripheral) });
			};

			_central.FailedToConnectPeripheral += (object sender, CBPeripheralErrorEventArgs e) => {
				// raise the failed to connect event
				this.DeviceFailedToConnect(this, new DeviceConnectionEventArgs() { 
					Device = new Device (e.Peripheral),
					ErrorMessage = e.Error.Description
				});
			};

		}
		public override void OnConnectionStateChange (BluetoothGatt gatt, GattStatus status, ProfileState newState)
		{
			Console.WriteLine ("OnConnectionStateChange: ");

			//TODO: need to pull the cached RSSI in here, or read it (requires the callback)
            var device = _adapter.DiscoveredDevices.Find(gatt.Device);
            if (device == null)
            {
                Debug.WriteLine(string.Format("Device {0} has not been discovered yet.", gatt.Device.Name));
                device = new Device(gatt.Device, gatt, this, 0);
                _adapter.DiscoveredDevices.Add(device);
            }

            switch (newState)
            {
                // disconnected
                case ProfileState.Disconnected:
                    Console.WriteLine("Disconnected.");
                    device.Disconnect();
                    OnDeviceDisconnected(device);
                    break;
                // connecting
                case ProfileState.Connecting:
                    Console.WriteLine("Connecting");
                    device.SetState(DeviceState.Connecting);
                    break;
                // connected
                case ProfileState.Connected:
                    Console.WriteLine("Connected");
                    device.SetState(DeviceState.Connected);
                    OnDeviceConnected(device);
                    break;
                // disconnecting
                case ProfileState.Disconnecting:
                    Console.WriteLine("Disconnecting");
                    device.SetState(DeviceState.Disconnected);
                    break;
            }
		}
示例#4
0
		public void OnLeScan (BluetoothDevice bleDevice, int rssi, byte[] scanRecord)
		{
			Console.WriteLine ("Adapter.LeScanCallback: " + bleDevice.Name);
			// TODO: for some reason, this doesn't work, even though they have the same pointer,
			// it thinks that the item doesn't exist. so i had to write my own implementation
//			if(!this._discoveredDevices.Contains(device) ) {
//				this._discoveredDevices.Add (device );
//			}
			Device device = new Device (bleDevice, null, null, rssi);

			if (!DeviceExistsInDiscoveredList (bleDevice)) {
				
				this._discoveredDevices.Add	(device);

				var parsedRecords = scanRecord;

				try {

					parsedRecords = scanRecord.Skip (34).Take (7).ToArray();
					
				} catch (Exception) {
					
				}


				this.DeviceDiscovered (this, new DeviceDiscoveredEventArgs { Device = device, RSSI = rssi, ScanRecords = parsedRecords });
			}
		}
示例#5
0
		public void OnLeScan (BluetoothDevice bleDevice, int rssi, byte[] scanRecord)
		{
			Console.WriteLine ("Adapter.LeScanCallback: " + bleDevice.Name);
			// TODO: for some reason, this doesn't work, even though they have the same pointer,
			// it thinks that the item doesn't exist. so i had to write my own implementation
//			if(!this._discoveredDevices.Contains(device) ) {
//				this._discoveredDevices.Add (device );
//			}
			Device device = new Device (bleDevice, null, null, rssi);

			if (!DeviceExistsInDiscoveredList (bleDevice))
				this._discoveredDevices.Add	(device);
			// TODO: in the cross platform API, cache the RSSI
			// TODO: shouldn't i only raise this if it's not already in the list?
			this.DeviceDiscovered (this, new DeviceDiscoveredEventArgs { Device = device });
		}
示例#6
0
		public Adapter ()
		{
			this._central = new CBCentralManager (DispatchQueue.CurrentQueue);

			_central.DiscoveredPeripheral += (object sender, CBDiscoveredPeripheralEventArgs e) => {
				Console.WriteLine ("DiscoveredPeripheral: " + e.Peripheral.Name);

				NSString localName = null;

				try {
					localName = e.AdvertisementData[CBAdvertisement.DataLocalNameKey] as NSString;
				} catch {
					localName = new NSString(e.Peripheral.Name);
				}

				Device d = new Device(e.Peripheral, localName);

				if(!ContainsDevice(this._discoveredDevices, e.Peripheral ) ){

					byte[] scanRecord = null;


					try {

						Console.WriteLine ("ScanRecords: " + e.AdvertisementData.ToString());

						NSError error = null;

						var soft = Clean(e.AdvertisementData);

						Console.WriteLine ("ScanRecords cleaned: " + soft.ToString());

						var binFormatter = new BinaryFormatter();
						var mStream = new MemoryStream();
						binFormatter.Serialize(mStream, soft);

						//This gives you the byte array.



						scanRecord = mStream.ToArray();
						 
					} catch (Exception exception)
					{
						Console.WriteLine ("ScanRecords to byte[] failed");
					}
					finally {
						this._discoveredDevices.Add (d);
						this.DeviceDiscovered(this, new DeviceDiscoveredEventArgs() { Device = d, RSSI = (int)e.RSSI, ScanRecords = scanRecord });
					}

				}
			};

			_central.UpdatedState += (object sender, EventArgs e) => {
				Console.WriteLine ("UpdatedState: " + _central.State);
				stateChanged.Set ();
			};


			_central.ConnectedPeripheral += (object sender, CBPeripheralEventArgs e) => {
				Console.WriteLine ("ConnectedPeripheral: " + e.Peripheral.Name);

				// when a peripheral gets connected, add that peripheral to our running list of connected peripherals
				if(!ContainsDevice(this._connectedDevices, e.Peripheral ) ){
					Device d = new Device(e.Peripheral);
					this._connectedDevices.Add (new Device(e.Peripheral));
					// raise our connected event
					this.DeviceConnected ( sender, new DeviceConnectionEventArgs () { Device = d } );
				}			
			};

			_central.DisconnectedPeripheral += (object sender, CBPeripheralErrorEventArgs e) => {
				Console.WriteLine ("DisconnectedPeripheral: " + e.Peripheral.Name);

				// when a peripheral disconnects, remove it from our running list.
				IDevice foundDevice = null;
				foreach (var d in this._connectedDevices) {
					if (d.ID == Guid.ParseExact(e.Peripheral.Identifier.AsString(), "d"))
						foundDevice = d;
				}
				if (foundDevice != null)
					this._connectedDevices.Remove(foundDevice);

				// raise our disconnected event
				this.DeviceDisconnected (sender, new DeviceConnectionEventArgs() { Device = new Device(e.Peripheral) });
			};

			_central.FailedToConnectPeripheral += (object sender, CBPeripheralErrorEventArgs e) => {
				// raise the failed to connect event
				this.DeviceFailedToConnect(this, new DeviceConnectionEventArgs() { 
					Device = new Device (e.Peripheral),
					ErrorMessage = e.Error.Description
				});
			};


		}
示例#7
0
        public void OnLeScan(BluetoothDevice bleDevice, int rssi, byte[] scanRecord)
        {
            Console.WriteLine("Adapter.LeScanCallback: " + bleDevice.Name);
            // TODO: for some reason, this doesn't work, even though they have the same pointer,
            // it thinks that the item doesn't exist. so i had to write my own implementation
            //			if(!this._discoveredDevices.Contains(device) ) {
            //				this._discoveredDevices.Add (device );
            //			}

            if (DiscoveredDevices.Find(bleDevice) == null)
            {
                var device = new Device(bleDevice, null, null, rssi);
                this._discoveredDevices.Add(device);
                // TODO: in the cross platform API, cache the RSSI
                this.DeviceDiscovered(this, new DeviceDiscoveredEventArgs { Device = device });
            }
        }