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"); if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop) { Console.WriteLine("Changing connection priority to balanced"); gatt.RequestConnectionPriority(BluetoothGatt.ConnectionPriorityBalanced); } this.DeviceDisconnected (this, new DeviceConnectionEventArgs () { Device = device }); break; // connecting case ProfileState.Connecting: Console.WriteLine ("Connecting"); break; // connected case ProfileState.Connected: Console.WriteLine ("Connected"); if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop) { Console.WriteLine("Changing connection priority to high"); gatt.RequestConnectionPriority(BluetoothGatt.ConnectionPriorityHigh); } this.DeviceConnected (this, new DeviceConnectionEventArgs () { Device = device }); break; // disconnecting case ProfileState.Disconnecting: Console.WriteLine ("Disconnecting"); break; } }
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, RSSI = rssi }); }
protected Adapter () { this._central = new CBCentralManager (DispatchQueue.CurrentQueue); _central.DiscoveredPeripheral += (object sender, CBDiscoveredPeripheralEventArgs e) => { Console.WriteLine ("DiscoveredPeripheral Name: " + e.Peripheral.Name); Console.WriteLine("DiscoveredPeripheral RSSI: " + e.RSSI); Device d = new Device(e.Peripheral); if(!ContainsDevice(this._discoveredDevices, e.Peripheral ) ){ this._discoveredDevices.Add (d); this.DeviceDiscovered(this, new DeviceDiscoveredEventArgs() { Device = d , RSSI = e.RSSI.Int32Value }); } }; _central.UpdatedState += (object sender, EventArgs e) => { Console.WriteLine ("UpdatedState: " + _central.State); stateChanged.Set (); BluetoothLEStates LEState = BluetoothLEStates.Unknown; switch (_central.State) { case CBCentralManagerState.PoweredOn: LEState = BluetoothLEStates.PoweredOn; break; case CBCentralManagerState.PoweredOff: LEState = BluetoothLEStates.PoweredOff; break; case CBCentralManagerState.Resetting: LEState = BluetoothLEStates.Resetting; break; case CBCentralManagerState.Unauthorized: LEState = BluetoothLEStates.Unauthorized; break; case CBCentralManagerState.Unknown: LEState = BluetoothLEStates.Unknown; break; case CBCentralManagerState.Unsupported: LEState = BluetoothLEStates.Unsupported; break; default: LEState = BluetoothLEStates.Unsupported; break; } this.BluetoothStateUpdated(this, new BluetoothStateEventArgs() {BTState = LEState}); }; _central.ConnectedPeripheral += (object sender, CBPeripheralEventArgs e) => { Console.WriteLine ("ConnectedPeripheral: " + e.Peripheral.Name); this.peripheral = e.Peripheral; // when a peripheral gets connected, add that peripheral to our running list of connected peripherals if(!ContainsDevice(this._connectedDevices,this.peripheral ) ){ Device d = new Device(e.Peripheral); this._connectedDevices.Add (new Device(this.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 Console.WriteLine ("Failed to connect to Peripheral: " + e.Peripheral.Name); this.DeviceFailedToConnect(this, new DeviceConnectionEventArgs() { Device = new Device (e.Peripheral), ErrorMessage = e.Error.Description }); }; }