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

📄 bluetoothendpoint.cs

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

namespace bluetoothX
{
	public class BluetoothEndPoint : EndPoint
	{
		private BluetoothAddress m_id;
		private Guid m_service;
		private int m_port;

        private const int defaultPort = -1;


        #region Constructor
		public BluetoothEndPoint(BluetoothAddress address, Guid service) : this(address, service, defaultPort)
		{
		}
		public BluetoothEndPoint(BluetoothAddress address, Guid service, int port)
		{
			
			m_id = address;
			m_service = service;
			m_port = port;
        }
            #endregion

        #region Serialize
		public override SocketAddress Serialize()
		{

			SocketAddress btsa = new SocketAddress(AddressFamily32.Bluetooth, 30);

			//copy address type
			btsa[0] = 32;
			
			//copy device id
			if(m_id != null)
			{
				byte[] deviceidbytes = m_id.ToByteArray();

				for(int idbyte =0; idbyte < 6; idbyte++)
				{

					btsa[idbyte + 2] = deviceidbytes[idbyte];

				}
			}

			//copy service clsid
            if (m_service != Guid.Empty)
            {
                byte[] servicebytes = m_service.ToByteArray();
                for (int servicebyte = 0; servicebyte < 16; servicebyte++)
                {

                    btsa[servicebyte + 10] = servicebytes[servicebyte];

                }
            }
			
			//copy port
			byte[] portbytes = BitConverter.GetBytes(m_port);
			for(int portbyte = 0; portbyte < 4; portbyte++)
			{

				btsa[portbyte + 26] = portbytes[portbyte];

			}
			
			return btsa;
        }
        #endregion

        #region Create
		public override EndPoint Create(SocketAddress socketAddress)
		{
			if(socketAddress == null)
			{
				throw new ArgumentNullException("socketAddress");
			}

			//if a Bluetooth SocketAddress
			if(socketAddress[0] == 32)
			{
				int ibyte;

				byte[] addrbytes = new byte[6];
				for(ibyte = 0; ibyte < 6; ibyte++)
				{

					addrbytes[ibyte] = socketAddress[2 + ibyte];

				}
				
				byte[] servicebytes = new byte[16];
				for(ibyte = 0; ibyte < 16; ibyte++)
				{

					servicebytes[ibyte] = socketAddress[10 + ibyte];

				}

				byte[] portbytes = new byte[4];
				for(ibyte = 0; ibyte < 4; ibyte++)
				{

					portbytes[ibyte] = socketAddress[26 + ibyte];

				}
				
				return new BluetoothEndPoint(new BluetoothAddress(addrbytes), new Guid(servicebytes), BitConverter.ToInt32(portbytes, 0));
				
			}
			else
			{
				//use generic method
				return base.Create(socketAddress);
			}
        }
        #endregion

        #region Equals
		public override bool Equals(object obj)
		{
			BluetoothEndPoint bep = obj as BluetoothEndPoint;
			
			if(bep!=null)
			{
				return (this.Address.Equals(bep.Address) && this.Service.Equals(bep.Service));	
			}

			return base.Equals (obj);

        }
        #endregion

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

        #region To string
        public override string ToString()
        {
            //if port is set then use that in uri else use full service guid
            if (this.m_port != defaultPort)
            {
                return Address.ToString() + ":" + Port.ToString();
            }
            else
            {
                return Address.ToString() + ":" + Service.ToString("N");
            }
        }
        #endregion

        #region Address Family
		public override AddressFamily AddressFamily
		{
			get
			{
				return (AddressFamily)32;
			}
        }
        #endregion

        #region Address
		public BluetoothAddress Address
		{
			get
			{
				return m_id;
			}
			set
			{
				m_id = value;
			}
        }
        #endregion

        #region Service
		public Guid Service
		{
			get
			{
				return m_service;
			}
			set
			{
				m_service = value;
			}
        }
        #endregion

        #region Port
		public int Port
		{
			get
			{
				return m_port;
			}
			set
			{
				m_port = value;
			}
        }
        #endregion


        #region Consts
		public const int MinPort = 1;
		public const int MaxPort = 0xffff;
        #endregion

    }
}

⌨️ 快捷键说明

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