using System;
using System.Collections.Generic;
using System.Text;
using InTheHand.Net.Bluetooth;
namespace ConsoleMenuTesting{
partial class BluetoothTesting
{
#if !NETCF
[MenuItem, SubMenu("Win32Auth")]
public void BtWin32Auth()
{
using (BluetoothWin32Authentication ar = new BluetoothWin32Authentication(Hndlr)) {
Console.Write("Hit return to break>");
Console.ReadLine();
}
}
//[MenuItem]
//public void BtWin32AuthEx()
//{
// using (BluetoothWin32AuthenticationEx ar = new BluetoothWin32AuthenticationEx(Hndlr)) {
// Console.Write("Hit return to break>");
// Console.ReadLine();
// }
//}
void Hndlr(object sender, BluetoothWin32AuthenticationEventArgs e)
{
Console.WriteLine("Hndlr!!!!!!!!!!!!");
}
//----
[MenuItem, SubMenu("Win32Auth")]
public void Win32AuthCallbackTwoSeparateAuthentications()
{
Console.WriteLine("Authenticate two devices");
Console.WriteLine("Passcode respectively: '{0}', '{1}', '{2}'",
"1234", "9876", "sdfghjkl");
Win32AuthCallback__(Win32AuthCallbackTwoSeparateAuthenticationsHandler);
}
Int32 Win32AuthCallbackTwoSeparateAuthentications_count;
void Win32AuthCallbackTwoSeparateAuthenticationsHandler(Object sender, InTheHand.Net.Bluetooth.BluetoothWin32AuthenticationEventArgs e)
{
Console.WriteLine("Authenticating {0} {1}", e.Device.DeviceAddress, e.Device.DeviceName);
Console.WriteLine(" Attempt# {0}, Last error code {1}", e.AttemptNumber, e.PreviousNativeErrorCode);
//
if (Win32AuthCallbackTwoSeparateAuthentications_count == 0) {
e.Pin = "1234";
} else if (Win32AuthCallbackTwoSeparateAuthentications_count == 1) {
e.Pin = "9876";
} else if (Win32AuthCallbackTwoSeparateAuthentications_count == 2) {
e.Pin = "sdfghjkl";
}
Console.WriteLine("Using '{0}'", e.Pin);
Win32AuthCallbackTwoSeparateAuthentications_count += 1;
}
void Win32AuthCallbackJohoHandler(Object sender, InTheHand.Net.Bluetooth.BluetoothWin32AuthenticationEventArgs e)
{
String address = 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")
|| address.Substring(0, 8).Equals("0099880E")) {
//send authentication response
e.Pin = "5276";
} else if (address.Substring(0, 8).Equals("00997788")) {
//send authentication response
e.Pin = "1423";
}
}
//The output of a standard run is the following. Note that the error code from
//the initial wrong response is error code 1244 which is ErrorNotAuthenticated.
//However when we try another attempt we get error code 1167 which is
//ErrorDeviceNotConnected. So for the devices I've tested it's apparently not
//worth trying a second response. If the user attempts to send another PIN-
//response (by setting the Pin field in the event args) then it will be ignored
//(we probably should have the event args throw when Pin is set in that case).
//
//[[
//Local radio address is 008099244999
//Authenticate two devices
//Passcode respectively: '1234', '9876', 'sdfghjkl'
//Making PC discoverable
//Hit Return to complete
//
//Complete
//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 000A99686999 Win2kBelkin
// Attempt# 0, Last error code 0
//Using '0.88516242889927'
//Authenticating 000A99686999 Win2kBelkin
// Attempt# 1, Last error code 1244
//Using '0.59947050996100'
//Authenticating 000A99686999 Win2kBelkin
// Attempt# 2, Last error code 1167
//Using '9876'
//]]
[MenuItem, SubMenu("Win32Auth")]
void Win32AuthCallbackInitialBadPasscodeAndRetry()
{
Console.WriteLine("Authenticate one device -- with wrong passcode here the first two times.");
Console.WriteLine("Passcode respectively: '{0}', '{1}', '{2}'",
"BAD-x", "BAD-y", "9876");
Win32AuthCallback__(Win32AuthCallbackInitialBadPasscodeAndRetryHandler);
}
void Win32AuthCallback__(System.EventHandler<BluetoothWin32AuthenticationEventArgs> handler)
{
Console.WriteLine("Making PC discoverable");
InTheHand.Net.Bluetooth.BluetoothRadio radio = InTheHand.Net.Bluetooth.BluetoothRadio.PrimaryRadio;
InTheHand.Net.Bluetooth.RadioMode origRadioMode = radio.Mode;
radio.Mode = InTheHand.Net.Bluetooth.RadioMode.Discoverable;
//
using (InTheHand.Net.Bluetooth.BluetoothWin32Authentication auther
= new InTheHand.Net.Bluetooth.BluetoothWin32Authentication(handler)) {
Console.WriteLine("Hit Return to complete");
Console.ReadLine();
Console.WriteLine("Complete");
}
radio.Mode = origRadioMode;
}
int Win32AuthCallbackInitialBadPasscodeAndRetry_count;
void Win32AuthCallbackInitialBadPasscodeAndRetryHandler(Object sender, InTheHand.Net.Bluetooth.BluetoothWin32AuthenticationEventArgs e)
{
Console.WriteLine("Authenticating {0} {1}", e.Device.DeviceAddress, e.Device.DeviceName);
Console.WriteLine(" Attempt# {0}, Last error code {1}", e.AttemptNumber, e.PreviousNativeErrorCode);
if (e.AttemptNumber != Win32AuthCallbackInitialBadPasscodeAndRetry_count) {
Console.WriteLine("Bad AttemptNumber!!!");
}
Random rnd = new Random();
String badPasscode = rnd.NextDouble().ToString();
badPasscode = badPasscode.Substring(0, Math.Min(16, badPasscode.Length));
//
if (Win32AuthCallbackInitialBadPasscodeAndRetry_count == 0
|| Win32AuthCallbackInitialBadPasscodeAndRetry_count == 1) {
e.Pin = badPasscode;
e.CallbackWithResult = true;
} else if (Win32AuthCallbackInitialBadPasscodeAndRetry_count == 2
|| Win32AuthCallbackInitialBadPasscodeAndRetry_count == 3) {
e.Pin = "9876";
e.CallbackWithResult = true;
} else if (Win32AuthCallbackInitialBadPasscodeAndRetry_count == 4) {
e.CallbackWithResult = true; //Try to *wrongly* get another callback!
} else {
Console.WriteLine("Unexpected callback #{0}", Win32AuthCallbackInitialBadPasscodeAndRetry_count);
}
Console.WriteLine("Using '{0}'", (e.Pin == null ? "<null>" : e.Pin));
Win32AuthCallbackInitialBadPasscodeAndRetry_count += 1;
}
[MenuItem, SubMenu("Win32Auth")]
void Win32AuthCallbackInduceFinalisationFault()
{
Console.WriteLine("Authenticate a device two times");
Console.WriteLine("Making PC discoverable");
InTheHand.Net.Bluetooth.BluetoothRadio radio = InTheHand.Net.Bluetooth.BluetoothRadio.PrimaryRadio;
InTheHand.Net.Bluetooth.RadioMode origRadioMode = radio.Mode;
radio.Mode = InTheHand.Net.Bluetooth.RadioMode.Discoverable;
//
Object auther;
auther = new InTheHand.Net.Bluetooth.BluetoothWin32Authentication(Win32AuthCallbackTwoSeparateAuthenticationsHandler);
Console.WriteLine("Hit Return to clear and GC");
Console.ReadLine();
auther = "foo";
GC.Collect();
Console.WriteLine("Hit Return to complete");
Console.ReadLine();
Console.WriteLine("Complete");
radio.Mode = origRadioMode;
}
#endif
}
}
|