Exemplo n.º 1
0
        private bool FindDevice(string BDAAddress, out string strOutDeviceName)
        {
            bool bFound = false;

            strOutDeviceName = "";
            try
            {
                log.WriteLog("Discovering devices ...");
                _deviceUtility.DiscoverDevices(null, null);
                log.WriteLog("...Discover finished");
            }
            catch (DeviceUtilityException ex)
            {
                log.WriteLog("DiscoveredDevices exception: " + ex.Message);
            }
            try
            {
                string[] temp = _deviceUtility.GetDiscoveredDevices();
                if (temp.Length > 0)
                {
                    // Add found devices to the list of known devices.
                    // When calling AddDevice with a device name returned by
                    // GetDiscoveredDevices, make sure you call AddDevice and
                    // GetDiscoveredDevices within the same Initialize/Close
                    // block. Otherwise,the device is added to the XML file
                    // as a non-Bluetooth device because Bluetooth device
                    // information retrieved by DiscoverDevices is lost when
                    // Close is called.
                    foreach (string device in temp)
                    {
                        _deviceUtility.AddDevice(null, null, device);
                        log.WriteLog("Added '" + device + "'");
                    }
                }
                else
                {
                    log.WriteLog("No devices nearby");
                }
            }
            catch (DeviceUtilityException ex)
            {
                log.WriteLog("GetDiscoveredDevices exception: " + ex.Message);
            }
            try
            {
                string   deviceAddress;
                string[] temp = _deviceUtility.GetDevices(null, null);
                if (temp.Length > 0)
                {
                    foreach (string device in temp)
                    {
                        deviceAddress = _deviceUtility.GetDeviceProperty(device, DeviceUtility.PropertyName.DeviceAddress);
                        if (string.Compare(deviceAddress, BDAAddress, true) == 0)
                        {
                            bFound           = true;
                            strOutDeviceName = device;
                            //string passkey = _deviceUtility.GetDeviceProperty(device, DeviceUtility.PropertyName.Passkey);  //throws exception see docu
                            //string authent = _deviceUtility.GetDeviceProperty(device, DeviceUtility.PropertyName.Authentication);
                            break;
                        }
                    }
                }
                else
                {
                    log.WriteLog("No devices by GetDevices()");
                }
            }
            catch (DeviceUtilityException ex)
            {
                log.WriteLog("GetDiscoveredDevices exception: " + ex.Message);
            }

            if (bFound)
            {
                log.WriteLog("Found device with matching '" + BDAAddress + "'");
                return(true);
            }
            else
            {
                log.WriteLog("No matching devices for '" + BDAAddress + "'");
                return(false);
            }
        }