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

📄 bluetooth.cs

📁 封装了一部分的蓝牙控制模块
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace HTSoftware.Mobile
{
    public delegate void LookupDeviceEventHandler(object sender, LookupDeviceEventArgs args);
    public delegate void LookupServiceEventHandler(object sender, LookupServiceEventArgs args);

    public class Bluetooth
    {
        public static bool Available
        {
            get
            {
                BthHardwareStatus hs = Status;
                return hs == BthHardwareStatus.HCI_HARDWARE_RUNNING;
            }
        }

        public static BthHardwareStatus Status
        {
            get
            {
                BthHardwareStatus hs = BthHardwareStatus.HCI_HARDWARE_ERROR;
                if (BluetoothManaged.BthGetHardwareStatus(ref hs) != BthHardwareStatusResult.hsrSuccess)
                    return BthHardwareStatus.HCI_HARDWARE_ERROR;
                else
                    return hs;
            }
        }

        public static byte[] LocalAddr
        {
            get
            {
                byte[] rsl = new byte[6];
                try
                {
                    BluetoothManaged.BthReadLocalAddr(rsl);
                }
                catch
                {
                }
                return rsl;
            }
        }

        public static String LocalAddrString
        {
            get
            {
                byte[] rsl = LocalAddr;
                return rsl[5].ToString("X2") + ":" + rsl[4].ToString("X2") + ":" + rsl[3].ToString("X2") + ":" + rsl[2].ToString("X2") + ":" + rsl[1].ToString("X2") + ":" + rsl[0].ToString("X2");
            }
        }

        public static TRadioMode RadioMode
        {
            get
            {
                TRadioMode rm;
                return (BluetoothManaged.BthGetMode(out rm) == 0) ? rm : TRadioMode.PowerOff;
            }
            set { BluetoothManaged.BthSetMode(value); }
        }

        public void DeviceLookup()
        {
            int bufferlen = 1024;
            int handle = 0;
            byte[] queryset = new byte[bufferlen];
            BitConverter.GetBytes(BluetoothManaged.WSAQUERYSET_SIZE).CopyTo(queryset, 0);
            BitConverter.GetBytes(BluetoothManaged.NS_BTH).CopyTo(queryset, 20);
            int lookupresult = BluetoothManaged.BthNsLookupServiceBegin(queryset, BthNsLookupFlags.LUP_CONTAINERS, out handle);
            while (lookupresult != -1)
            {
                lookupresult = BluetoothManaged.BthNsLookupServiceNext(handle, BthNsLookupFlags.LUP_RETURN_ADDR | BthNsLookupFlags.LUP_RETURN_NAME, ref bufferlen, queryset);
                if (lookupresult != -1 && lookupDevice != null)
                {
                    int sockaddrptr = Marshal.ReadInt32((IntPtr)BitConverter.ToInt32(queryset, 48), 8) + 8;
                    byte[] b = new byte[6];
                    System.Runtime.InteropServices.Marshal.Copy((IntPtr)sockaddrptr, b, 0, b.Length);
                    lookupDevice(this, new LookupDeviceEventArgs(b, System.Runtime.InteropServices.Marshal.PtrToStringUni((IntPtr)BitConverter.ToInt32(queryset, 4))));
                }
                System.Threading.Thread.Sleep(500);
            }
            lookupresult = BluetoothManaged.BthNsLookupServiceEnd(handle);
        }

        private LookupDeviceEventHandler lookupDevice;

        public event LookupDeviceEventHandler LookupDevice
        {
            add
            {
                lookupDevice += value;
            }
            remove
            {
                lookupDevice -= value;
            }
        }

        public void ServiceLookup()
        {
            int bufferlen = 1024;
            int handle = 0;
            byte[] queryset = new byte[bufferlen];
            BitConverter.GetBytes(BluetoothManaged.WSAQUERYSET_SIZE).CopyTo(queryset, 0);
            BitConverter.GetBytes(BluetoothManaged.NS_BTH).CopyTo(queryset, 20);
            int lookupresult = BluetoothManaged.BthNsLookupServiceBegin(queryset, BthNsLookupFlags.LUP_RES_SERVICE, out handle);
            while (lookupresult != -1)
            {
                lookupresult = BluetoothManaged.BthNsLookupServiceNext(handle, BthNsLookupFlags.LUP_RETURN_ADDR | BthNsLookupFlags.LUP_RETURN_NAME, ref bufferlen, queryset);
                if (lookupresult != -1 && lookupDevice != null)
                {
                    int sockaddrptr = Marshal.ReadInt32((IntPtr)BitConverter.ToInt32(queryset, 48), 8) + 8;
                    byte[] b = new byte[6];
                    System.Runtime.InteropServices.Marshal.Copy((IntPtr)sockaddrptr, b, 0, b.Length);
                    lookupDevice(this, new LookupDeviceEventArgs(b, System.Runtime.InteropServices.Marshal.PtrToStringUni((IntPtr)BitConverter.ToInt32(queryset, 4))));
                }
                System.Threading.Thread.Sleep(500);
            }
            lookupresult = BluetoothManaged.BthNsLookupServiceEnd(handle);
        }

        private LookupServiceEventHandler lookupService;

        public event LookupServiceEventHandler LookupService
        {
            add
            {
                lookupService += value;
            }
            remove
            {
                lookupService -= value;
            }
        }
    }
}

⌨️ 快捷键说明

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