/// <summary> /// Instruct Spokes object to disconnect from Spokes runtime engine and unregister its /// session in Spokes. /// </summary> public void Disconnect() { DetachDevice(); try { if (m_comSession != null) { if (m_sessionEvents != null) { // release session events m_sessionEvents.onCallRequested -= m_sessionEvents_CallRequested; m_sessionEvents.onCallStateChanged -= m_sessionEvents_CallStateChanged; Marshal.ReleaseComObject(m_sessionEvents); m_sessionEvents = null; } // unregister session if (m_sessionManagerEvents != null) { m_sessionManagerEvents.onCallStateChanged -= m_sessionComManager_CallStateChanged; m_sessionManagerEvents.onDeviceStateChanged -= m_sessionComManager_DeviceStateChanged; } //#if DEBUG // NOTE this is failing in Spokes 3.0 // TODO so for now disable it in Release version // so it does not crash when exiting! m_sessionComManager.Unregister((COMSession)m_comSession); //#endif Marshal.ReleaseComObject(m_comSession); m_comSession = null; } if (m_sessionComManager != null) { Marshal.ReleaseComObject(m_sessionComManager); m_sessionComManager = null; } } catch (Exception e) { DebugPrint(MethodInfo.GetCurrentMethod().Name, "Exception caught in disconnect: " + e.ToString()); } }
/// <summary> /// Instruct Spokes object to connect to Spokes runtime engine and register itself /// so that it can begin to communicate with the attached Plantronics device. /// </summary> /// <param name="SessionName">Optional name of your appplication's session within Spokes runtime engine. If omitted it will default to "COM Session".</param> public bool Connect(string SessionName = "COM Session") { m_sessionName = SessionName; if (!IsSpokesInstalled()) { DebugPrint(MethodInfo.GetCurrentMethod().Name, "FATAL ERROR: cannot connect if Spokes COMSessionManager/SessionCOMManager class is not registered! Spokes not installed (or wrong major version installed for this Spokes Wrapper)!"); throw new Exception("Cannot connect if Spokes COM SessionManager class is not registered! Plantronics Hub not installed!"); } if (isConnected) return true; DeviceCapabilities = new SpokesDeviceCaps(false, false, false, false, false, false, false, false); // we don't yet know what the capabilities are OnCapabilitiesChanged(EventArgs.Empty); bool success = false; try { //////////////////////////////////////////////////////////////////////////////////////// // create session manager, and attach to session manager events m_sessionComManager = new COMSessionManager(); m_sessionManagerEvents = m_sessionComManager as ICOMSessionManagerEvents_Event; if (m_sessionManagerEvents != null) { m_sessionManagerEvents.onCallStateChanged += m_sessionComManager_CallStateChanged; m_sessionManagerEvents.onDeviceStateChanged += m_sessionComManager_DeviceStateChanged; } else success = false; //////////////////////////////////////////////////////////////////////////////////////// // register session to spokes m_sessionComManager.Register(SessionName, out m_comSession); if (m_comSession != null) { // attach to session call events m_sessionEvents = m_comSession as ICOMCallEvents_Event; if (m_sessionEvents != null) { m_sessionEvents.onCallRequested += m_sessionEvents_CallRequested; m_sessionEvents.onCallStateChanged += m_sessionEvents_CallStateChanged; } else success = false; //////////////////////////////////////////////////////////////////////////////////////// // Attach to active device and print all device information // and registers for proximity (if supported by device) AttachDevice(); // note: with latest Hub 3.9 it is hanging here is Hub was only started as a result of my COM request!!!!! success = true; } } catch (System.Exception e) { success = false; throw new Exception("Failed to connect to Spokes", e); } return success; }