/// <summary>
        /// Remove the device by deleting it from the Registry.
        /// </summary>
        /// <param name="device">The device address.</param>
        /// <returns>Whether the device is deleted -- it is no longer a remembered device.
        /// </returns>
        internal static bool DeleteKnownDevice(BluetoothAddress device)
        {
            string itemName = GetWidcommDeviceKeyName(device);

            using (RegistryKey rkDevices = Registry.LocalMachine.OpenSubKey(DevicesRegPath, true)) {
                using (RegistryKey theOne = rkDevices.OpenSubKey(itemName, false)) {
                    if (theOne == null) // Isn't present.
                    {
                        return(true);
                    }
                }
                try {
                    rkDevices.DeleteSubKeyTree(itemName);
                    return(true);
                } catch (System.Security.SecurityException ex) {
                    Utils.MiscUtils.Trace_WriteLine("DeleteKnownDevice DeleteSubKeyTree(" + itemName + "): "
                                                    + ExceptionExtension.ToStringNoStackTrace(ex));
                } catch (UnauthorizedAccessException ex) {
                    Utils.MiscUtils.Trace_WriteLine("DeleteKnownDevice DeleteSubKeyTree(" + itemName + "): "
                                                    + ExceptionExtension.ToStringNoStackTrace(ex));
                }
                return(false);
            }
        }
Exemple #2
0
        private static void GetStacks_inLock()
        {
            List_BluetoothFactory list   = new List_BluetoothFactory();
            List_Exception        errors = new List_Exception();
            var stacks = new System.Collections.Generic.List <string>(
                BluetoothFactoryConfig.KnownStacks);

            TraceWriteLibraryVersion();
            foreach (string factoryName in stacks)
            {
                try {
                    Type t = Type.GetType(factoryName, true);
                    Debug.Assert(t != null, string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                          "Expected GetType to throw when type not found: '{0}'", factoryName));
                    object tmp = Activator.CreateInstance(t);
                    Debug.Assert(tmp != null, "Expect all failures to throw rather than return null.");
                    IBluetoothFactoryFactory ff = tmp as IBluetoothFactoryFactory;
                    if (ff == null)
                    {
                        list.Add((BluetoothFactory)tmp);
                    }
                    else     // BluetoothFactoryFactory!
                    {
                        IList_BluetoothFactory multiple = ff.GetFactories(errors);
                        if (multiple != null)
                        {
                            Debug.WriteLine(string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                          "BluetoothFactoryFactory '{0}' supplied {1} items.",
                                                          ff.GetType().AssemblyQualifiedName, multiple.Count));
                            list.AddRange(multiple);
                        }
                        else
                        {
                            Debug.WriteLine(string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                          "BluetoothFactoryFactory '{0}' returned null.", ff.GetType().AssemblyQualifiedName));
                        }
                    }
                    if (BluetoothFactoryConfig.OneStackOnly)
                    {
                        break;
                    }
                } catch (Exception ex) {
                    if (ex is System.Reflection.TargetInvocationException)
                    {
                        Debug.Assert(ex.InnerException != null, "We know from the old }catch(TIEX){throw ex.InnerEx;} that this is non-null");
                        ex = ex.InnerException;
                    }
                    errors.Add(ex);
                    string msg = string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                               "Exception creating factory '{0}, ex: {1}", factoryName, ex);
                    if (BluetoothFactoryConfig.ReportAllErrors)
                    {
                        Utils.MiscUtils.Trace_Fail(msg);  // (No Trace.Fail on NETCF).
                    }
                    Debug.WriteLine(msg);
                }
            }//for
            if (list.Count == 0)
            {
                if (!BluetoothFactoryConfig.ReportAllErrors)   // Have been reported above.
                {
                    foreach (Exception ex in errors)
                    {
                        string info = ExceptionExtension.ToStringNoStackTrace(ex);
                        Utils.MiscUtils.Trace_WriteLine(info);
                    }
                }
#if !NO_WIDCOMM
                // Special case Widcomm -- report if stacks seems there, but not our DLL.
                Exception wcNoInterfaceDll = null;
                try {
                    wcNoInterfaceDll = Widcomm.WidcommBtIf.IsWidcommStackPresentButNotInterfaceDll();
                } catch (Exception ex) {
                    // Maybe a P/Invoke exception calling GetModuleHandle
                    if (Environment.OSVersion.Platform.ToString().StartsWith("Win", StringComparison.Ordinal))
                    {
                        // Don't complain in Linux etc.
                        Utils.MiscUtils.Trace_WriteLine("Exception in IsWidcommStackPresentButNotInterfaceDll: " + ex);
                    }
                }
                if (wcNoInterfaceDll != null)
                {
                    throw wcNoInterfaceDll;
                }
#endif
                throw new PlatformNotSupportedException("No supported Bluetooth protocol stack found.");
            }
            else
            {
                SetFactories_inLock(list);
            }
            // result
#if !ANDROID
            Debug.WriteLine(string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                          "Num factories: {1}, Primary Factory: {0}",
                                          (s_factories == null ? "(null)" : s_factories[0].GetType().Name),
                                          (s_factories == null ? "(null)" : s_factories.Count.ToString())));
#endif
        }