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

📄 bluetoothdeviceinfo.cs

📁 蓝牙传输控件,用于蓝牙文件上传和下载。芯片只要选用crs
💻 CS
字号:
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using bluetoothX;
using Microsoft.Win32;



namespace bluetoothX
{
	public class BluetoothDeviceInfo : IComparable
	{
		private BLUETOOTH_DEVICE_INFO deviceInfo;

        private bool valid = false;


        #region Constructor
        public BluetoothDeviceInfo(IntPtr pDevice)
		{
            deviceInfo = new BLUETOOTH_DEVICE_INFO(0);
            Marshal.PtrToStructure(pDevice, deviceInfo);

            valid = true;

		}

        internal BluetoothDeviceInfo(BLUETOOTH_DEVICE_INFO device)
        {
            deviceInfo = device;

            valid = true;

        }

		public BluetoothDeviceInfo(BluetoothAddress address)
		{
            this.deviceInfo = new BLUETOOTH_DEVICE_INFO(address.ToInt64());

            GetDeviceInfo();
            valid = true;

		}

        #endregion

        #region GetDeviceInfo

        private void GetDeviceInfo()
        {
            if (!valid)
            {
                NativeMethods.BluetoothGetDeviceInfo(IntPtr.Zero, ref deviceInfo);
                valid = true;
            }
        }

        #endregion

        #region Refresh
        public void Refresh()
        {

            valid = false;

            deviceInfo.szName = "";
            deviceInfo.ulClassofDevice = 0;
        }
        #endregion

        #region Update
        public void Update()
        {

            int result = NativeMethods.BluetoothUpdateDeviceRecord(ref this.deviceInfo);

        }
        #endregion

        #region Address
        public BluetoothAddress DeviceAddress
		{
			get
			{
				return new BluetoothAddress(deviceInfo.Address);
			}
		}
        #endregion

        #region Name
		public string DeviceName
		{
			get
			{

				GetDeviceInfo();

                    if (string.IsNullOrEmpty(deviceInfo.szName))
                    {
                        return this.DeviceAddress.ToString("C");
                    }

                return deviceInfo.szName;

			}
            set
            {
                deviceInfo.szName = value;
            }
        }
        #endregion

        #region Class of Device
        public ClassOfDevice ClassOfDevice
        {
            get
            {

                GetDeviceInfo();

                return new ClassOfDevice(deviceInfo.ulClassofDevice);
            }
        }
        #endregion

        #region Installed Services
        public Guid[] InstalledServices
        {
            get
            {
                
                    GetDeviceInfo();

                    int nservices = 0;
                    //get the count

                    int result = NativeMethods.BluetoothEnumerateInstalledServices(IntPtr.Zero, ref deviceInfo, ref nservices, null);

                    byte[] services = new byte[nservices*16];

                    result = NativeMethods.BluetoothEnumerateInstalledServices(IntPtr.Zero, ref deviceInfo, ref nservices, services);


                    if (result < 0)
                    {
                        return new Guid[0];
                    }
                    Guid[] foundservices = new Guid[nservices];
                    for (int iservice = 0; iservice < nservices; iservice++)
                    {
                        byte[] buffer = new byte[16];
                        Buffer.BlockCopy(services, iservice * 16, buffer, 0, 16);
                        foundservices[iservice] = new Guid(buffer);
                    }
                    return foundservices;

            }
        }
        #endregion

        #region Set Service State
        public void SetServiceState(Guid service, bool state)
        {

            GetDeviceInfo();
            int hresult = NativeMethods.BluetoothSetServiceState(IntPtr.Zero, ref deviceInfo, ref service, state ? 1 : 0);

        }
        #endregion

        #region Get Service Records
        public ServiceRecord[] GetServiceRecords(Guid service)
        {
            //temporary workaround - sockets must be initialised
            Socket s = new Socket(AddressFamily32.Bluetooth, SocketType.Stream, BluetoothProtocolType.RFComm);

            //store variable length collection of records
            System.Collections.ArrayList records = new System.Collections.ArrayList();

            byte[] sdp = null;
            
            WSAQUERYSET wqs = new WSAQUERYSET();
            wqs.dwSize = 60;
            wqs.dwNameSpace = 16;

            GCHandle hservice = GCHandle.Alloc(service.ToByteArray(),GCHandleType.Pinned);
            wqs.lpServiceClassId = hservice.AddrOfPinnedObject();
            wqs.lpszContext = "(" + this.DeviceAddress.ToString("C") + ")"; // sb.ToString(); // hContext.AddrOfPinnedObject();

            
            int handle = 0;

            int lookupresult = 0;


            lookupresult = NativeMethods.WSALookupServiceBegin(ref wqs, LookupFlags.FlushCache | LookupFlags.ReturnName | LookupFlags.ReturnBlob, ref handle);

            BluetoothClient.ThrowSocketExceptionForHR(lookupresult);

            

            hservice.Free();


            while (lookupresult == 0)
            {
                byte[] sdpBuffer = new byte[6000];
                BitConverter.GetBytes(60).CopyTo(sdpBuffer, 0);
                BitConverter.GetBytes(16).CopyTo(sdpBuffer, 20);
                int size = sdpBuffer.Length;


                lookupresult = NativeMethods.WSALookupServiceNext(handle, LookupFlags.FlushCache | LookupFlags.ReturnBlob, ref size, sdpBuffer);


                if (lookupresult != -1)
                {
                    IntPtr pBlob = (IntPtr)BitConverter.ToInt32(sdpBuffer, 56);
                    if (pBlob != IntPtr.Zero)
                    {
                        IntPtr pSdpBlob = (IntPtr)Marshal.ReadInt32(pBlob, 4);
                        int cSdpBlob = Marshal.ReadInt32(pBlob);
                        sdp = new byte[cSdpBlob];
                        Marshal.Copy(pSdpBlob, sdp, 0, cSdpBlob);

                        records.Add(new ServiceRecord(sdp));
                    }

                }

            }

            lookupresult = NativeMethods.WSALookupServiceEnd(handle);
            BluetoothClient.ThrowSocketExceptionForHR(lookupresult);
          
            return (ServiceRecord[])records.ToArray(typeof(ServiceRecord));
        }
        #endregion

        #region Connected
		public bool Connected
		{
			get
			{

				GetDeviceInfo();

                return deviceInfo.fConnected;

			}
        }
        #endregion

        #region Remembered
		public bool Remembered
		{
			get
			{

				GetDeviceInfo();
                return deviceInfo.fRemembered;

			}
        }
        #endregion

        #region Authenticated
		public bool Authenticated
		{
			get
			{

                GetDeviceInfo();

                return deviceInfo.fAuthenticated;
			}

            internal set

            {
                deviceInfo.fAuthenticated = value;
            }
        }
        #endregion


        #region Last Seen
		public DateTime LastSeen
		{
			get
			{

				GetDeviceInfo();

                return deviceInfo.LastSeen;				

			}
        }
        #endregion

        #region Last Used
		public DateTime LastUsed
		{
			get
			{

				GetDeviceInfo();
                return deviceInfo.LastUsed;
               
			}
        }
        #endregion


        #region Show Dialog
		public void ShowDialog()
		{

            GetDeviceInfo();
            bool success = NativeMethods.BluetoothDisplayDeviceProperties(IntPtr.Zero, ref deviceInfo);

        }
        #endregion

        #region Equals
		public override bool Equals(object obj)
		{
			//objects are equal if device address matches
			BluetoothDeviceInfo bdi = obj as BluetoothDeviceInfo;
			
			if(bdi!=null)
			{
				return this.deviceInfo.Address.Equals(bdi.deviceInfo.Address);
			}

			return base.Equals(obj);
        }
        #endregion

        #region Get Hash Code
		public override int GetHashCode()
		{
			return this.deviceInfo.Address.GetHashCode();
        }
        #endregion


        #region IComparable Members

        int IComparable.CompareTo(object obj)
        {
            //objects are equal if device address matches
            BluetoothDeviceInfo bdi = obj as BluetoothDeviceInfo;

            if (bdi != null)
            {
                return ((IComparable)this.DeviceAddress).CompareTo(bdi);
            }

            return -1;
        }

        #endregion
    }
}

⌨️ 快捷键说明

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