| C# | Visual Basic |
public BluetoothWin32Authentication( EventHandler<BluetoothWin32AuthenticationEventArgs> handler )
Public Sub New ( _ handler As EventHandler(Of BluetoothWin32AuthenticationEventArgs) _ )
- handler (EventHandler<(Of <(BluetoothWin32AuthenticationEventArgs>)>))
- A reference to a handler function that can respond to authentication requests.
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 setting
CallbackWithResult=true and checking in the callback 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.
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