?? bluetoothmanaged.cs
字號(hào):
?using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace HTSoftware.Mobile
{
public enum BthNsLookupFlags : uint
{
//LUP_DEEP = 0x0001,
LUP_CONTAINERS = 0x0002,
//LUP_NOCONTAINERS = 0x0004,
//LUP_NEAREST = 0x0008,
LUP_RETURN_NAME = 0x0010,
LUP_RETURN_TYPE = 0x0020,
//LUP_RETURN_VERSION = 0x0040,
//LUP_RETURN_COMMENT = 0x0080,
LUP_RETURN_ADDR = 0x0100,
LUP_RETURN_BLOB = 0x0200,
//LUP_RETURN_ALIASES = 0x0400,
//LUP_RETURN_QUERY_STRING = 0x0800,
//LUP_RETURN_ALL = 0x0FF0,
LUP_RES_SERVICE = 0x8000,
//LUP_FLUSHCACHE = 0x1000,
//LUP_FLUSHPREVIOUS = 0x2000,
BTHNS_LUP_RESET_ITERATOR = 0x00010000,
BTHNS_LUP_NO_ADVANCE = 0x00020000,
BTHNS_ABORT_CURRENT_INQUIRY = 0xfffffffd
}
public enum BthHardwareStatus : int
{
HCI_HARDWARE_UNKNOWN = 0,
HCI_HARDWARE_NOT_PRESENT,
HCI_HARDWARE_INITIALIZING,
HCI_HARDWARE_RUNNING,
HCI_HARDWARE_SHUTDOWN,
HCI_HARDWARE_ERROR
}
public enum BthHardwareStatusResult : int
{
hsrSuccess = 0,
hsrServiceNotActive = 1062
}
public enum TRadioMode : int
{
Connectable = 1,
Discoverable = 2,
PowerOff = 0
}
[StructLayout(LayoutKind.Explicit, Size = 16)]
internal struct SockAddrIn
{
[FieldOffset(0)]
internal ushort family;
[FieldOffset(2)]
internal char[] addrstr; //14
[FieldOffset(2)]
internal ushort port;
[FieldOffset(4)]
internal byte[] addr; //4
[FieldOffset(8)]
internal byte[] zero; //8
}
public class SocketAddress
{
internal IntPtr pSockAddr;
internal int lSockAddr;
}
public class CSAddrInfo
{
internal SocketAddress LocalAddr, RemoteAddr;
internal int iSocketType, iProtocol;
}
internal struct TGUID
{
internal int D1;
internal ushort D2;
internal ushort D3;
internal byte[] D4; //8
}
internal enum WSAEcomparator : byte
{
COMP_EQUAL = 0,
COMP_NOTLESS
}
internal struct WSAVersion
{
internal int dwVersion;
internal WSAEcomparator ecHow;
}
internal struct AFProtocols
{
internal int iAddressFamily, iProtocol;
}
internal struct TBLOB
{
internal uint cbSize;
internal IntPtr pBlobData; //PBYTE
}
public class WSAQuerySet
{
internal int dwSize;
internal IntPtr lpszServiceInstanceName; //PWideChar
internal IntPtr lpServiceClassId; //PGUID
internal IntPtr lpVersion; //PWSAVERSION;
internal IntPtr lpszComment; //PWideChar
internal int dwNameSpace;
internal IntPtr lpNSProviderId; //PGUID;
internal IntPtr lpszContext; // PWideChar;
internal int dwNumberOfProtocols;
internal IntPtr lpafpProtocols; //PAFProtocols;
internal IntPtr lpszQueryString; //PWideChar;
internal int dwNumberOfCsAddrs;
internal IntPtr lpcsaBuffer; //PCSADDR_INFO;
internal int dwOutputFlags;
internal IntPtr lpBlob; //PBLOB;
public static int NS_BTH = 16;
public int NameSpace
{
get { return dwNameSpace; }
set { dwNameSpace = value; }
}
public String ServiceInstanceName
{
get { return Marshal.PtrToStringUni(lpszServiceInstanceName); }
}
public int NumberOfCsAddrs
{
get { return dwNumberOfCsAddrs; }
}
public CSAddrInfo CSABuffer
{
get
{
CSAddrInfo addrinfo = new CSAddrInfo();
Marshal.PtrToStructure(lpcsaBuffer, addrinfo);
return addrinfo;
}
}
public WSAQuerySet()
{
dwSize = Marshal.SizeOf(typeof(WSAQuerySet));
lpszServiceInstanceName = IntPtr.Zero;
lpServiceClassId = IntPtr.Zero;
lpVersion = IntPtr.Zero;
lpszComment = IntPtr.Zero;
dwNameSpace = NS_BTH;
lpNSProviderId = IntPtr.Zero;
lpszContext = IntPtr.Zero;
dwNumberOfProtocols = 0;
lpafpProtocols = IntPtr.Zero;
lpszQueryString = IntPtr.Zero;
dwNumberOfCsAddrs = 0;
lpcsaBuffer = IntPtr.Zero;
dwOutputFlags = 0;
lpBlob = IntPtr.Zero;
}
}
public class BluetoothManaged
{
public static int WSAQUERYSET_SIZE = 60;
public static int NS_BTH = 16;
/**/
/// <summary>
/// 查找本地藍(lán)牙DeviceID
/// </summary>
/// <param name="pba">本地藍(lán)牙DeviceID</param>
/// <returns></returns>
[DllImport("Btdrt.dll", SetLastError = true)]
public static extern int BthReadLocalAddr(byte[] pba);
/**/
/// <summary>
/// 初始化查找周圍設(shè)備
/// </summary>
/// <param name="pQuerySet">尋找條件</param>
/// <param name="dwFlags">尋找標(biāo)識(shí)(LUP_CONTAINERS / LUP_RES_SERVICE)</param>
/// <param name="lphLookup">句柄</param>
/// <returns></returns>
[DllImport("Btdrt.dll", SetLastError = true)]
public static extern int BthNsLookupServiceBegin(byte[] pQuerySet, BthNsLookupFlags dwFlags, out int lphLookup);
/**/
/// <summary>
/// 開始查找藍(lán)牙設(shè)備
/// </summary>
/// <param name="hLookup">句柄</param>
/// <param name="dwFlags">標(biāo)識(shí)(LUP_RETURN_NAME / LUP_RETURN_ADDR / LUP_RETURN_BLOB / BTHNS_LUP_RESET_ITERATOR / BTHNS_LUP_NO_ADVANCE)</param>
/// <param name="lpdwBufferLength">返回的pResults含的字節(jié)數(shù)</param>
/// <param name="pResults">WSAQUERYSET結(jié)構(gòu)字節(jié)組</param>
/// <returns></returns>
[DllImport("Btdrt.dll", SetLastError = true)]
public static extern int BthNsLookupServiceNext(int hLookup, BthNsLookupFlags dwFlags, ref int lpdwBufferLength, byte[] pResults);
//public static extern int BthNsLookupServiceNext(int hLookup, BthNsLookupFlags dwFlags, ref int lpdwBufferLength, ref WSAQuerySet QuerySet);
/**/
/// <summary>
/// 查找周圍藍(lán)牙設(shè)備結(jié)束
/// </summary>
/// <param name="hLookup"></param>
/// <returns></returns>
[DllImport("Btdrt.dll", SetLastError = true)]
public static extern int BthNsLookupServiceEnd(int hLookup);
/**/
/// <summary>
/// 獲取配對(duì)碼請(qǐng)求
/// </summary>
/// <param name="pba"></param>
/// <returns></returns>
[DllImport("Btdrt.dll", SetLastError = true)]
public static extern int BthGetPINRequest(byte[] pba);
/**/
/// <summary>
/// 設(shè)置配對(duì)碼
/// </summary>
/// <param name="pba"></param>
/// <param name="cPinLength"></param>
/// <param name="ppin"></param>
/// <returns></returns>
[DllImport("btdrt.dll", SetLastError = true)]
public static extern int BthSetPIN(byte[] pba, int cPinLength, byte[] ppin);
/**/
/// <summary>
/// 創(chuàng)建ACL連接
/// </summary>
/// <param name="pbt"></param>
/// <param name="phandle"></param>
/// <returns></returns>
[DllImport("Btdrt.dll", SetLastError = true)]
public static extern int BthCreateACLConnection(byte[] pbt, ref ushort phandle);
/**/
/// <summary>
/// 配對(duì)碼驗(yàn)證
/// </summary>
/// <param name="pbt"></param>
/// <returns></returns>
[DllImport("Btdrt.dll", SetLastError = true)]
public static extern int BthAuthenticate(byte[] pbt);
/**/
/// <summary>
/// 關(guān)閉認(rèn)證連接
/// </summary>
/// <param name="handle"></param>
/// <returns></returns>
[DllImport("Btdrt.dll", SetLastError = true)]
public static extern int BthCloseConnection(ushort handle);
//BthGetHardwareStatus
[DllImport("Btdrt.dll", SetLastError = true)]
public static extern BthHardwareStatusResult BthGetHardwareStatus(ref BthHardwareStatus pistatus);
//BthRevokePin
[DllImport("Btdrt.dll", SetLastError = true)]
public static extern int BthRevokePIN(byte[] pba);
[DllImport("ws2.dll", EntryPoint = "WSAGetLastError", SetLastError = true)]
public static extern int CeGetLastError();
[DllImport("ws2_32.dll", EntryPoint = "WSAGetLastError", SetLastError = true)]
public static extern int XpGetLastError();
//SetService
//[DllImport("Btdrt.dll", SetLastError = true)]
//public static extern int BthNsSetService(byte[] lpqsRegInfo, WSAESETSERVICEOP essoperation, int dwControlFlags);
[DllImport("BthUtil.dll")]
public static extern int BthSetMode(TRadioMode dwMode);
[DllImport("BthUtil.dll")]
public static extern int BthGetMode(out TRadioMode dwMode);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -