public Adapter(CBCentralManager centralManager) { _centralManager = centralManager; _centralManager.DiscoveredPeripheral += (sender, e) => { Trace.Message("DiscoveredPeripheral: {0}, Id: {1}", e.Peripheral.Name, e.Peripheral.Identifier); var name = e.Peripheral.Name; if (e.AdvertisementData.ContainsKey(CBAdvertisement.DataLocalNameKey)) { // iOS caches the peripheral name, so it can become stale (if changing) // keep track of the local name key manually name = ((NSString)e.AdvertisementData.ValueForKey(CBAdvertisement.DataLocalNameKey)).ToString(); } var device = new Device(this, e.Peripheral, name, e.RSSI.Int32Value, ParseAdvertismentData(e.AdvertisementData)); HandleDiscoveredDevice(device); }; _centralManager.UpdatedState += (sender, e) => { Trace.Message("UpdatedState: {0}", _centralManager.State); _stateChanged.Set(); }; _centralManager.ConnectedPeripheral += (sender, e) => { Trace.Message("ConnectedPeripherial: {0}", e.Peripheral.Name); // when a peripheral gets connected, add that peripheral to our running list of connected peripherals var guid = ParseDeviceGuid(e.Peripheral).ToString(); IDevice device; if (_deviceOperationRegistry.TryGetValue(guid, out device)) { _deviceOperationRegistry.Remove(guid); ((Device)device).Update(e.Peripheral); } else { Trace.Message("Device not found in operation registry. Creating a new one."); device = new Device(this, e.Peripheral); } _deviceConnectionRegistry[guid] = device; HandleConnectedDevice(device); }; _centralManager.DisconnectedPeripheral += (sender, e) => { if (e.Error != null) { Trace.Message("Disconnect error {0} {1} {2}", e.Error.Code, e.Error.Description, e.Error.Domain); } // when a peripheral disconnects, remove it from our running list. var id = ParseDeviceGuid(e.Peripheral); var stringId = id.ToString(); IDevice foundDevice; // normal disconnect (requested by user) var isNormalDisconnect = _deviceOperationRegistry.TryGetValue(stringId, out foundDevice); if (isNormalDisconnect) { _deviceOperationRegistry.Remove(stringId); } // remove from connected devices if (_deviceConnectionRegistry.TryGetValue(stringId, out foundDevice)) { _deviceConnectionRegistry.Remove(stringId); } foundDevice = foundDevice ?? new Device(this, e.Peripheral); //make sure all cached services are cleared ((Device)foundDevice).ClearServices(); HandleDisconnectedDevice(isNormalDisconnect, foundDevice); }; _centralManager.FailedToConnectPeripheral += (sender, e) => { var id = ParseDeviceGuid(e.Peripheral); var stringId = id.ToString(); IDevice foundDevice; // remove instance from registry if (_deviceOperationRegistry.TryGetValue(stringId, out foundDevice)) { _deviceOperationRegistry.Remove(stringId); } foundDevice = foundDevice ?? new Device(this, e.Peripheral); HandleConnectionFail(foundDevice, e.Error.Description); }; }
public async void CheckMyDevice(Device device) { await device.UpdateRssiAsync(); }
/// <summary> /// Connects to known device async. /// /// https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/BestPracticesForInteractingWithARemotePeripheralDevice/BestPracticesForInteractingWithARemotePeripheralDevice.html /// /// </summary> /// <returns>The to known device async.</returns> /// <param name="deviceGuid">Device GUID.</param> public override async Task<IDevice> ConnectToKnownDeviceAsync(Guid deviceGuid, CancellationToken cancellationToken = default(CancellationToken)) { // Wait for the PoweredOn state await WaitForState(CBCentralManagerState.PoweredOn, cancellationToken, true); if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException("ConnectToKnownDeviceAsync cancelled"); //ToDo attempted to use tobyte array insetead of string but there was a roblem with byte ordering Guid->NSUui var uuid = new NSUuid(deviceGuid.ToString()); Trace.Message($"[Adapter] Attempting connection to {uuid.ToString()}"); var peripherials = _centralManager.RetrievePeripheralsWithIdentifiers(uuid); var peripherial = peripherials.SingleOrDefault(); if (peripherial == null) { var systemPeripherials = _centralManager.RetrieveConnectedPeripherals(new CBUUID[0]); var cbuuid = CBUUID.FromNSUuid(uuid); peripherial = systemPeripherials.SingleOrDefault(p => p.UUID.Equals(cbuuid)); if (peripherial == null) throw new Exception($"[Adapter] Device {deviceGuid} not found."); } var device = new Device(this, peripherial, peripherial.Name, peripherial.RSSI?.Int32Value ?? 0, new List<AdvertisementRecord>()); await ConnectToDeviceAsync(device, false, cancellationToken); return device; }