/// <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 async Task <Device> ConnectToKnownDeviceAsync(Guid deviceGuid, ConnectParameters connectParameters = default(ConnectParameters), CancellationToken cancellationToken = default(CancellationToken), bool dontThrowExceptionOnNotFound = false) { // Wait for the PoweredOn state await WaitForState(CBCentralManagerState.PoweredOn, cancellationToken, true); if (cancellationToken.IsCancellationRequested) { throw new TaskCanceledException("ConnectToKnownDeviceAsync cancelled"); } //FYI attempted to use tobyte array insetead of string but there was a problem with byte ordering Guid->NSUui var uuid = new NSUuid(deviceGuid.ToString()); Trace.Message($"[Adapter] Attempting connection to {uuid}"); var peripherials = _centralManager.RetrievePeripheralsWithIdentifiers(uuid); var peripherial = peripherials.SingleOrDefault(); if (peripherial == null) { var systemPeripherials = _centralManager.RetrieveConnectedPeripherals(new CBUUID[0]); #if __IOS__ var cbuuid = CBUUID.FromNSUuid(uuid); #endif peripherial = systemPeripherials.SingleOrDefault(p => #if __IOS__ p.UUID.Equals(cbuuid) #else p.Identifier.Equals(uuid) #endif ); if (peripherial == null) { if (dontThrowExceptionOnNotFound == true) { return(null); } throw new DeviceNotFoundException(deviceGuid); } } var device = new Device(this, peripherial, _bleCentralManagerDelegate, peripherial.Name, peripherial.RSSI?.Int32Value ?? 0, new List <AdvertisementRecord>()); await ConnectToDeviceAsync(device, connectParameters, cancellationToken); return(device); }
/// <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, ConnectParameters connectParameters = default(ConnectParameters), CancellationToken cancellationToken = default(CancellationToken)) { // Wait for the PoweredOn state await this.WaitForState(CBCentralManagerState.PoweredOn, cancellationToken, true); if (cancellationToken.IsCancellationRequested) { throw new TaskCanceledException("ConnectToKnownDeviceAsync cancelled"); } //FYI attempted to use tobyte array insetead of string but there was a problem with byte ordering Guid->NSUui NSUuid uuid = new NSUuid(deviceGuid.ToString()); Trace.Message($"[Adapter] Attempting connection to {uuid}"); CBPeripheral[] peripherials = this._centralManager.RetrievePeripheralsWithIdentifiers(uuid); CBPeripheral peripherial = peripherials.SingleOrDefault(); if (peripherial == null) { CBPeripheral[] systemPeripherials = this._centralManager.RetrieveConnectedPeripherals(new CBUUID[0]); CBUUID cbuuid = CBUUID.FromNSUuid(uuid); peripherial = systemPeripherials.SingleOrDefault(p => p.UUID.Equals(cbuuid)); if (peripherial == null) { throw new Exception($"[Adapter] Device {deviceGuid} not found."); } } Device device = new Device(this, peripherial, this._centralManager, peripherial.Name, peripherial.RSSI?.Int32Value ?? 0, new List <AdvertisementRecord>()); await this.ConnectToDeviceAsync(device, connectParameters, cancellationToken); return(device); }
// Overridden form CBCentralManagerDelegate public override void DiscoveredPeripheral(CBCentralManager central, CBPeripheral peripheral, NSDictionary advertisementData, NSNumber RSSI) { string name = null; var values = advertisementData.Values; var data = advertisementData[CBAdvertisement.DataManufacturerDataKey]; if (!AttemptNameFetch(peripheral, advertisementData, out name)) { // Log.E(this, "Failed to resolve peripheral name '" + name + "'. The peripheral will not be presented to the application."); return; } if (!name.IsValidSerialNumber()) { return; } IConnection connection = null; var uuid = CBUUID.FromNSUuid(peripheral.Identifier); if (connectionLookup.ContainsKey(uuid)) { connection = connectionLookup[uuid]; } ISerialNumber sn = null; byte[] packet = null; if (RigadoBroadcastParser.ParseBroadcastPacket(ExtractBroadcastData(advertisementData), out sn, out packet)) { // We received a fully valid broadcast packet. We know that the gauge is an Appion gauge and should resolve it. if (connection == null) { // We have not discovered this device before. Notify the world NotifyDeviceFound(sn, uuid.ToString(), packet, EProtocolVersion.V4); } else { // The connection already exists. Give it a new packet. NotifyDeviceFound(sn, uuid.ToString(), null, EProtocolVersion.V4); connection.lastPacket = packet; connection.lastSeen = DateTime.Now; } } else { // We didn't receive a valid broadcasting packet, but the device may still be ours. if (name.IsValidSerialNumber()) { // See, the device has a valid serial number. sn = name.ParseSerialNumber(); // Check that an iserialnumber was returned if (sn != null) { if (connection == null) { var p = FindProtocolFromDeviceModel(sn.deviceModel); // We have not discovered this device before. Notify the world NotifyDeviceFound(sn, uuid.ToString(), null, p); } else { // The connection already exists. Update the last time that it was seen. var d = ion.deviceManager[sn]; if (d == null) { //Log.E(this, "Failed to get device from device manager"); return; } NotifyDeviceFound(sn, uuid.ToString(), null, d.protocol.version); connection.lastSeen = DateTime.Now; } } } else { Log.D(this, "Ignoring non-appion device: " + name); } } }