| C# | Visual Basic |
public class BluetoothWin32Authentication : IDisposable
Public Class BluetoothWin32Authentication _ Implements IDisposable
| All Members | Constructors | Methods | Fields | ||
| Icon | Member | Description |
|---|---|---|
| BluetoothWin32Authentication(BluetoothAddress, String) |
Initializes a new instance of the BluetoothWin32Authentication class,
to respond to a specific address with a specific PIN string.
| |
| BluetoothWin32Authentication(EventHandler<(Of <(BluetoothWin32AuthenticationEventArgs>)>)) |
Initializes a new instance of the BluetoothWin32Authentication class,
to call a specified handler when any device requires authentication.
| |
| Dispose()()() |
Release the unmanaged resources used by the BluetoothWin32Authentication.
| |
| Dispose(Boolean) |
Release the unmanaged resources used by the BluetoothWin32Authentication,
and optionally disposes of the managed resources.
| |
| Equals(Object) | (Inherited from Object.) | |
| Finalize()()() |
Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection.
(Inherited from Object.) | |
| GetHashCode()()() |
Serves as a hash function for a particular type.
(Inherited from Object.) | |
| GetType()()() |
Gets the Type of the current instance.
(Inherited from Object.) | |
| MemberwiseClone()()() |
Creates a shallow copy of the current Object.
(Inherited from Object.) | |
| NativeErrorDeviceNotConnected |
Windows’ ERROR_DEVICE_NOT_CONNECTED
| |
| NativeErrorNotAuthenticated |
Windows’ ERROR_NOT_AUTHENTICATED
| |
| NativeErrorSuccess |
Windows’ ERROR_SUCCESS
| |
| ToString()()() | (Inherited from Object.) |
Respond to requests for authentication for Bluetooth devices. It is used by BluetoothClient to support its Pin()()() property, in that case an instance is created specifying the device that is being connected to and the PIN string to use. It can also be used a mode where a user supplied callback will be called when any device requires authentication, see the example below.
The callback mode can be configured to do a callback after the ‘send PIN’action, this allows one to see if it was successful etc. An example sequence where the PIN was incorrect is as follows.
Authenticate one device -- with wrong passcode here the first two times. Passcode respectively: 'BAD-x', 'BAD-y', '9876' Making PC discoverable Hit Return to complete Authenticating 0017E464CF1E wm_alan1 Attempt# 0, Last error code 0 Sending "BAD-x" Authenticating 0017E464CF1E wm_alan1 Attempt# 1, Last error code 1244 Sending "BAD-y" Authenticating 0017E464CF1E wm_alan1 Attempt# 2, Last error code 1167 Sending "9876" Authenticating 0017E464CF1E wm_alan1 Attempt# 3, Last error code 1167 etc
That is we see the error code of 1244=NativeErrorNotAuthenticated once, and then the peer device disappears (1167=NativeErrorDeviceNotConnected). I suppose that's a security feature -- its stops an attacker from trying again and again with different passcodes. Anyway the result of that is that is it not worth repeating the callback after the device disappears. The code now enforces this. With CallbackWithResult set to true, if the result of the previous attempt was ‘success’ or ‘device not connected’ then any new PIN set in the callback won’t be used and thus the callback won’t be called again for that authentication attempt.
A successful authentication process can thus be detected by checking if
CopyC#e.PreviousNativeErrorCode == NativeErrorSuccess && e.AttemptNumber != 0
The instance will continue receiving authentication requests until it is disposed or garbage collected, so keep a reference to it whilst it should be active and call Dispose()()() when you’re finished.
BluetoothWin32Authentication authenticator
= new BluetoothWin32Authentication(remoteEP.Address, m_pin);
// when the peer is expected to require pairing, perhaps do some work.
authenticator.Dispose();Using pairer As New BluetoothWin32Authentication(AddressOf Win32AuthCallbackHandler)
Console.WriteLine("Hit Return to stop authenticating")
Console.ReadLine()
End Using
...
Sub Win32AuthCallbackHandler(ByVal sender As Object, ByVal e As InTheHand.Net.Bluetooth.BluetoothWin32AuthentionEventArgs)
Dim address As String = e.Device.DeviceAddress.ToString()
Console.WriteLine("Received an authentication request from address " + address)
' compare the first 8 hex numbers, this is just a special case because in the
' used scenario the model of the devices can be identified by the first 8 hex
' numbers, the last 4 numbers being the device specific part.
If address.Substring(0, 8).Equals("0099880D") OrElse _
address.Substring(0, 8).Equals("0099880E") Then
' send authentication response
e.Pin = "5276"
ElseIf (address.Substring(0, 8).Equals("00997788")) Then
' send authentication response
e.Pin = "ásdfghjkl"
End If
End Sub| Object | |
| BluetoothWin32Authentication | |