// 32feet.NET - Personal Area Networking for .NET
//
// InTheHand.Windows.Forms.SelectBluetoothDeviceDialog
//
// Copyright (c) 2003-2010 In The Hand Ltd, All rights reserved.
// This source code is licensed under the In The Hand Community License - see License.txt
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;
namespace InTheHand.Windows.Forms{
/// <summary>
/// Provides a form to select an available Bluetooth device.
/// </summary>
#if WinXP
public class SelectBluetoothDeviceDialog : CommonDialog
{
private BLUETOOTH_SELECT_DEVICE_PARAMS dialogWin32;
#else
public class SelectBluetoothDeviceDialog : Component
{
#endif
private SelectBluetoothDeviceForm dialogCustom;
private ISelectBluetoothDevice dialog;
private BluetoothDeviceInfo device;
/// <summary>
/// Initializes an instance of the <see cref="SelectBluetoothDeviceDialog"/> class.
/// </summary>
public SelectBluetoothDeviceDialog()
: this(false)
{
}
internal SelectBluetoothDeviceDialog(bool forceCustomDialog)
{
#if NETCF
InTheHand.Net.PlatformVerification.ThrowException();
#endif
#if ! WinXP
// Always the custom dialog on WM/CE.
forceCustomDialog = true;
#endif
BluetoothRadio radio;
if (forceCustomDialog || (radio = BluetoothRadio.PrimaryRadio) != null && radio.SoftwareManufacturer != Manufacturer.Microsoft) {
dialogCustom = new SelectBluetoothDeviceForm();
dialog = dialogCustom;
} else {
#if WinXP
dialogWin32 = new BLUETOOTH_SELECT_DEVICE_PARAMS();
dialogWin32.Reset();
dialog = dialogWin32;
#else
Debug.Fail("Should use custom dialog on non-Win32!");
#endif
}
}
/// <summary>
/// Resets the properties of the <see cref="SelectBluetoothDeviceDialog"/> to their default values.
/// </summary>
#if WinXP
public override void Reset()
#else
public void Reset()
#endif
{
dialog.Reset();
}
#if NETCF
/// <summary>
///
/// </summary>
/// <returns></returns>
public DialogResult ShowDialog()
{
return ShowCustomDialog();
}
#endif
/// <summary>
///
/// </summary>
/// <returns></returns>
DialogResult ShowCustomDialog()
{
DialogResult dr = dialogCustom.ShowDialog();
device = dialogCustom.selectedDevice;
#if NETCF
if (ForceAuthentication) {
ForceAuthenticationCE(device);
}
#endif
return dr;
}
#if NETCF
void ForceAuthenticationCE(BluetoothDeviceInfo device)
{
int hresult;
ushort handle = 0;
//connect to device
try
{
hresult = NativeMethods.BthCreateACLConnection(device.DeviceAddress.ToByteArray(), out handle);
if (hresult != 0)
{
//success = false;
}
else
{
//force authentication
hresult = NativeMethods.BthAuthenticate(device.DeviceAddress.ToByteArray());
/*if (hresult != 0)
{
success = false;
}*/
}
}
finally
{
if (handle != 0)
{
//close connection
hresult = NativeMethods.BthCloseConnection(handle);
}
}
}
#else
/// <summary>
/// Specifies a common dialog box.
/// </summary>
/// <param name="hwndOwner">A value that represents the window handle of the owner window for the common dialog box.</param>
/// <returns>true if the dialog box was successfully run; otherwise, false.</returns>
protected override bool RunDialog(IntPtr hwndOwner)
{
if (dialogCustom == null) {
return RunDialogMsft(hwndOwner);
} else {
DialogResult rslt = ShowCustomDialog();
return rslt == DialogResult.OK;
}
}
bool RunDialogMsft(IntPtr hwndOwner)
{
//set parent window
dialogWin32.hwndParent = hwndOwner;
bool success = NativeMethods.BluetoothSelectDevices(ref dialogWin32);
if (success)
{
if (dialogWin32.cNumDevices > 0)
{
device = new BluetoothDeviceInfo(new WindowsBluetoothDeviceInfo(dialogWin32.Device));
}
bool freed = NativeMethods.BluetoothSelectDevicesFree(ref dialogWin32);
}
return success;
}
/// <summary>
/// Gets or sets the information text.
/// </summary>
/// <remarks>Supported only on Windows XP/Vista with Microsoft stack.</remarks>
public string Info
{
get
{
return dialog.Info;
}
set
{
dialog.Info = value;
}
}
/// <summary>
/// If TRUE, invokes the Add New Device Wizard.
/// </summary>
/// <remarks>Supported only on Windows XP/Vista with Microsoft stack.</remarks>
public bool AddNewDeviceWizard
{
get
{
return dialog.AddNewDeviceWizard;
}
set
{
dialog.AddNewDeviceWizard = value;
}
}
/// <summary>
/// If TRUE, skips the Services page in the Add New Device Wizard.
/// </summary>
/// <remarks>Supported only on Windows XP/Vista with Microsoft stack.</remarks>
public bool SkipServicesPage
{
get
{
return dialog.SkipServicesPage;
}
set
{
dialog.SkipServicesPage = value;
}
}
#endif
/// <summary>
/// Gets the selected Bluetooth device.
/// </summary>
public BluetoothDeviceInfo SelectedDevice
{
get
{
return device;
}
}
/// <summary>
/// If TRUE, authenticated devices are shown in the picker.
/// </summary>
public bool ShowAuthenticated
{
get
{
return dialog.ShowAuthenticated;
}
set
{
dialog.ShowAuthenticated = value;
}
}
/// <summary>
/// If TRUE, remembered devices are shown in the picker.
/// </summary>
public bool ShowRemembered
{
get
{
return dialog.ShowRemembered;
}
set
{
dialog.ShowRemembered = value;
}
}
/// <summary>
/// If TRUE, unknown devices are shown in the picker.
/// </summary>
public bool ShowUnknown
{
get
{
return dialog.ShowUnknown;
}
set
{
dialog.ShowUnknown = value;
}
}
/// <summary>
/// If TRUE, forces authentication before returning.
/// </summary>
/// <remarks></remarks>
public bool ForceAuthentication
{
get
{
return dialog.ForceAuthentication;
}
set
{
dialog.ForceAuthentication = value;
}
}
}
}
|