Exemplo n.º 1
0
        public bool DoAssociation(string btAddr)
        {
            if (btAddr == "")
            {
                log.WriteLog("Missing BT Address!");
                Win32.doBadBeeps();
                return(false);
            }
            if (_deviceUtility == null)
            {
                log.WriteLog("DeviceUtility unavailable!");
                Win32.doBadBeeps();
                return(false);
            }

            string strDeviceName;

            log.WriteLog("FindDevice '" + btAddr + "'...");
            if (!FindDevice(btAddr, out strDeviceName))
            {
                log.WriteLog("Device not found!");
                Win32.doBadBeeps();
                return(false);
            }
            log.WriteLog("... Device was found");

            log.WriteLog("Association of '" + strDeviceName + "'...");
            foreach (string s in _sPasskeys)
            {
                try
                {
                    log.WriteLog("Bonding...");
                    if (_deviceUtility.IsBondWithDeviceNeeded(strDeviceName))
                    {
                        string strPasskey;
                        log.WriteLog("Authentication: trying '" + s + "'...");
                        //deviceUtility.GetDeviceProperty(strDeviceName, DeviceUtility.PropertyName.Passkey);
                        //if (string.Compare(strPasskey, "") == 0)
                        //{
                        strPasskey = s;
                        //strPasskey = "1111";
                        _deviceUtility.SetDeviceProperty(strDeviceName, DeviceUtility.PropertyName.Passkey, strPasskey);
                        //}
                        // Bond the computer and the device.
                        // TODO: Because radio connections can sometimes fail,
                        // you should build in retry logic when BondWithDevice
                        // fails. This sample just tries once.
                        _deviceUtility.BondWithDevice(strDeviceName);
                        log.WriteLog("... Association and bonding finished");
                        break;
                    }
                    else
                    {
                        log.WriteLog("... no bonding needed");
                        break;
                    }
                }
                catch (DeviceUtilityException ex)
                {
                    log.WriteLog("Error in Association: " + ex.Message);
                    return(false);
                }
                catch (Exception ex)
                {
                    log.WriteLog("Error in Association: " + ex.Message);
                    return(false);
                }
            }//foreach

            try
            {
                log.WriteLog("Registration...");
                _deviceUtility.SetDefaultDevice(null, null, strDeviceName);
                // Set up a port to use the selected device. Refer to
                // the BtPort element in the XML file.

                //_deviceUtility.AddDevice(null, null, "BtPort");
                //_deviceUtility.SetDeviceProperty("BtPort", DeviceUtility.PropertyName.DeviceAddress, "COM6:");

                _deviceUtility.SetActiveDevice(null, null, strDeviceName);
                log.WriteLog("...Registration OK");
                Win32.doGoodBeeps();
                return(true);
            }
            catch (DeviceUtilityException ex)
            {
                log.WriteLog("Error in Registration: " + ex.Message);
                Win32.doBadBeeps();
                return(false);
            }
        }