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

📄 servicerecord.cs

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

namespace bluetoothX
{
    public class ServiceRecord
    {
        private byte[] record;

        public ServiceRecord(byte[] record)
        {
            this.record = record;
        }


        public object GetAttribute(int attribute)
        {
            IntPtr pData = Marshal.AllocHGlobal(24);
            int hresult = NativeMethods.BluetoothSdpGetAttributeValue(record, record.Length, Convert.ToUInt16(attribute), pData);

            if (hresult < 0)
            {
                return null;
            }

            SdpSpecificType type = (SdpSpecificType)Marshal.ReadInt32(pData, 4);
            object val = null;
            switch (type)
            {
                case SdpSpecificType.Int16:
                    val = Marshal.ReadInt16(pData, 16);
                    break;
                case SdpSpecificType.Int32:
                    val = Marshal.ReadInt32(pData, 16);
                    break;
                case SdpSpecificType.Int64:
                    val = Marshal.ReadInt64(pData, 16);
                    break;
                case SdpSpecificType.Int8:
                    val = Marshal.ReadByte(pData, 16);
                    break;
                case SdpSpecificType.Uuid16:
                case SdpSpecificType.UInt16:
                    val = Marshal.PtrToStructure(new IntPtr(pData.ToInt32() + 16), typeof(UInt16));
                    break;
                case SdpSpecificType.Uuid32:
                case SdpSpecificType.UInt32:
                    val = Marshal.PtrToStructure(new IntPtr(pData.ToInt32() + 16), typeof(UInt32));
                    break;
                case SdpSpecificType.UInt64:
                    val = Marshal.PtrToStructure(new IntPtr(pData.ToInt32() + 16), typeof(UInt64));
                    break;
                case SdpSpecificType.Int128:
                case SdpSpecificType.Uuid128:
                    val = Marshal.PtrToStructure(new IntPtr(pData.ToInt32() + 16), typeof(Guid));
                    break;
                case SdpSpecificType.UInt8:
                    val = Marshal.PtrToStructure(new IntPtr(pData.ToInt32() + 16), typeof(sbyte));
                    break;
            }
            Marshal.FreeHGlobal(pData);

            return val;
        }

        public byte[] ToByteArray()
        {
            return this.record;
        }
    }
}

⌨️ 快捷键说明

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