/// <summary> /// Discovers accessible Bluetooth devices and returns their names and addresses. /// This method does not block the calling thread. /// </summary> /// - /// <remarks> /// <para>See <see cref="M:InTheHand.Net.Sockets.BluetoothClient.DiscoverDevices(System.Int32,System.Boolean,System.Boolean,System.Boolean,System.Boolean)"/> /// for more information. /// </para> /// <para>The devices are presented in the <see cref="E:InTheHand.Net.Bluetooth.BluetoothComponent.DiscoverDevicesComplete"/> /// and <see cref="E:InTheHand.Net.Bluetooth.BluetoothComponent.DiscoverDevicesProgress"/> events. /// </para> /// </remarks> /// - /// <param name="maxDevices">The maximum number of devices to get information about. /// </param> /// <param name="authenticated">True to return previously authenticated/paired devices. /// </param> /// <param name="remembered">True to return remembered devices. /// </param> /// <param name="unknown">True to return previously unknown devices. /// </param> /// <param name="discoverableOnly">True to return only the devices that /// are in range, and in discoverable mode. See the remarks section. /// </param> /// <param name="state">A user-defined object that is passed to the method /// invoked when the asynchronous operation completes. /// </param> /// - /// <returns>An array of BluetoothDeviceInfo objects describing the devices discovered.</returns> public void DiscoverDevicesAsync(int maxDevices, bool authenticated, bool remembered, bool unknown, bool discoverableOnly, object state) { AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(state); // Provide the remembered devices immediately DoRemembered(authenticated, remembered, discoverableOnly, asyncOp); // #if PRE_V2_4 if (discoverableOnly) { throw new ArgumentException("Flag 'discoverableOnly' is not supported in this version.", "discoverableOnly"); } FuncDiscoDevs dlgt = new FuncDiscoDevs(m_cli.DiscoverDevices); FuncDiscoDevs exist = Interlocked.CompareExchange(ref m_dlgt, dlgt, null); if (exist != null) { throw new InvalidOperationException("Only support one concurrent operation."); } dlgt.BeginInvoke(maxDevices, authenticated, remembered, unknown, //discoverableOnly, HandleDiscoComplete, asyncOp); #else m_cli.BeginDiscoverDevices(maxDevices, authenticated, remembered, unknown, discoverableOnly, HandleDiscoComplete, asyncOp, HandleDiscoNewDevice, asyncOp); #endif }
private void HandleDiscoComplete(IAsyncResult ar) { AsyncOperation asyncOp = (AsyncOperation)ar.AsyncState; DiscoverDevicesEventArgs e; try { #if PRE_V2_4 FuncDiscoDevs dlgt = Interlocked.Exchange(ref m_dlgt, null); BluetoothDeviceInfo[] arr = dlgt.EndInvoke(ar); #else BluetoothDeviceInfo[] arr = m_cli.EndDiscoverDevices(ar); #endif e = new DiscoverDevicesEventArgs(arr, asyncOp.UserSuppliedState); } catch (Exception ex) { e = new DiscoverDevicesEventArgs(ex, asyncOp.UserSuppliedState); // TO-DO ?? Set Cancelled if disposed? } // SendOrPostCallback cb = delegate(object args) { OnDiscoveryComplete((DiscoverDevicesEventArgs)args); }; asyncOp.PostOperationCompleted(cb, e); }