bthutil.cs

来自「通过手机蓝牙控制个人电脑上的幻灯片的播放」· CS 代码 · 共 91 行

CS
91
字号
#region Using directives

using System;
using System.Data;
using System.Globalization;
using System.Text;
#endregion

namespace BTHWrapper
{
    /// <summary>
    /// Summary description for BTHUtil.
    /// </summary>
    public class BTHUtil
    {
        private const int BTHADDR_COMPONENTS = 6;
        private const char BTHADDR_SEPARATOR = ':';

        public static UInt64 ToBT_ADDR(string strAddress)
        {
            UInt64 intAddress = new UInt64();
            string[] parts = strAddress.Split(new char[] { BTHADDR_SEPARATOR });
            if ((parts == null) || (parts.Length != BTHADDR_COMPONENTS))
            {
                throw new ArgumentException("Bad device address format");
            }
            for (int i = 0; i < parts.Length; i++)
            {
                byte b = byte.Parse(parts[i], NumberStyles.HexNumber);
                intAddress |= (UInt64)b << ((parts.Length - (i + 1)) * 8);
            }
            return intAddress;
        }

        public static string ToString(UInt64 intAddress)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = BTHADDR_COMPONENTS - 1; i >= 0; i--)
            {
                byte b = (byte)((intAddress >> ((i) * 8)) & 0xFF);
                if(i==(BTHADDR_COMPONENTS-1))
                {
                    sb.Append(string.Format("{0:X2}", b));
                }
                else
                {
                sb.Append(string.Format(":{0:X2}", b));
                }
            }
            return sb.ToString();
        }

        public static byte[] StringToByteArray(string str)
        {
            return System.Text.Encoding.ASCII.GetBytes(str);
        }

        public static string ByteArrayToString(byte[] byteArray)
        {
            string strRtn = System.Text.Encoding.ASCII.GetString(byteArray, 0, 50);
            return strRtn;

        }

        public static string ToStandardlizeAddress(string strAddress)
        {
            int index = 0;
            for (int i = 0; i <= 4; i++)
            {
                index = 2 + 3 * i;
                strAddress = strAddress.Insert(index, ":");
            }
            return strAddress;
        }

        public static bool IsAddressVaild(string strAddr)
        {
            try
            {
                ToBT_ADDR(strAddr);
            }
            catch
            {
                return false;
            }
            return true;
        }

    }
}

⌨️ 快捷键说明

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