/// <summary> /// This function revokes the personal identification number (PIN) for the Bluetooth device. /// </summary> /// <remarks><para>On Windows CE platforms this calls <c>BthRevokePIN</c>, /// its MSDN remarks say: /// </para> /// <para>“When the PIN is revoked, it is removed from registry. /// The active connection to the device is not necessary, nor is the presence /// of the Bluetooth controller.” /// </para> /// <para>On Windows CE platforms this removes any pending BluetoothWin32Authentication object but does not remove the PIN for an already authenticated device. /// Use RemoveDevice to ensure a pairing is completely removed.</para> /// <para>See also /// <see cref="M:InTheHand.Net.Bluetooth.BluetoothSecurity.SetPin(InTheHand.Net.BluetoothAddress,System.String)"/> /// </para> /// </remarks> /// <param name="device">The remote device.</param> /// <returns>True on success, else False.</returns> /// <seealso cref="M:InTheHand.Net.Bluetooth.BluetoothSecurity.SetPin(InTheHand.Net.BluetoothAddress,System.String)"/> public bool RevokePin(BluetoothAddress device) { if (device == null) { throw new ArgumentNullException("device"); } #if NETCF int result = NativeMethods.BthRevokePIN(device.ToByteArray()); if (result != 0) { int error = Marshal.GetLastWin32Error(); return(false); } return(true); #else if (authenticators.ContainsKey(device)) { BluetoothWin32Authentication bwa = (BluetoothWin32Authentication)authenticators[device]; authenticators.Remove(device); bwa.Dispose(); return(true); } return(false); //throw new PlatformNotSupportedException("Use RemoveDevice to remove a pairing with a device from Windows XP"); #endif }
/// <summary> /// Connects the client to a remote Bluetooth host using the specified mac address and OnConnected handler. /// </summary> /// <param name="mac">The mac address of the remote host.</param> /// <param name="handler">The delegate to handle connecting event.</param> /// <returns>true if the BluetoothAdapter was connected to a remote resource; otherwise, false.</returns> public bool Connect(string mac, OnConnected handler) { BluetoothAddress bta = new BluetoothAddress(Convert.ToInt64(mac, 16)); BluetoothEndPoint rep = new BluetoothEndPoint(bta, BluetoothService.SerialPort); lock ( mConnLock ) { try { if ((Connected && !ALLOW_OTHER_CONNECTION) || !ValidateAddress(mac)) { return(false); } Disconnect(); RemovePairedDevice(mac); EventHandler <BluetoothWin32AuthenticationEventArgs> authHandler = new EventHandler <BluetoothWin32AuthenticationEventArgs>(handleRequests); BluetoothWin32Authentication authenticator = new BluetoothWin32Authentication(authHandler); // 페어링 요청 BluetoothSecurity.PairRequest(bta, null); mBtClient = new BluetoothClient(); mBtClient.Connect(rep); authenticator.Dispose(); DeviceClass = DeviceClass != 0 ? DeviceClass : FindDeviceClass(mac); if (DeviceClass == 0) { mBtClient.Dispose(); return(false); } WriteDeviceClass(mac, DeviceClass); DeviceAddress = mac; handler(DeviceClass); } catch { return(false); } return(true); } }
//#endif #region Set PIN /// <summary> /// This function stores the personal identification number (PIN) for the Bluetooth device. /// </summary> /// <param name="device">Address of remote device.</param> /// <param name="pin">Pin, alphanumeric string of between 1 and 16 ASCII characters.</param> /// <remarks><para>On Windows CE platforms this calls <c>BthSetPIN</c>, /// its MSDN remarks say: /// </para> /// <para>“Stores the pin for the Bluetooth device identified in pba. /// The active connection to the device is not necessary, nor is the presence /// of the Bluetooth controller. The PIN is persisted in the registry until /// BthRevokePIN is called. /// </para> /// <para>“While the PIN is stored, it is supplied automatically /// after the PIN request is issued by the authentication mechanism, so the /// user will not be prompted for it. Typically, for UI-based devices, you /// would set the PIN for the duration of authentication, and then revoke /// it after authentication is complete.” /// </para> /// <para>See also /// <see cref="M:InTheHand.Net.Bluetooth.BluetoothSecurity.RevokePin(InTheHand.Net.BluetoothAddress)"/> /// </para> /// </remarks> /// <returns>True on success, else False.</returns> /// <seealso cref="M:InTheHand.Net.Bluetooth.BluetoothSecurity.RevokePin(InTheHand.Net.BluetoothAddress)"/> public bool SetPin(BluetoothAddress device, string pin) { if (device == null) { throw new ArgumentNullException("device"); } if (pin == null) { throw new ArgumentNullException("pin"); } #if NETCF if (pin.Length < 1 | pin.Length > 16) { throw new ArgumentException("Pin must be between 1 and 16 characters long."); } byte[] pinbytes = System.Text.Encoding.ASCII.GetBytes(pin); int len = pin.Length; int result = NativeMethods.BthSetPIN(device.ToByteArray(), len, pinbytes); if (result != 0) { int error = Marshal.GetLastWin32Error(); return(false); } return(true); #else //remove existing listener if (authenticators.ContainsKey(device)) { BluetoothWin32Authentication bwa = (BluetoothWin32Authentication)authenticators[device]; authenticators.Remove(device); bwa.Dispose(); } authenticators.Add(device, new BluetoothWin32Authentication(device, pin)); return(true); //else //{ // throw new PlatformNotSupportedException("Use PairRequest to pair with a device from Windows XP"); //} #endif }