コード例 #1
0
ファイル: MacOsBluetooth.cs プロジェクト: trocken2/Eco2
        void UpdatedState(object sender, EventArgs eventArgs)
        {
            Console.Error.WriteLine($"Updated state: {central.State}");
            if (central.State == CBCentralManagerState.PoweredOn)
            {
                central.ConnectedPeripheral       += ConnectedPeripheral;
                central.FailedToConnectPeripheral += FailedToConnectPeripheral;

                if (uuidOfPeripheralToConnectTo != null)
                {
                    var uuid        = new NSUuid(uuidOfPeripheralToConnectTo);
                    var peripherals = central.RetrievePeripheralsWithIdentifiers(uuid);
                    if (peripherals.Length == 1)
                    {
                        Console.Error.WriteLine($"Connecting to known peripheral with UUID {uuid}");
                        var peripheral = peripherals[0];
                        discoveredPeripherals.Add(peripheral);
                        central.ConnectPeripheral(peripheral);
                        return;
                    }
                    else
                    {
                        // Somehow we've lost the peripheral from the list of known peripherals.
                        // We probably need to do handshake all over.
                        firstConnect = true;
                    }
                }

                Console.Error.WriteLine("Scanning for peripherals");
                central.DiscoveredPeripheral += DiscoveredPeripheral;
                central.ScanForPeripherals(new CBUUID[0]);
            }
        }
コード例 #2
0
        /// <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 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)
                {
                    throw new Exception($"[Adapter] Device {deviceGuid} not found.");
                }
            }


            var device = new Device(this, peripherial, _bleCentralManagerDelegate, peripherial.Name, peripherial.RSSI?.Int32Value ?? 0, new List <AdvertisementRecord>());

            await ConnectToDeviceAsync(device, connectParameters, cancellationToken);

            return(device);
        }
コード例 #3
0
 public void RetrievePeripherals()
 {
     if (TestRuntime.CheckXcodeVersion(7, 0))
     {
         // ToString in a CBUUID with true returns the full uuid which can be used to create a NSUuid
         using (var uuid = new NSUuid(heartRateMonitorUUID.ToString(true)))
             mgr.RetrievePeripheralsWithIdentifiers(uuid);
     }
     else
     {
         // that API was deprecated in 7.0 and removed from 9.0
         mgr.RetrievePeripherals(heartRateMonitorUUID);
     }
 }
コード例 #4
0
        public IBluetoothLEDevice GetKnownDevice(string address)
        {
            var peripheral = _centralManager?.RetrievePeripheralsWithIdentifiers(new NSUuid(address)).FirstOrDefault();

            if (peripheral == null)
            {
                return(null);
            }

            var device = new BluetoothLEDevice(_centralManager, peripheral);

            _peripheralMap[peripheral] = device;

            return(device);
        }
コード例 #5
0
 public void RetrievePeripherals()
 {
     if (mgr.State == CBCentralManagerState.Unknown)
     {
         // ensure we do get called
         mgrDelegate.Event.WaitOne();
     }
     if (mgr.State != CBCentralManagerState.PoweredOn)
     {
         Assert.Inconclusive("Bluetooth is off and therefore the test cannot be ran. State == {0}.", mgr.State);
     }
     if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0))
     {
         using (var uuid = new NSUuid("B9401000-F5F8-466E-AFF9-25556B57FE6D"))
             mgr.RetrievePeripheralsWithIdentifiers(uuid);
     }
     else
     {
         // that API was deprecated in 7.0 and removed from 9.0
         using (var uuid = CBUUID.FromString("B9401000-F5F8-466E-AFF9-25556B57FE6D"))
             mgr.RetrievePeripherals(uuid);
     }
 }
コード例 #6
0
        public void RetrievePeripherals()
        {
            if (mgr.State == CBCentralManagerState.Unknown)
            {
                // ensure we do get called
                Assert.IsTrue(mgrDelegate.Event.WaitOne(TimeSpan.FromSeconds(5)), "Initialization");
            }
            if (mgr.State != CBCentralManagerState.PoweredOn)
            {
                Assert.Inconclusive("Bluetooth is off and therefore the test cannot be ran. State == {0}.", mgr.State);
            }

            if (TestRuntime.CheckXcodeVersion(7, 0))
            {
                // ToString in a CBUUID with true returns the full uuid which can be used to create a NSUuid
                using (var uuid = new NSUuid(heartRateMonitorUUID.ToString(true)))
                    mgr.RetrievePeripheralsWithIdentifiers(uuid);
            }
            else
            {
                // that API was deprecated in 7.0 and removed from 9.0
                mgr.RetrievePeripherals(heartRateMonitorUUID);
            }
        }
コード例 #7
0
        public void RetrievePeripherals()
        {
            if (mgr.State == CBCentralManagerState.Unknown)
            {
                // ensure we do get called
                Assert.IsTrue(mgrDelegate.Event.WaitOne(TimeSpan.FromSeconds(5)), "Initialization");
            }
            if (mgr.State != CBCentralManagerState.PoweredOn)
            {
                Assert.Inconclusive("Bluetooth is off and therefore the test cannot be ran. State == {0}.", mgr.State);
            }

            if (TestRuntime.CheckXcodeVersion(7, 0))
            {
                using (var uuid = new NSUuid("B9401000-F5F8-466E-AFF9-25556B57FE6D"))
                    mgr.RetrievePeripheralsWithIdentifiers(uuid);
            }
            else
            {
                // that API was deprecated in 7.0 and removed from 9.0
                using (var uuid = CBUUID.FromString("B9401000-F5F8-466E-AFF9-25556B57FE6D"))
                    mgr.RetrievePeripherals(uuid);
            }
        }
コード例 #8
0
 public IEnumerable <BluetoothDevice> GetBondedDevices()
 {
     _manager.RetrievePeripheralsWithIdentifiers();
     return(_devices);
 }