⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bluetoothmanaged.cs

📁 封装了一部分的蓝牙控制模块
💻 CS
字号:
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>
        /// 查找本地蓝牙DeviceID
        /// </summary>
        /// <param name="pba">本地蓝牙DeviceID</param>
        /// <returns></returns>
        [DllImport("Btdrt.dll", SetLastError = true)]
        public static extern int BthReadLocalAddr(byte[] pba);

        /**/
        /// <summary>
        /// 初始化查找周围设备
        /// </summary>
        /// <param name="pQuerySet">寻找条件</param>
        /// <param name="dwFlags">寻找标识(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>
        /// 开始查找蓝牙设备
        /// </summary>
        /// <param name="hLookup">句柄</param>
        /// <param name="dwFlags">标识(LUP_RETURN_NAME / LUP_RETURN_ADDR / LUP_RETURN_BLOB / BTHNS_LUP_RESET_ITERATOR / BTHNS_LUP_NO_ADVANCE)</param>
        /// <param name="lpdwBufferLength">返回的pResults含的字节数</param>
        /// <param name="pResults">WSAQUERYSET结构字节组</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>
        /// 查找周围蓝牙设备结束
        /// </summary>
        /// <param name="hLookup"></param>
        /// <returns></returns>
        [DllImport("Btdrt.dll", SetLastError = true)]
        public static extern int BthNsLookupServiceEnd(int hLookup);

        /**/
        /// <summary>
        /// 获取配对码请求
        /// </summary>
        /// <param name="pba"></param>
        /// <returns></returns>
        [DllImport("Btdrt.dll", SetLastError = true)]
        public static extern int BthGetPINRequest(byte[] pba);

        /**/
        /// <summary>
        /// 设置配对码
        /// </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>
        /// 创建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>
        /// 配对码验证
        /// </summary>
        /// <param name="pbt"></param>
        /// <returns></returns>
        [DllImport("Btdrt.dll", SetLastError = true)]
        public static extern int BthAuthenticate(byte[] pbt);

        /**/
        /// <summary>
        /// 关闭认证连接
        /// </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);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -