示例#1
0
        public BOND_RETURN_CODE Bond(BluetoothAddress address, string passphrase)
        {
            byte[] bdaddr = WidcommUtils.FromBluetoothAddress(address);
            byte[] pinUtf8;
            byte[] pinUtf16; // As seen used in WM sample with CString::GetBuffer().
            if (passphrase != null)
            {
                // TODO should be ASCII??
                pinUtf8  = Encoding.UTF8.GetBytes(passphrase + "\0");
                pinUtf16 = Encoding.Unicode.GetBytes(passphrase + "\0");
            }
            else
            {
                // ?????????
                pinUtf8  = null;
                pinUtf16 = null;
            }
            //
            BOND_RETURN_CODE ret2;

#if !NETCF
            int retBtw = NativeMethods.BtIf_Bond(m_pBtIf, bdaddr, pinUtf8);
            ret2 = (BOND_RETURN_CODE)retBtw;
#else
            int retWce = NativeMethods.BtIf_Bond(m_pBtIf, bdaddr, pinUtf16);
            ret2 = Convert((BOND_RETURN_CODE__WCE)retWce);
#endif
            return(ret2);
        }
示例#2
0
        public bool UnBond(BluetoothAddress address)
        {
            byte[] bdaddr = WidcommUtils.FromBluetoothAddress(address);
            bool   ret    = NativeMethods.BtIf_UnBond(m_pBtIf, bdaddr);

            return(ret);
        }
示例#3
0
        public ISdpDiscoveryRecordsBuffer ReadDiscoveryRecords(BluetoothAddress address, int maxRecords, ServiceDiscoveryParams args)
        {
            byte[] bdaddr = WidcommUtils.FromBluetoothAddress(address);
            IntPtr pList;
            Guid   filter = args.serviceGuid;
            int    count;

            if (args.searchScope == SdpSearchScope.Anywhere)
            {
                count = NativeMethods.BtIf_ReadDiscoveryRecords(m_pBtIf, bdaddr,
                                                                maxRecords, out pList);
            }
            else
            {
                Debug.Assert(args.searchScope == SdpSearchScope.ServiceClassOnly, "the other enum");
                try {
                    count = NativeMethods.BtIf_ReadDiscoveryRecordsServiceClassOnly(m_pBtIf, bdaddr,
                                                                                    maxRecords, out pList, ref filter);
                } catch (EntryPointNotFoundException ex) {
                    ReportNeedNeedNativeDllUpgrade(ex, true);
                    count = NativeMethods.BtIf_ReadDiscoveryRecords(m_pBtIf, bdaddr,
                                                                    maxRecords, out pList);
                } catch (MissingMethodException ex) { // for NETCF
                    ReportNeedNeedNativeDllUpgrade(ex, true);
                    count = NativeMethods.BtIf_ReadDiscoveryRecords(m_pBtIf, bdaddr,
                                                                    maxRecords, out pList);
                }
            }
            ISdpDiscoveryRecordsBuffer recBuf = new SdpDiscoveryRecordsBuffer(
                _factory, pList, count, args);

            return(recBuf);
        }
示例#4
0
        public DISCOVERY_RESULT GetLastDiscoveryResult(out BluetoothAddress address, out UInt16 p_num_recs)
        {
            byte[]           bdaddr = WidcommUtils.FromBluetoothAddress(BluetoothAddress.None);
            DISCOVERY_RESULT ret    = NativeMethods.BtIf_GetLastDiscoveryResult(m_pBtIf, bdaddr, out p_num_recs);

            address = WidcommUtils.ToBluetoothAddress(bdaddr);
            return(ret);
        }
        //--------
        internal static WidcommBluetoothDeviceInfo CreateFromGivenAddressNoLookup(BluetoothAddress address, WidcommBluetoothFactoryBase factory)
        {
            REM_DEV_INFO rdi = new REM_DEV_INFO();

            rdi.bda = WidcommUtils.FromBluetoothAddress(address);
            WidcommBluetoothDeviceInfo bdi = new WidcommBluetoothDeviceInfo(rdi, factory);

            return(bdi);
        }
示例#6
0
        //-------------
        public bool StartDiscovery(BluetoothAddress address, Guid serviceGuid)
        {
            Utils.MiscUtils.Trace_WriteLine("WidcommBtIf.StartDiscovery");
            byte[] bdaddr = WidcommUtils.FromBluetoothAddress(address);
            int    sizeofSdpDiscoveryRec; // just for interests sake
            bool   ret = NativeMethods.BtIf_StartDiscovery(m_pBtIf, bdaddr, ref serviceGuid,
                                                           out sizeofSdpDiscoveryRec);

            return(ret);
        }
示例#7
0
        //----

        internal string CreatePort(BluetoothAddress addr)
        {
            if (IsSet(_waitConnect))
            {
                throw new InvalidOperationException("Already used.");
            }
            _addr = addr;
            byte[] bd_addr           = WidcommUtils.FromBluetoothAddress(addr);
            byte[] tcharzServiceName = { 0, 0 };
            var    inUse             = Interlocked.CompareExchange(ref _sfInConnect, 1, 0);

            if (inUse != 0)
            {
                throw new InvalidOperationException("Widcomm only allows one SPP Connect attempt at a time.");
            }
            SPP_CLIENT_RETURN_CODE ret = (SPP_CLIENT_RETURN_CODE)(-1);

            _singleThreader.AddCommand(new WidcommPortSingleThreader.MiscNoReturnCommand(
                                           () => ret = NativeMethods.SppClient_CreateConnection(
                                               _pSppCli, bd_addr, tcharzServiceName)
                                           )).WaitCompletion();
            Debug.WriteLine("SppClient_CreateConnection ret: " + ret);
            int  timeout   = 30000;
            bool signalled = _waitConnect.WaitOne(timeout, false);

            // Eeek want to set this even when we got NO callback........
            // Do for now because Win32 is not working at all......
            Interlocked.Exchange(ref _sfInConnect, 0);
            if (!signalled)
            {
                throw CommonSocketExceptions.Create_NoResultCode(
                          WidcommSppSocketExceptions.SocketError_Misc,
                          "CreatePort failed (time-out).");
            }
            MemoryBarrier();
            if (_statusState != SPP_STATE_CODE.CONNECTED)
            {
                throw WidcommSppSocketExceptions.Create(_statusState, "CreatePort");
            }
            if (_statusComPort == null)
            {
                throw CommonSocketExceptions.Create_NoResultCode(
                          WidcommSppSocketExceptions.SocketError_Misc,
                          "CreatePort did not complete (cpn).");
            }
            //
            // TODO Move these into the native-event handler.
            //string comPort = MakePortName(_statusComPort.Value);
            //_comNum = _statusComPort.Value;
            //_comPortName = comPort;
            Debug.Assert(_comPortName != null, "_comPortName IS null");
            Debug.Assert(WidcommUtils.ToBluetoothAddress(_statusBda) == addr,
                         "addr NOT equal, is: " + WidcommUtils.ToBluetoothAddress(_statusBda));
            return(_comPortName);
        }
示例#8
0
        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);
        }
        internal static WidcommBluetoothDeviceInfo CreateFromGivenAddress(BluetoothAddress address, WidcommBluetoothFactoryBase factory)
        {
            REM_DEV_INFO rdi = new REM_DEV_INFO();

            rdi.bda = WidcommUtils.FromBluetoothAddress(address);
            WidcommBluetoothDeviceInfo bdi = factory.GetWidcommBtInterface()
                                             .ReadDeviceFromRegistryAndCheckAndSetIfPaired(address, factory);

            if (bdi == null)
            {
                bdi = CreateFromGivenAddressNoLookup(address, factory);
            }
            return(bdi);
        }
示例#10
0
        private static WidcommBluetoothDeviceInfo CreateFromStoredRemoteDeviceInfo(
            BluetoothAddress devAddress, byte[] devName, byte[] devClass,
            WidcommBluetoothFactoryBase factory)
        {
            REM_DEV_INFO rdi = new REM_DEV_INFO();

            rdi.bda       = WidcommUtils.FromBluetoothAddress(devAddress);
            rdi.bd_name   = devName;
            rdi.dev_class = devClass;
            // rdi.b_connected = ...
            // rdi.b_paired = ...
            WidcommBluetoothDeviceInfo bdi = WidcommBluetoothDeviceInfo.CreateFromStoredRemoteDeviceInfo(rdi, factory);
            string nameStr = bdi.DeviceName;

            Debug.Assert(nameStr.Length == 0 || nameStr[nameStr.Length - 1] != 0, "null terminator!!");
            int idxDbg;

            Debug.Assert((idxDbg = nameStr.IndexOf((char)0)) == -1, "null terminator!! at: " + idxDbg);
            return(bdi);
        }
示例#11
0
        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");
            }
        }
示例#12
0
 //--
 public RemoteDeviceState FindIfPresentOrConnected(BluetoothAddress addr)
 {
     return(FindIfPresentOrConnected(WidcommUtils.FromBluetoothAddress(addr)));
 }