public static bool SetBluetoothPortName(BluetoothAddress deviceaddress, string portname) { if (portname == null) return false; string strDeviceAddress = deviceaddress.ToString("8"); using (RegistryKey rkPorts = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Bluetooth\Serial\Ports", true)) { RegistryKey rkNewPort; rkNewPort = rkPorts.OpenSubKey(strDeviceAddress, true); if (rkNewPort == null) { rkNewPort = rkPorts.CreateSubKey(strDeviceAddress); if (rkNewPort == null) { return false; } } else { // deregister for old port string oldPort = ((string)rkNewPort.GetValue("Port", null)).Trim('\0'); if (oldPort != null) DeregisterDevice(oldPort); } rkNewPort.SetValue("KeepDCB", 0); rkNewPort.SetValue("RemoteDCB", 0); rkNewPort.SetValue("Encryption", 0); rkNewPort.SetValue("Authentication", 0); rkNewPort.SetValue("Port", portname); rkNewPort.SetValue("Server", 0); rkNewPort.Close(); rkNewPort = null; PORTEMUPortParams pep = new PORTEMUPortParams(); pep.uiportflags = RFCOMM_PORT_FLAGS.REMOTE_DCB; pep.flocal = false; pep.device = deviceaddress.ToInt64(); pep.uuidService = BluetoothService.SerialPort; string portprefix = portname.Substring(0, 3); int portindex = int.Parse(portname[3].ToString()); IntPtr handle = RegisterDevice(portprefix, portindex, "btd.dll", ref pep); if (handle == IntPtr.Zero) { rkPorts.DeleteSubKeyTree(strDeviceAddress); return false; } } return true; }