//---------- public List_IBluetoothDeviceInfo GetKnownRemoteDeviceEntries() { List <REM_DEV_INFO> list = new List <REM_DEV_INFO>(); REM_DEV_INFO info = new REM_DEV_INFO(); int cb = System.Runtime.InteropServices.Marshal.SizeOf(typeof(REM_DEV_INFO)); IntPtr pBuf = System.Runtime.InteropServices.Marshal.AllocHGlobal(cb); try { REM_DEV_INFO_RETURN_CODE ret = m_btIf.GetRemoteDeviceInfo(ref info, pBuf, cb); Utils.MiscUtils.Trace_WriteLine("GRDI: ret: {0}=0x{0:X}", ret); while (ret == REM_DEV_INFO_RETURN_CODE.SUCCESS) { list.Add(info); // COPY it into the list ret = m_btIf.GetNextRemoteDeviceInfo(ref info, pBuf, cb); Utils.MiscUtils.Trace_WriteLine("GnRDI: ret: {0}=0x{0:X}", ret); }//while if (ret != REM_DEV_INFO_RETURN_CODE.EOF) { throw WidcommSocketExceptions.Create(ret, "Get[Next]RemoteDeviceInfo"); } // List_IBluetoothDeviceInfo bdiList = new List_IBluetoothDeviceInfo(list.Count); foreach (REM_DEV_INFO cur in list) { IBluetoothDeviceInfo bdi = WidcommBluetoothDeviceInfo.CreateFromStoredRemoteDeviceInfo(cur, m_factory); bdiList.Add(bdi); } return(bdiList); } finally { System.Runtime.InteropServices.Marshal.FreeHGlobal(pBuf); } }
public IAsyncResult BeginServiceDiscovery(BluetoothAddress address, Guid serviceGuid, SdpSearchScope searchScope, AsyncCallback asyncCallback, Object state) { BeginServiceDiscoveryKillInquiry(); // Just in case the user modifies the original address!!! BluetoothAddress addr2 = (BluetoothAddress)address.Clone(); AsyncResult <ISdpDiscoveryRecordsBuffer, ServiceDiscoveryParams> ar = new AsyncResult <ISdpDiscoveryRecordsBuffer, ServiceDiscoveryParams>(asyncCallback, state, new ServiceDiscoveryParams(addr2, serviceGuid, searchScope)); lock (lockServiceDiscovery) { if (m_arServiceDiscovery != null) { throw new NotSupportedException("Currently support only one concurrent Service Lookup operation."); } bool success = false; try { m_arServiceDiscovery = ar; bool ret = m_btIf.StartDiscovery(addr2, serviceGuid); Debug.WriteLine(WidcommUtils.GetTime4Log() + ": StartDiscovery ret: " + ret); if (!ret) { WBtRc ee = GetExtendedError(); throw WidcommSocketExceptions.Create_StartDiscovery(ee); } success = true; } finally { if (!success) { m_arServiceDiscovery = null; } } } return(ar); }
protected override bool TryBondingIf_inLock(BluetoothAddress addressToConnect, int ocScn, out Exception error) { const bool Retrying = true; const bool NotRetrying = false; // /* * if(havePin) * if(Bond success) * if(OpenClient success) * return Retrying; * return NotRetrying; */ try { // Mustn't die on this thread, need to report all exceptions back!! // if (m_passcodeToTry != null) { if (addressToConnect == null) { Debug.Fail("In retry, have passcode, but looks like Server mode!!"); error = null; return(NotRetrying); } // string passcodeToTry = m_passcodeToTry; m_passcodeToTry = null; Debug.Assert(addressToConnect != null, "addressToConnect != null"); bool didPair = Bond(addressToConnect, passcodeToTry); if (didPair) { //TODO Destroy old port! SetPort(m_factory.GetWidcommRfcommPort()); PORT_RETURN_CODE ret = m_port.OpenClient(ocScn, WidcommUtils.FromBluetoothAddress(addressToConnect)); Utils.MiscUtils.Trace_WriteLine("OpenClient/AB ret: {0}=0x{0:X}", ret); if (ret == PORT_RETURN_CODE.SUCCESS) { error = null; return(Retrying); } else { error = WidcommSocketExceptions.Create(ret, "OpenClient/AB"); return(NotRetrying); } } } // error = null; } catch (Exception ex) { error = ex; } return(NotRetrying); }
void ISdpService.AddAttribute(ushort id, DESC_TYPE dt, int valLen, byte[] val) { Debug.Assert(id != 0, "Can't add ServiceRecordHandle..."); SDP_RETURN_CODE ret = NativeMethods.SdpService_AddAttribute(m_pSdpService, id, dt, checked ((uint)valLen), val); if (ret != SDP_RETURN_CODE.OK) { throw WidcommSocketExceptions.Create_SDP_RETURN_CODE(ret, "AddAttribute"); } }
void ISdpService.AddRFCommProtocolDescriptor(byte scn) { Debug.Assert(scn >= BluetoothEndPoint.MinScn, ">=1"); Debug.Assert(scn <= BluetoothEndPoint.MaxScn, "<=30"); SDP_RETURN_CODE ret = NativeMethods.SdpService_AddRFCommProtocolDescriptor( m_pSdpService, scn); if (ret != SDP_RETURN_CODE.OK) { throw WidcommSocketExceptions.Create_SDP_RETURN_CODE(ret, "AddRFCommProtocolDescriptor"); } }
void ISdpService.AddServiceClassIdList(Guid serviceClass) { GCHandle h = GCHandle.Alloc(serviceClass, GCHandleType.Pinned); try { IntPtr pArray = h.AddrOfPinnedObject(); SDP_RETURN_CODE ret = NativeMethods.SdpService_AddServiceClassIdList( m_pSdpService, 1, pArray); if (ret != SDP_RETURN_CODE.OK) { throw WidcommSocketExceptions.Create_SDP_RETURN_CODE(ret, "AddServiceClassIdList"); } } finally { h.Free(); } }
void ISdpService.AddServiceClassIdList(IList <Guid> serviceClasses) { Guid[] serviceClassArray = new Guid[serviceClasses.Count]; serviceClasses.CopyTo(serviceClassArray, 0); GCHandle h = GCHandle.Alloc(serviceClassArray, GCHandleType.Pinned); try { IntPtr pArray = h.AddrOfPinnedObject(); SDP_RETURN_CODE ret = NativeMethods.SdpService_AddServiceClassIdList( m_pSdpService, serviceClassArray.Length, pArray); if (ret != SDP_RETURN_CODE.OK) { throw WidcommSocketExceptions.Create_SDP_RETURN_CODE(ret, "AddServiceClassIdList"); } } finally { h.Free(); } }
internal void HandleDiscoveryComplete() { Utils.MiscUtils.Trace_WriteLine("HandleDiscoveryComplete"); AsyncResult <ISdpDiscoveryRecordsBuffer, ServiceDiscoveryParams> sacAr = null; ISdpDiscoveryRecordsBuffer recBuf = null; Exception sacEx = null; try { lock (lockServiceDiscovery) { Debug.Assert(m_arServiceDiscovery != null, "NOT m_arServiceDiscovery != null"); if (m_arServiceDiscovery == null) { return; } // Nothing we can do then! sacAr = m_arServiceDiscovery; m_arServiceDiscovery = null; BluetoothAddress addr; ushort numRecords0; DISCOVERY_RESULT result = m_btIf.GetLastDiscoveryResult(out addr, out numRecords0); if (result != DISCOVERY_RESULT.SUCCESS) { sacEx = WidcommSocketExceptions.Create(result, "ServiceRecordsGetResult"); return; } if (!addr.Equals(sacAr.BeginParameters.address)) { sacEx = new InvalidOperationException("Internal error -- different DiscoveryComplete address."); return; } // Get the records recBuf = m_btIf.ReadDiscoveryRecords(addr, MaxNumberSdpRecords, sacAr.BeginParameters); }//lock } catch (Exception ex) { sacEx = ex; } finally { Debug.Assert(sacAr != null, "out: NOT sacAr != null"); Debug.Assert(m_arServiceDiscovery == null, "out: NOT m_arServiceDiscovery == null"); WaitCallback dlgt = delegate { RaiseDiscoveryComplete(sacAr, recBuf, sacEx); }; ThreadPool.QueueUserWorkItem(dlgt); } }
//-------- protected override void DoWrite(byte[] p_data, UInt16 len_to_write, out UInt16 p_len_written) { PORT_RETURN_CODE ret; if (_singleThreader != null) { WidcommPortSingleThreader.PortWriteCommand cmd = AddCommand( new WidcommPortSingleThreader.PortWriteCommand(p_data, len_to_write, m_port)); ret = cmd.WaitCompletion(out p_len_written); } else { ret = m_port.Write(p_data, len_to_write, out p_len_written); } if (ret != PORT_RETURN_CODE.SUCCESS) { throw new IOException(WrappingIOExceptionMessage, WidcommSocketExceptions.Create(ret, "Write")); } }
protected override void DoOpenServer(int scn) { PORT_RETURN_CODE ret; if (_singleThreader != null) { WidcommPortSingleThreader.OpenServerCommand cmd = AddCommand( new WidcommPortSingleThreader.OpenServerCommand(scn, m_port)); ret = cmd.WaitCompletion(); } else { ret = m_port.OpenServer(scn); } Utils.MiscUtils.Trace_WriteLine("OpenServer ret: {0}=0x{0:X}", ret); if (ret != PORT_RETURN_CODE.SUCCESS) { throw WidcommSocketExceptions.Create(ret, "OpenServer"); } }
void ISdpService.AddServiceName(string serviceName) { IntPtr pServiceNameAscii = IntPtr.Zero; #if !NETCF // Don't need ANSI there (and CF doesn't support marshalling to ascii/ansi) pServiceNameAscii = Marshal.StringToHGlobalAnsi(serviceName); #endif try { SDP_RETURN_CODE ret = NativeMethods.SdpService_AddServiceName( m_pSdpService, serviceName, pServiceNameAscii); if (ret != SDP_RETURN_CODE.OK) { throw WidcommSocketExceptions.Create_SDP_RETURN_CODE(ret, "AddServiceName"); } } finally { if (pServiceNameAscii != IntPtr.Zero) { Marshal.FreeHGlobal(pServiceNameAscii); } } }
protected override void DoOpenClient(int scn, BluetoothAddress addressToConnect) { byte[] address = WidcommUtils.FromBluetoothAddress(addressToConnect); PORT_RETURN_CODE ret; if (_singleThreader != null) { WidcommPortSingleThreader.OpenClientCommand cmd = AddCommand( new WidcommPortSingleThreader.OpenClientCommand(scn, address, m_port)); ret = cmd.WaitCompletion(); } else { ret = m_port.OpenClient(scn, address); } Utils.MiscUtils.Trace_WriteLine("OpenClient ret: {0}=0x{0:X}", ret); if (ret != PORT_RETURN_CODE.SUCCESS) { throw WidcommSocketExceptions.Create(ret, "OpenClient"); } }