protected override void Dispose(bool disposing) { _state = State.Closed; try { var hConn = _hConn; if (hConn != null) { _hConn = null; BtSdkError ret = _factory.Api.Btsdk_Disconnect(hConn.Value); Debug.Assert(ret == BtSdkError.OK, "Btsdk_Disconnect ret: " + ret); } // Unblock any Reader. try { _received.Set(); } catch (ObjectDisposedException) { Debug.Assert(_state == State.PeerDidClose || _state == State.Closed, "_received event already closed but unexpected state: " + _state); } _received.Close(); } finally { //Debug.Assert(base.CanRead, "CanRead before"); //Debug.Assert(base.CanWrite, "CanWrite before"); base.Dispose(disposing); //Debug.Assert(!base.CanRead, "CanRead after"); //Debug.Assert(!base.CanWrite, "CanWrite after"); } }
internal BlueSoleilSocketException(BtSdkError bsError, int socketErr) : base(socketErr) { int iError = (int)bsError; Set(iError); }
//-- void PlayConnectSppSvc() { BTCONNHDL s_currSPPConnHdl = StackConsts.BTSDK_INVALID_HANDLE; BTDEVHDL s_currRmtSppDevHdl = StackConsts.BTSDK_INVALID_HANDLE; UInt32 s_ComSerialNum = 0; //---- BtSdkError ulRet = 0; UInt32 osComPort; // const UInt32 UsageTypeConst = 1; const StackConsts.COMM_SET flags = StackConsts.COMM_SET.Record | StackConsts.COMM_SET.UsageType; const UInt16 BTSDK_CLS_SERIAL_PORT = 0x1101; const int Timeout = 2200; // s_ComSerialNum = _factory.Api.Btsdk_GetASerialNum(); _factory.Api.Btsdk_PlugInVComm(s_ComSerialNum, out osComPort, UsageTypeConst, flags, Timeout); ulRet = _factory.Api.Btsdk_InitCommObj(checked ((byte)osComPort), BTSDK_CLS_SERIAL_PORT); // if (ulRet != 0) { var sppStru = new Structs.BtSdkSPPConnParamStru(osComPort); BtSdkError ret = _factory.Api.Btsdk_ConnectEx(s_currRmtSppDevHdl, BTSDK_CLS_SERIAL_PORT, ref sppStru, out s_currSPPConnHdl); if (ret != 0) { Console.WriteLine("Connect remote SPP Service with local COM{0}\n", osComPort); } } }
//---- internal void SdkInit() { bool init = _api.Btsdk_IsSDKInitialized(); bool conn = _api.Btsdk_IsServerConnected(); // Ok to call if '!init'? if (init && conn) { return; } BtSdkError ret = _api.Btsdk_Init(); //Debug.WriteLine("BlueSoleil Btsdk_Init: " + BluesoleilUtils.BtSdkErrorToString(ret)); BluesoleilUtils.CheckAndThrow(ret, "Btsdk_Init"); #if DEBUG // When Radio is Off, Init succeeds when called but IsSDKInitialized // still returns false! That persists until the radio's turned on. bool initAfter = _api.Btsdk_IsSDKInitialized(); bool connAfter = _api.Btsdk_IsServerConnected(); // Ok to call if '!init'? if (!initAfter || !connAfter) { bool ready = _api.Btsdk_IsBluetoothReady(); Debug.Assert(!ready, "Not init&&conn, but NOT ready..."); Debug.Assert(!init, "Not init&&conn, but NOT !init..."); Debug.Assert(conn, "Not init&&conn, but NOT conn..."); } #endif _needsDispose = true; }
public static string BtSdkErrorToString(BtSdkError ret) { var txt = string.Format(CultureInfo.InvariantCulture, "{0}=0x{1:X4}", ret, (int)ret); return(txt); }
internal static void CheckAndThrow(BtSdkError ret, string descr) { if (ret == BtSdkError.OK) { return; } SocketError?sockErr = null; switch (ret) { // TODO ! Match BlueSoleil error codes to SocketExceptions. // Note need different errors for GetServiceRecords, handle here or in GSR itself? case BtSdkError.PAGE_TIMEOUT: sockErr = SocketError.TimedOut; break; case BtSdkError.NO_SERVICE: sockErr = SocketError.ConnectionRefused; break; case BtSdkError.SDK_UNINIT: sockErr = SocketError.NotInitialized; break; } if (sockErr == null) { sockErr = SocketError.NotSocket; } throw new BlueSoleilSocketException(ret, sockErr.Value); }
void PinReply_Runner(object state) { var args = (DeviceAndPin)state; BtSdkError ret = _factory.Api.Btsdk_PinCodeReply(args.Device, args.Pin, (UInt16)args.Pin.Length); Debug.Assert(ret == BtSdkError.OK); }
bool IBluetoothSecurity.RemoveDevice(BluetoothAddress device) { BluesoleilDeviceInfo bdi = BluesoleilDeviceInfo.CreateFromGivenAddress(device, _factory); // TO-DO We don't need to do Btsdk_UnPairDevice also do we? BtSdkError ret = _factory.Api.Btsdk_DeleteRemoteDeviceByHandle(bdi.Handle); return(ret == BtSdkError.OK); }
//---- private void ConnectRfcomm(BluetoothEndPoint remoteEP, UInt32 hDev, out UInt32 hConn, out byte channel, out int comPort) { Structs.BtSdkAppExtSPPAttrStru sppAttr = new Structs.BtSdkAppExtSPPAttrStru(remoteEP); // Debug.WriteLine("Gonna Btsdk_ConnectAppExtSPPService with: " + sppAttr.ToString()); BtSdkError ret = _factory.Api.Btsdk_ConnectAppExtSPPService(hDev, ref sppAttr, out hConn); Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "ret: {0}, hConn: 0x{1:X}, with: {2}", ret, hConn, sppAttr.ToString())); _hDev = hDev; BluesoleilUtils.CheckAndThrow(ret, "Btsdk_ConnectAppExtSPPService"); // var comNumGetClientPort = _factory.Api.Btsdk_GetClientPort(hConn); Debug.WriteLine("comNumGetClientPort: " + comNumGetClientPort); // if (sppAttr.rf_svr_chnl != 0) { Debug.Assert(sppAttr.com_index != 0, "Argghhhh com_index is zero! (When rf_svr_chnl isn't)."); channel = sppAttr.rf_svr_chnl; comPort = sppAttr.com_index; } else { // Some profiles are handled specifically OBEX, etc etc // so they don't create a COM port when that Debug.Assert(sppAttr.com_index == 0, "Don't expect a COM port to be created in this (fail) case."); // // Connecting to SPP 0x1101 also returns no com port in the // struct but a COM port is connected for it. Btsdk_GetClientPort // DOES return the correct port see whether we're in that case. if (comNumGetClientPort != 0) { comPort = comNumGetClientPort; channel = 0; // Unknown! } else { // Highly likely an OPP/etc connection was made, and not a RFCOMM // connection, and thus no COM port we can use. :-( So fail! Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, "BlueSoleil seems no RFCOMM connection made, closing. (channel: {0}, COM: {1})", sppAttr.rf_svr_chnl, sppAttr.com_index)); // (Note: Add a dummy record so RemoveLiveConnection works ok). int liveCountB = _factory.AddConnection(hConn, NullBluesoleilConnection.Instance); Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "BlueSoleilClient.Connect non-RFCOMM LiveConns count: {0}.", liveCountB)); var retD = _factory.Api.Btsdk_Disconnect(hConn); BluesoleilUtils.Assert(retD, "Close non-RFCOMM connection"); throw BluesoleilUtils.ErrorConnectIsNonRfcomm(); } } //System.Windows.Forms.MessageBox.Show("BlueSoleil created port: '" + comPort + "'."); }
internal void Dispose(bool disposing) { if (!_needsDispose) { return; } // CloseAnyLiveConnections(); // _needsDispose = false; BtSdkError ret = _api.Btsdk_Done(); BluesoleilUtils.Assert(ret, "Btsdk_Done"); }
internal IAsyncResult BeginInquiry(int maxDevices, TimeSpan inquiryLength, AsyncCallback callback, object state, BluetoothClient.LiveDiscoveryCallback liveDiscoHandler, object liveDiscoState, DiscoDevsParams args) { _fcty.SdkInit(); _fcty.RegisterCallbacksOnce(); // byte maxDurations; byte maxNum; CommonDiscoveryBluetoothClient.ConvertBthInquiryParams(maxDevices, inquiryLength, out maxNum, out maxDurations); return(BeginInquiry(maxDevices, inquiryLength, callback, state, liveDiscoHandler, liveDiscoState, delegate() { BtSdkError ret = _fcty.Api.Btsdk_StartDeviceDiscovery( AnyClass, maxNum, maxDurations); BluesoleilUtils.CheckAndThrow(ret, "Btsdk_StartDeviceDiscovery"); }, args)); }
bool IBluetoothSecurity.PairRequest(BluetoothAddress device, string pin) { //if (pin != null) { // throw new NotImplementedException("Only support pin 'null'/'Nothing' to show the UI dialog."); // BluesoleilDeviceInfo bdi = BluesoleilDeviceInfo.CreateFromGivenAddress(device, _factory); BTDEVHDL hDev = bdi.Handle; _factory.RegisterCallbacksOnce(); try { if (pin != null) { SetPin(hDev, pin); } BtSdkError ret = _factory.Api.Btsdk_PairDevice(hDev); // TODO Do we have to wait here for completion??? return(ret == BtSdkError.OK); } finally { if (pin != null) { RevokePin(hDev); } } }
internal BlueSoleilSocketException(BtSdkError bsError, SocketError socketErr) : this(bsError, (int)socketErr) { }
internal static void Assert(BtSdkError ret, string descr) { Debug.Assert(ret == BtSdkError.OK, descr + ": " + BtSdkErrorToString(ret)); }