public IBluetoothDeviceInfo[] EndDiscoverDevices(IAsyncResult asyncResult) { AsyncResultDD arDD = (AsyncResultDD)asyncResult; List_IBluetoothDeviceInfo discoverableDevices = arDD.EndInvoke(); DiscoDevsParams args = arDD.BeginParameters; // DEBUG Iff 'known' devices only: we complete immediately (sync'ly), and with null result. if (args.unknown || args.discoverableOnly) { // Result from BeginInquiry callback expected Debug.Assert(discoverableDevices != null, "a1"); Debug.Assert(!arDD.CompletedSynchronously, "a2"); // don't really care however } else { // Null result from BeginDD method expected. Debug.Assert(discoverableDevices == null, "b1"); Debug.Assert(arDD.CompletedSynchronously, "b2"); } // List_IBluetoothDeviceInfo knownDevices = GetKnownRemoteDeviceEntries(); // List_IBluetoothDeviceInfo mergedDevices = BluetoothClient.DiscoverDevicesMerge( args.authenticated, args.remembered, args.unknown, knownDevices, discoverableDevices, args.discoverableOnly, args.discoTime); return(mergedDevices.ToArray()); }
public IAsyncResult BeginDiscoverDevices(int maxDevices, bool authenticated, bool remembered, bool unknown, bool discoverableOnly, AsyncCallback callback, object state, BluetoothClient.LiveDiscoveryCallback liveDiscoHandler, object liveDiscoState) { DateTime discoTime = DateTime.UtcNow; DiscoDevsParams args = new DiscoDevsParams(maxDevices, authenticated, remembered, unknown, discoverableOnly, discoTime); AsyncResultDD arDD = new AsyncResultDD(callback, state, args); // if (unknown || discoverableOnly) // No need to do SLOW Inquiry when just want known remembered. { BeginInquiry(maxDevices, DiscoDevs_InquiryCallback, arDD, liveDiscoHandler, liveDiscoState, args); } else { arDD.SetAsCompleted(null, true); } return(arDD); }
//---- public IAsyncResult BeginInquiry(int maxDevices, TimeSpan inquiryLength, AsyncCallback asyncCallback, Object state, BluetoothClient.LiveDiscoveryCallback liveDiscoHandler, object liveDiscoState, ThreadStart startInquiry, DiscoDevsParams args) { int fakeUseI = maxDevices; AR_Inquiry ar; AR_Inquiry sacAr = null; List_IBluetoothDeviceInfo sacResult = null; lock (_lockInquiry) { if (_inquiryAr != null) { Debug.WriteLine("Merging concurrent DiscoverDevices call into the running one (will get same result list)."); // Just give any new request the same results as the outstanding Inquiry. ar = new AR_Inquiry(asyncCallback, state, args); if (_inquiryAr.IsCompleted) { // This can never occur (is nulled before SAC'd), but leave in anyway... sacAr = ar; sacResult = _inquiryDevices; } else { if (_arInquiryFollowers == null) { _arInquiryFollowers = new List <AR_Inquiry>(); } _arInquiryFollowers.Add(ar); } } else // New inquiry process. { ar = new AR_Inquiry(asyncCallback, state, args); _inquiryAr = ar; _arInquiryFollowers = null; _inquiryDevices = new List_IBluetoothDeviceInfo(); _liveDiscoHandler = liveDiscoHandler; _liveDiscoState = liveDiscoState; bool siSuccess = false; try { startInquiry(); siSuccess = true; } finally { if (!siSuccess) { _inquiryAr = null; } } if (inquiryLength.CompareTo(TimeSpan.Zero) > 0) { var tTmp = TimeSpan.FromMilliseconds( checked (1.5 * inquiryLength.TotalMilliseconds)); System.Threading.ThreadPool.QueueUserWorkItem(InquiryTimeout_Runner, new InquiryTimeoutParams(ar, tTmp)); } } }//lock if (sacAr != null) { sacAr.SetAsCompleted(sacResult, true); } return(ar); }
protected abstract void BeginInquiry(int maxDevices, AsyncCallback callback, object state, BluetoothClient.LiveDiscoveryCallback liveDiscoHandler, object liveDiscoState, DiscoDevsParams args);