// 32feet.NET - Personal Area Networking for .NET
//
// InTheHand.Net.Bluetooth.NativeMethods
//
// 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.Runtime.InteropServices;
namespace InTheHand.Net.Bluetooth{
#if V2
internal static class NativeMethods
{
#else
internal sealed class NativeMethods
{
private NativeMethods(){}
#endif
// WINDEF.H
internal const int MAX_PATH = 260;
#if NETCF
private const string wsDll = "ws2.dll";
private const string btdrtDll = "btdrt.dll";
internal const string ceRegistryRoot = "\\SOFTWARE\\Microsoft\\Bluetooth\\";
#else
private const string wsDll = "ws2_32.dll";
private const string irpropsDll = "Irprops.cpl";
private const string bthpropsDll = "bthprops.cpl";
#endif
#if NETCF
[DllImport("coredll.dll", SetLastError = true)]
internal static extern int GetModuleFileName(IntPtr hModule, System.Text.StringBuilder lpFilename, int nSize);
//CE 5.0
[DllImport(btdrtDll, SetLastError = false)]
public static extern int BthAcceptSCOConnections(int fAccept);
[DllImport(btdrtDll, SetLastError=false)]
public static extern int BthAuthenticate(byte[] pbt);
[DllImport(btdrtDll, SetLastError=false)]
public static extern int BthCancelInquiry();
[DllImport(btdrtDll, SetLastError=false)]
public static extern int BthClearInquiryFilter();
[DllImport(btdrtDll, SetLastError=false)]
public static extern int BthCloseConnection(ushort handle);
[DllImport(btdrtDll, SetLastError=false)]
public static extern int BthCreateACLConnection(byte[] pbt, out ushort phandle);
[DllImport(btdrtDll, SetLastError=false)]
public static extern int BthCreateSCOConnection(byte[] pbt, out ushort phandle);
/*
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthEnterHoldMode(ref long pba, ushort hold_mode_max, ushort hold_mode_min, ref ushort pinterval);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthEnterParkMode(ref long pba, ushort beacon_max, ushort beacon_min, ref ushort pinterval);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthEnterSniffMode(ref long pba, ushort sniff_mode_max, ushort sniff_mode_min, ushort sniff_attempt, ushort sniff_timeout, ref ushort pinterval);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthExitParkMode(ref long pba);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthExitSniffMode(ref long pba);
*/
[DllImport(btdrtDll, SetLastError=false)]
public static extern int BthGetAddress(ushort handle, out long pba);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthGetBasebandConnections(int cConnections, byte[] pConnections, ref int pcConnectionsReturned);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthGetBasebandHandles(int cHandles, ref ushort pHandles, ref int pcHandlesReturned);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthGetCurrentMode(byte[] pba, ref byte pmode);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthGetHardwareStatus(ref HardwareStatus pistatus);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthGetLinkKey(byte[] pba, ref Guid key);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthGetPINRequest(byte[] pba);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthGetRemoteCOD(byte[] pbt, out uint pcod);
//CE 5.0
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthPairRequest(byte[] pba, int cPinLength, byte[] ppin);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthPerformInquiry(int LAP, byte length, byte num_responses,
int cBuffer, ref int pcDiscoveredDevices, byte[] InquiryList);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthReadAuthenticationEnable(out byte pae);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthReadCOD(out uint pcod);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthReadLinkPolicySettings(byte[] pba, ref ushort plps);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthReadLocalAddr(byte[] pba);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthReadLocalVersion(out byte phci_version, out ushort phci_revision,
out byte plmp_version, out ushort plmp_subversion, out ushort pmanufacturer, out byte plmp_features);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthReadPageTimeout(out ushort ptimeout);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthReadRemoteVersion(byte[] pba, ref byte plmp_version,
ref ushort plmp_subversion, ref ushort pmanufacturer, ref byte plmp_features);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthReadScanEnableMask(out byte pmask);
//CE 6.0
[DllImport(btdrtDll, SetLastError = true)]
public static extern int BthReadRSSI(byte[] pbt, out sbyte pbRSSI);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthRefusePINRequest(byte[] pbt);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthRemoteNameQuery(byte[] pba, int cBuffer, out int pcRequired, byte[] szString);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthRevokeLinkKey(byte[] pba);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthRevokePIN(byte[] pba);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthSetEncryption(byte[] pba, int fOn);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthSetInquiryFilter(byte[] pba);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthSetLinkKey(byte[] pba, ref Guid key);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthSetPIN(byte[] pba, int cPinLength, byte[] ppin);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthSetSecurityUI(IntPtr hEvent, int dwStoreTimeout, int dwProcTimeout);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthTerminateIdleConnections();
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthWriteAuthenticationEnable(byte ae);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthWriteCOD(uint cod);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthWriteLinkPolicySettings(byte[] pba, ushort lps);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthWritePageTimeout(ushort timeout);
[DllImport(btdrtDll, SetLastError=true)]
public static extern int BthWriteScanEnableMask(byte mask);
//Utils
[DllImport("BthUtil.dll", SetLastError=true)]
public static extern int BthSetMode(RadioMode dwMode);
[DllImport("BthUtil.dll", SetLastError=true)]
public static extern int BthGetMode(out RadioMode dwMode);
//msgqueue (for notifications)
/*
[DllImport("coredll.dll", SetLastError = true)]
internal static extern IntPtr RequestBluetoothNotifications(BTE_CLASS dwClass, IntPtr hMsgQ);
[DllImport("coredll.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool StopBluetoothNotifications(IntPtr h);
internal enum BTE
{
CONNECTION = 100,
DISCONNECTION = 101,
ROLE_SWITCH = 102,
MODE_CHANGE = 103,
PAGE_TIMEOUT = 104,
KEY_NOTIFY = 200,
KEY_REVOKED = 201,
LOCAL_NAME = 300,
COD = 301,
STACK_UP = 400,
STACK_DOWN = 401,
}
[Flags()]
internal enum BTE_CLASS
{
CONNECTIONS = 1,
PAIRING = 2,
DEVICE = 4,
STACK = 8,
}
[DllImport("coredll.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CloseMsgQueue(IntPtr hMsgQ);
[DllImport("coredll.dll", SetLastError = true)]
internal static extern IntPtr CreateMsgQueue(string lpszName, ref MSGQUEUEOPTIONS lpOptions);
[DllImport("coredll.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool ReadMsgQueue(IntPtr hMsgQ, IntPtr lpBuffer, int cbBufferSize, out int lpNumberOfBytesRead, int dwTimeout, out int pdwFlags);
[StructLayout(LayoutKind.Sequential)]
internal struct MSGQUEUEOPTIONS
{
internal int dwSize;
internal int dwFlags;
internal int dwMaxMessages;
internal int cbMaxMessage;
[MarshalAs(UnmanagedType.Bool)]
internal bool bReadAccess;
}
[StructLayout(LayoutKind.Sequential)]
internal struct BTEVENT
{
internal BTE dwEventId;
internal int dwReserved;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=64)]
internal byte[] baEventData;
}
internal struct BT_CONNECT_EVENT
{
internal int dwSize;
internal ushort hConnection;
internal long bta;
internal byte ucLinkType;
internal byte ucEncryptMode;
}
*/
#if V1
[DllImport("coredll.dll", SetLastError=true)]
public static extern int LocalFree(IntPtr ptr);
#endif
#endif
//SetService
[DllImport(wsDll, EntryPoint = "WSASetService", SetLastError = true)]
public static extern int WSASetService(ref InTheHand.Net.Sockets.WSAQUERYSET lpqsRegInfo, WSAESETSERVICEOP essoperation, int dwControlFlags);
//LookupService
[DllImport(wsDll, EntryPoint = "WSALookupServiceBegin", SetLastError = true)]
public static extern int WSALookupServiceBegin(byte[] pQuerySet, LookupFlags dwFlags, out int lphLookup);
[DllImport(wsDll, EntryPoint = "WSALookupServiceBegin", SetLastError = true)]
public static extern int WSALookupServiceBegin(ref InTheHand.Net.Sockets.WSAQUERYSET pQuerySet, LookupFlags dwFlags, out int lphLookup);
[DllImport(wsDll, EntryPoint="WSALookupServiceNext", SetLastError=true)]
public static extern int WSALookupServiceNext(int hLookup, LookupFlags dwFlags, ref int lpdwBufferLength, byte[] pResults);
[DllImport(wsDll, EntryPoint="WSALookupServiceEnd", SetLastError=true)]
public static extern int WSALookupServiceEnd(int hLookup);
#if WinXP
internal enum BTH_ERROR
{
SUCCESS = (0x00),
UNKNOWN_HCI_COMMAND = (0x01),
NO_CONNECTION = (0x02),
HARDWARE_FAILURE = (0x03),
PAGE_TIMEOUT = (0x04),
AUTHENTICATION_FAILURE = (0x05),
KEY_MISSING = (0x06),
MEMORY_FULL = (0x07),
CONNECTION_TIMEOUT = (0x08),
MAX_NUMBER_OF_CONNECTIONS = (0x09),
MAX_NUMBER_OF_SCO_CONNECTIONS = (0x0a),
ACL_CONNECTION_ALREADY_EXISTS = (0x0b),
COMMAND_DISALLOWED = (0x0c),
HOST_REJECTED_LIMITED_RESOURCES = (0x0d),
HOST_REJECTED_SECURITY_REASONS = (0x0e),
HOST_REJECTED_PERSONAL_DEVICE = (0x0f),
HOST_TIMEOUT = (0x10),
UNSUPPORTED_FEATURE_OR_PARAMETER = (0x11),
INVALID_HCI_PARAMETER = (0x12),
REMOTE_USER_ENDED_CONNECTION = (0x13),
REMOTE_LOW_RESOURCES = (0x14),
REMOTE_POWERING_OFF = (0x15),
LOCAL_HOST_TERMINATED_CONNECTION = (0x16),
REPEATED_ATTEMPTS = (0x17),
PAIRING_NOT_ALLOWED = (0x18),
UKNOWN_LMP_PDU = (0x19),
UNSUPPORTED_REMOTE_FEATURE = (0x1a),
SCO_OFFSET_REJECTED = (0x1b),
SCO_INTERVAL_REJECTED = (0x1c),
SCO_AIRMODE_REJECTED = (0x1d),
INVALID_LMP_PARAMETERS = (0x1e),
UNSPECIFIED_ERROR = (0x1f),
UNSUPPORTED_LMP_PARM_VALUE = (0x20),
ROLE_CHANGE_NOT_ALLOWED = (0x21),
LMP_RESPONSE_TIMEOUT = (0x22),
LMP_TRANSACTION_COLLISION = (0x23),
LMP_PDU_NOT_ALLOWED = (0x24),
ENCRYPTION_MODE_NOT_ACCEPTABLE = (0x25),
UNIT_KEY_NOT_USED = (0x26),
QOS_IS_NOT_SUPPORTED = (0x27),
INSTANT_PASSED = (0x28),
PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED = (0x29),
DIFFERENT_TRANSACTION_COLLISION = (0x2a),
QOS_UNACCEPTABLE_PARAMETER = (0x2c),
QOS_REJECTED = (0x2d),
CHANNEL_CLASSIFICATION_NOT_SUPPORTED = (0x2e),
INSUFFICIENT_SECURITY = (0x2f),
PARAMETER_OUT_OF_MANDATORY_RANGE = (0x30),
ROLE_SWITCH_PENDING = (0x32),
RESERVED_SLOT_VIOLATION = (0x34),
ROLE_SWITCH_FAILED = (0x35),
EXTENDED_INQUIRY_RESPONSE_TOO_LARGE = (0x36),
SECURE_SIMPLE_PAIRING_NOT_SUPPORTED_BY_HOST = (0x37),
HOST_BUSY_PAIRING = (0x38),
UNSPECIFIED = (0xFF),
}
//for bluetooth events (Win32)
[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Unicode)]
internal static extern int RegisterDeviceNotification(IntPtr hRecipient, ref DEV_BROADCAST_HANDLE NotificationFilter, int Flags);
[StructLayout(LayoutKind.Sequential)]
internal struct DEV_BROADCAST_HDR
{
internal int dbch_size;
internal int dbch_devicetype;
private int dbch_reserved;
}
[StructLayout(LayoutKind.Sequential)]
internal struct DEV_BROADCAST_HANDLE
{
internal int dbch_size;
internal int dbch_devicetype;
private int dbch_reserved;
internal IntPtr dbch_handle;
internal IntPtr dbch_hdevnotify;
internal Guid dbch_eventguid;
internal int dbch_nameoffset;
internal byte dbch_data;
}
[StructLayout(LayoutKind.Sequential)]
internal struct BTH_RADIO_IN_RANGE
{
BTH_DEVICE_INFO deviceInfo;
BDIF previousDeviceFlags;
}
internal const int DBT_DEVTYP_HANDLE = 0x00000006;
internal const int WM_DEVICECHANGE = 0x0219;
internal const int BTH_MAX_NAME_SIZE = 248;
internal const int BLUETOOTH_MAX_PASSKEY_SIZE = 16;
// {0850302A-B344-4fda-9BE9-90576B8D46F0}
internal static readonly Guid GUID_BTHPORT_DEVICE_INTERFACE = new Guid(0x850302a, 0xb344, 0x4fda, 0x9b, 0xe9, 0x90, 0x57, 0x6b, 0x8d, 0x46, 0xf0);
// This message is sent when any of the following attributes of a remote Bluetooth device has changed:
// the device has been discovered, the class of device, name, connected state, or device remembered state.
// This message is also sent when these attributes are set or cleared.
//
// {EA3B5B82-26EE-450E-B0D8-D26FE30A3869}
internal static readonly Guid GUID_BLUETOOTH_RADIO_IN_RANGE = new Guid(0xea3b5b82, 0x26ee, 0x450e, 0xb0, 0xd8, 0xd2, 0x6f, 0xe3, 0x0a, 0x38, 0x69);
// This message is sent when a previously discovered device has not been found after the completion of the last inquiry.
// This message will not be sent for remembered devices.
// The BTH_ADDRESS structure is the address of the device that was not found.
//
// {E28867C9-C2AA-4CED-B969-4570866037C4}
internal static readonly Guid GUID_BLUETOOTH_RADIO_OUT_OF_RANGE = new Guid(0xe28867c9, 0xc2aa, 0x4ced, 0xb9, 0x69, 0x45, 0x70, 0x86, 0x60, 0x37, 0xc4);
// This message should be ignored by the application.
// If the application must receive PIN requests, the BluetoothRegisterForAuthentication function should be used.
//
// BD198B7C-24AB-4B9A-8C0D-A8EA8349AA16
//internal static readonly Guid GUID_BLUETOOTH_PIN_REQUEST = new Guid(0xbd198b7c, 0x24ab, 0x4b9a, 0x8c, 0x0d, 0xa8, 0xea, 0x83, 0x49, 0xaa, 0x16);
// This message is sent when an L2CAP channel between the local radio and a remote Bluetooth device has been established or terminated.
// For L2CAP channels that are multiplexers, such as RFCOMM, this message is only sent when the underlying channel is established,
// not when each multiplexed channel, such as an RFCOMM channel, is established or terminated.
//
// {7EAE4030-B709-4AA8-AC55-E953829C9DAA}
internal static readonly Guid GUID_BLUETOOTH_L2CAP_EVENT = new Guid(0x7eae4030, 0xb709, 0x4aa8, 0xac, 0x55, 0xe9, 0x53, 0x82, 0x9c, 0x9d, 0xaa);
// This message is sent when a remote Bluetooth device connects or disconnects at the ACL level.
//
// {FC240062-1541-49BE-B463-84C4DCD7BF7F}
internal static readonly Guid GUID_BLUETOOTH_HCI_EVENT = new Guid(0xfc240062, 0x1541, 0x49be, 0xb4, 0x63, 0x84, 0xc4, 0xdc, 0xd7, 0xbf, 0x7f);
internal class BluetoothMessageFilter : System.Windows.Forms.IMessageFilter
{
#region IMessageFilter Members
public bool PreFilterMessage(ref System.Windows.Forms.Message m)
{
if (m.Msg == WM_DEVICECHANGE)
{
DEV_BROADCAST_HANDLE dbh = (DEV_BROADCAST_HANDLE)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HANDLE));
return true;
}
return false;
}
#endregion
}
//[DllImport(wsDll, EntryPoint = "WSAAddressToString", SetLastError = true)]
//public static extern int WSAAddressToString(byte[] lpsaAddress, int dwAddressLength, IntPtr lpProtocolInfo, System.Text.StringBuilder lpszAddressString, ref int lpdwAddressStringLength);
//desktop methods
/// <summary>
/// The BluetoothAuthenticateDevice function sends an authentication request to a remote Bluetooth device.
/// </summary>
/// <param name="hwndParent">The window to parent the authentication wizard.
/// If NULL, the wizard will be parented off the desktop.</param>
/// <param name="hRadio">A valid local radio handle, or NULL. If NULL, authentication is attempted on all local radios; if any radio succeeds, the function call succeeds.</param>
/// <param name="pbtdi">A structure of type BLUETOOTH_DEVICE_INFO that contains the record of the Bluetooth device to be authenticated.</param>
/// <param name="pszPasskey">A Personal Identification Number (PIN) to be used for device authentication. If set to NULL, the user interface is displayed and and the user must follow the authentication process provided in the user interface. If pszPasskey is not NULL, no user interface is displayed. If the passkey is not NULL, it must be a NULL-terminated string. For more information, see the Remarks section.</param>
/// <param name="ulPasskeyLength">The size, in characters, of pszPasskey.
/// The size of pszPasskey must be less than or equal to BLUETOOTH_MAX_PASSKEY_SIZE.</param>
/// <returns></returns>
[DllImport(irpropsDll, SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int BluetoothAuthenticateDevice(IntPtr hwndParent, IntPtr hRadio, ref BLUETOOTH_DEVICE_INFO pbtdi, string pszPasskey, int ulPasskeyLength);
/// <summary>
/// The BluetoothAuthenticateDeviceEx function sends an authentication request to a remote Bluetooth device. Additionally, this function allows for out-of-band data to be passed into the function call for the device being authenticated.
/// Note This API is supported in Windows Vista SP2 and Windows 7.
/// </summary>
/// <param name="hwndParentIn">The window to parent the authentication wizard.
/// If NULL, the wizard will be parented off the desktop.</param>
/// <param name="hRadioIn">A valid local radio handle or NULL.
/// If NULL, then all radios will be tried. If any of the radios succeed, then the call will succeed.</param>
/// <param name="pbtdiInout">A pointer to a BLUETOOTH_DEVICE_INFO structure describing the device being authenticated.</param>
/// <param name="pbtOobData">Pointer to device specific out-of-band data to be provided with this API call.
/// If NULL, then UI is displayed to continue the authentication process.
/// If not NULL, no UI is displayed.</param>
/// <param name="authenticationRequirement">An AUTHENTICATION_REQUIREMENTS enumeration that specifies the protection required for authentication.</param>
/// <returns></returns>
[DllImport(bthpropsDll, SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern int BluetoothAuthenticateDeviceEx(IntPtr hwndParentIn, IntPtr hRadioIn, ref BLUETOOTH_DEVICE_INFO pbtdiInout, byte[] pbtOobData, AUTHENTICATION_REQUIREMENTS authenticationRequirement);
// This method is deprecated and other stacks have no equivalent so do not implement
//[DllImport(irpropsDll, SetLastError = true, CharSet = CharSet.Unicode)]
//public static extern int BluetoothAuthenticateMultipleDevices(IntPtr hwndParent, IntPtr hRadio, int cDevices, BLUETOOTH_DEVICE_INFO[] rgbtdi);
[DllImport(irpropsDll, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool BluetoothDisplayDeviceProperties(IntPtr hwndParent, ref BLUETOOTH_DEVICE_INFO pbtdi);
[DllImport(irpropsDll, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool BluetoothEnableDiscovery(IntPtr hRadio, bool fEnabled);
[DllImport(irpropsDll, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool BluetoothEnableIncomingConnections(IntPtr hRadio, bool fEnabled);
[DllImport(irpropsDll, SetLastError = true)]
public static extern int BluetoothEnumerateInstalledServices(IntPtr hRadio, ref BLUETOOTH_DEVICE_INFO pbtdi, ref int pcServices, byte[] pGuidServices);
[DllImport(irpropsDll, SetLastError = true)]
public static extern int BluetoothSetServiceState(IntPtr hRadio, ref BLUETOOTH_DEVICE_INFO pbtdi, ref Guid pGuidService, int dwServiceFlags);
//Radio
[DllImport(irpropsDll, SetLastError = true)]
public static extern IntPtr BluetoothFindFirstRadio(ref BLUETOOTH_FIND_RADIO_PARAMS pbtfrp, ref IntPtr phRadio);
[DllImport(irpropsDll, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool BluetoothFindNextRadio(IntPtr hFind, ref IntPtr phRadio);
[DllImport(irpropsDll, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool BluetoothFindRadioClose(IntPtr hFind);
[DllImport(irpropsDll, SetLastError = true)]
public static extern int BluetoothGetDeviceInfo(IntPtr hRadio, ref BLUETOOTH_DEVICE_INFO pbtdi);
[DllImport(irpropsDll, SetLastError = true)]
public static extern int BluetoothGetRadioInfo(IntPtr hRadio, ref BLUETOOTH_RADIO_INFO pRadioInfo);
[DllImport(irpropsDll, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool BluetoothIsConnectable(IntPtr hRadio);
[DllImport(irpropsDll, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool BluetoothIsDiscoverable(IntPtr hRadio);
[DllImport(irpropsDll, SetLastError = true)]
public static extern int BluetoothUpdateDeviceRecord(ref BLUETOOTH_DEVICE_INFO pbtdi);
//XP SDP Parsing
[DllImport(irpropsDll, SetLastError = true)]
public static extern int BluetoothSdpGetAttributeValue(byte[] pRecordStream, int cbRecordLength, ushort usAttributeId, IntPtr pAttributeData);
[DllImport(irpropsDll, SetLastError = true)]
public static extern int BluetoothSdpGetContainerElementData(byte[] pContainerStream, uint cbContainerLength, ref IntPtr pElement, byte[] pData);
[DllImport(irpropsDll, SetLastError = true)]
public static extern int BluetoothSdpGetElementData(byte[] pSdpStream, uint cbSpdStreamLength, byte[] pData);
[DllImport(irpropsDll, SetLastError = true)]
public static extern int BluetoothSdpGetString(byte[] pRecordStream, uint cbRecordLength,
/*PSDP_STRING_DATA_TYPE*/IntPtr pStringData, ushort usStringOffset, byte[] pszString, ref uint pcchStringLength);
internal struct SDP_STRING_TYPE_DATA
{
internal ushort encoding;
internal ushort mibeNum;
internal ushort attributeID;
void ShutUpCompiler()
{
encoding = 0;
mibeNum = 0;
attributeID = 0;
}
}
[DllImport(irpropsDll, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool BluetoothSdpEnumAttributes(
IntPtr pSDPStream,
int cbStreamSize,
BluetoothEnumAttributesCallback pfnCallback,
IntPtr pvParam);
[return: MarshalAs(UnmanagedType.Bool)]
internal delegate bool BluetoothEnumAttributesCallback(
uint uAttribId,
IntPtr pValueStream,
int cbStreamSize,
IntPtr pvParam);
//Authentication functions
[DllImport(irpropsDll, SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern UInt32 BluetoothRegisterForAuthentication(
ref BLUETOOTH_DEVICE_INFO pbtdi,
out BluetoothAuthenticationRegistrationHandle phRegHandle,
BluetoothAuthenticationCallback pfnCallback,
IntPtr pvParam);
//Requires Vista SP2 or later
[DllImport(bthpropsDll, SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern UInt32 BluetoothRegisterForAuthenticationEx(
ref BLUETOOTH_DEVICE_INFO pbtdi,
out BluetoothAuthenticationRegistrationHandle phRegHandle,
BluetoothAuthenticationCallbackEx pfnCallback,
IntPtr pvParam);
[return: MarshalAs(UnmanagedType.Bool)] // Does this have any effect?
internal delegate bool BluetoothAuthenticationCallback(IntPtr pvParam, ref BLUETOOTH_DEVICE_INFO bdi);
[return: MarshalAs(UnmanagedType.Bool)] // Does this have any effect?
internal delegate bool BluetoothAuthenticationCallbackEx(IntPtr pvParam, ref BLUETOOTH_AUTHENTICATION_CALLBACK_PARAMS pAuthCallbackParams);
[DllImport(irpropsDll, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
[System.Security.SuppressUnmanagedCodeSecurity] // Since called from SafeHandle
public static extern bool BluetoothUnregisterAuthentication(IntPtr hRegHandle);
[DllImport(bthpropsDll, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
[System.Security.SuppressUnmanagedCodeSecurity] // Since called from SafeHandle
public static extern bool BluetoothUnregisterAuthenticationEx(IntPtr hRegHandle);
[DllImport(irpropsDll, SetLastError = false, CharSet = CharSet.Unicode)]
public static extern Int32 BluetoothSendAuthenticationResponse(IntPtr hRadio, ref BLUETOOTH_DEVICE_INFO pbtdi, string pszPasskey);
//[DllImport("bthprops.cpl", SetLastError = false, CharSet = CharSet.Unicode)]
//public static extern Int32 BluetoothSendAuthenticationResponseEx(IntPtr hRadio, ref BLUETOOTH_AUTHENTICATE_RESPONSE pauthResponse);
[DllImport(bthpropsDll, SetLastError = false, CharSet = CharSet.Unicode)]
public static extern Int32 BluetoothSendAuthenticationResponseEx(IntPtr hRadio, ref BLUETOOTH_AUTHENTICATE_RESPONSE__PIN_INFO pauthResponse);
[DllImport(bthpropsDll, SetLastError = false, CharSet = CharSet.Unicode)]
public static extern Int32 BluetoothSendAuthenticationResponseEx(IntPtr hRadio, ref BLUETOOTH_AUTHENTICATE_RESPONSE__OOB_DATA_INFO pauthResponse);
[DllImport(bthpropsDll, SetLastError = false, CharSet = CharSet.Unicode)]
public static extern Int32 BluetoothSendAuthenticationResponseEx(IntPtr hRadio, ref BLUETOOTH_AUTHENTICATE_RESPONSE__NUMERIC_COMPARISON_PASSKEY_INFO pauthResponse);
[DllImport(irpropsDll, SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int BluetoothRemoveDevice(byte[] pAddress);
// Code for setting radio name
// devguid.h
internal static readonly Guid GUID_DEVCLASS_BLUETOOTH = new Guid("{E0CBF06C-CD8B-4647-BB8A-263B43F0F974}");
// SETUPAPI.H
[Flags()]
internal enum DIGCF
{
PRESENT = 0x00000002, // Return only devices that are currently present in a system.
ALLCLASSES = 0x00000004, // Return a list of installed devices for all device setup classes or all device interface classes.
PROFILE = 0x00000008, // Return only devices that are a part of the current hardware profile.
}
internal enum SPDRP
{
HARDWAREID = 0x00000001, // HardwareID (R/W)
DRIVER = 0x00000009, // Driver (R/W)
}
// WinError.h
internal const int ERROR_INSUFFICIENT_BUFFER = 0x7A;
internal const int ERROR_SUCCESS = 0;
[StructLayout(LayoutKind.Sequential)]
internal struct SP_DEVINFO_DATA
{
internal int cbSize; // = (uint)Marshal.SizeOf(typeof(SP_DEVINFO_DATA));
internal Guid ClassGuid;
internal UInt32 DevInst;
internal IntPtr Reserved;
}
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern bool DeviceIoControl(
IntPtr hDevice,
uint dwIoControlCode,
ref long InBuffer,
int nInBufferSize,
IntPtr OutBuffer,
int nOutBufferSize,
ref int pBytesReturned,
IntPtr lpOverlapped);
// The SetupDiGetClassDevs function returns a handle to a device information set that contains requested device information elements for a local machine.
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern IntPtr SetupDiGetClassDevs(
ref Guid classGuid,
[MarshalAs(UnmanagedType.LPTStr)] string enumerator,
IntPtr hwndParent,
DIGCF flags);
// The SetupDiEnumDeviceInfo function returns a SP_DEVINFO_DATA structure that specifies a device information element in a device information set.
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool SetupDiEnumDeviceInfo(
IntPtr deviceInfoSet,
uint memberIndex,
ref SP_DEVINFO_DATA deviceInfoData);
// The SetupDiDestroyDeviceInfoList function deletes a device information set and frees all associated memory.
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool SetupDiDestroyDeviceInfoList(IntPtr deviceInfoSet);
// The SetupDiGetDeviceInstanceId function retrieves the device instance ID that is associated with a device information element.
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool SetupDiGetDeviceInstanceId(
IntPtr deviceInfoSet,
ref SP_DEVINFO_DATA deviceInfoData,
System.Text.StringBuilder deviceInstanceId,
int deviceInstanceIdSize,
out int requiredSize);
//The BluetoothIsVersionAvailable function indicates if the installed Bluetooth binary set supports the requested version.
//Requires Windows XP SP2 or later
[DllImport(bthpropsDll, SetLastError = false, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool BluetoothIsVersionAvailable(byte MajorVersion, byte MinorVersion
);
#endif
}
internal enum WSAESETSERVICEOP : int
{
/// <summary>
/// Register the service. For SAP, this means sending out a periodic broadcast.
/// This is an NOP for the DNS namespace.
/// For persistent data stores, this means updating the address information.
/// </summary>
RNRSERVICE_REGISTER = 0,
/// <summary>
/// Remove the service from the registry.
/// For SAP, this means stop sending out the periodic broadcast.
/// This is an NOP for the DNS namespace.
/// For persistent data stores this means deleting address information.
/// </summary>
RNRSERVICE_DEREGISTER,
/// <summary>
/// Delete the service from dynamic name and persistent spaces.
/// For services represented by multiple CSADDR_INFO structures (using the SERVICE_MULTIPLE flag), only the specified address will be deleted, and this must match exactly the corresponding CSADDR_INFO structure that was specified when the service was registered
/// </summary>
RNRSERVICE_DELETE,
}
[Flags()]
internal enum LookupFlags : uint
{
Containers = 0x0002,
ReturnName = 0x0010,
ReturnAddr = 0x0100,
ReturnBlob = 0x0200,
FlushCache = 0x1000,
ResService = 0x8000,
}
[Flags()]
internal enum ScanMask : byte
{
None = 0,
Inquiry = 1,
Page = 2,
}
}
|