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

📄 bluetoothclient.cs

📁 蓝牙通讯
💻 CS
📖 第 1 页 / 共 2 页
字号:

		#region Authentication Enabled
		/// <summary>
		/// Gets a value to indicated whether Authentication is enabled.
		/// </summary>
		public bool AuthenticationEnabled
		{
			get
			{
				byte[] authbytes = mSocket.GetSocketOption((SocketOptionLevel)BluetoothSocketOptionLevel.RFComm, (SocketOptionName)BluetoothSocketOptionName.GetAuthenticationEnabled, 4);
				int auth = BitConverter.ToInt32(authbytes, 0);
				return auth==0 ? false : true;
				
			}
		}
		#endregion

		#region Class Of Device
		/// <summary>
		/// Returns the Class of Device for the local device.
		/// </summary>
		public ClassOfDevice ClassOfDevice
		{
			get
			{
				byte[] codbytes = mSocket.GetSocketOption((SocketOptionLevel)BluetoothSocketOptionLevel.RFComm, (SocketOptionName)BluetoothSocketOptionName.GetCod, 4);
				int cod = BitConverter.ToInt32(codbytes, 0);
				return (ClassOfDevice)cod;
			}
		}
		#endregion

		#region Encrypt
		/// <summary>
		/// 
		/// </summary>
		public bool Encrypt
		{
			set
			{
				mSocket.SetSocketOption((SocketOptionLevel)BluetoothSocketOptionLevel.RFComm, (SocketOptionName)BluetoothSocketOptionName.Encrypt, value ? -1 : 0);
			}
		}
		#endregion

		#region Hardware Status
		/// <summary>
		/// Returns the current status of the Bluetooth radio hardware.
		/// </summary>
		/// <value>A member of the <see cref="OpenNETCF.Net.Bluetooth.HardwareStatus"/> enumeration.</value>
		public HardwareStatus HardwareStatus
		{
			get
			{
				HardwareStatus status = 0;
				int result = NativeMethods.BthGetHardwareStatus(ref status);

				if(result!=0)
				{
					throw new ExternalException("Error retrieving Bluetooth hardware status");
				}
				return status;
			}
		}
			
		#endregion

		#region Link Key
		/// <summary>
		/// Returns link key associated with peer Bluetooth device.
		/// </summary>
		public Guid LinkKey
		{
			get
			{
				byte[] link = mSocket.GetSocketOption((SocketOptionLevel)BluetoothSocketOptionLevel.RFComm, (SocketOptionName)BluetoothSocketOptionName.GetLink, 32);

				byte[] bytes = new byte[16];
				Buffer.BlockCopy(link, 16, bytes, 0, 16);
				return new Guid(bytes);
			}
		}
		#endregion

		#region Link Policy
		/// <summary>
		/// Returns the Link Policy of the current connection.
		/// </summary>
		public LinkPolicy LinkPolicy
		{
			get
			{
				byte[] policy = mSocket.GetSocketOption((SocketOptionLevel)BluetoothSocketOptionLevel.RFComm, (SocketOptionName)BluetoothSocketOptionName.GetLinkPolicy, 4);
				return (LinkPolicy)BitConverter.ToInt32(policy, 0);
			}
		}
		#endregion

		#region Local Address
		/// <summary>
		/// Returns the address of the local Bluetooth device.
		/// </summary>
		public BluetoothAddress LocalAddress
		{
			get
			{
				BluetoothAddress ba = new BluetoothAddress();

				int result = NativeMethods.BthReadLocalAddr(ba.ToByteArray());

				if(result != 0)
				{
					throw new ExternalException("Error retrieving local Bluetooth address");
				}

				return ba;
			}
		}
		
		#endregion

		#region Local Version

		public BluetoothVersion LocalVersion
		{
			get
			{
				if(System.Environment.OSVersion.Platform==PlatformID.WinCE)
				{
					byte hv = 0;
					ushort hr = 0;
					byte lv = 0;
					ushort ls = 0;
					ushort man = 0;
					byte fea = 0;

					int hresult = BthReadLocalVersion(ref hv, ref hr, ref lv, ref ls, ref man, ref fea);

					return new BluetoothVersion(hv, hr, lv, ls, man, fea);
				}
				else
				{
					byte[] version = mSocket.GetSocketOption((SocketOptionLevel)BluetoothSocketOptionLevel.RFComm, (SocketOptionName)BluetoothSocketOptionName.GetLocalVersion, 16);
					return new BluetoothVersion(version);
				}
			}
		}
		[DllImport("Btdrt.dll", SetLastError=true)]
		private static extern int BthReadLocalVersion(
			ref byte phci_version,
			ref ushort phci_revision,
			ref byte plmp_version,
			ref ushort plmp_subversion,
			ref ushort pmanufacturer,
			ref byte plmp_features);
		#endregion

		#region PairRequest
		/*
		/// <summary>
		/// Attempts a pairing with the specified device and PIN number.
		/// </summary>
		/// <param name="device"></param>
		/// <param name="pin"></param>
		public void PairRequest(BluetoothAddress device, string pin)
		{
			ushort handle = 0;

			SetPin(device, pin);

			int success = NativeMethods.BthCreateACLConnection(device.ToByteArray(), ref handle);

			success = NativeMethods.BthAuthenticate(device.ToByteArray());

			success = NativeMethods.BthCloseConnection(handle);
		}*/
		#endregion

		#region Pin
		/// <summary>
		/// Sets the PIN associated with the currently connected device.
		/// </summary>
		/// <value>PIN which must be composed of 1 to 16 ASCII characters.
		/// Assigning null (Nothing in VB) or an empty String will revoke the PIN.</value>
		public string Pin
		{
			set
			{
				SetPin(null, value);
			}
		}

		/// <summary>
		/// Set or change the PIN to be used with a specific remote device.
		/// </summary>
		/// <param name="device">Address of Bluetooth device.</param>
		/// <param name="pin">PIN string consisting of 1-16 ASCII characters.</param>
		public void SetPin(BluetoothAddress device, string pin)
		{
			if(pin.Length < 1 | pin.Length > 16)
			{
				throw new ArgumentException("Pin must be between 1 and 16 characters long.");
			}
			byte[] pinbytes = System.Text.Encoding.ASCII.GetBytes(pin);
			int len = pin.Length;
			
			int result = NativeMethods.BthSetPIN(device.ToByteArray(), len, pinbytes);

			if(result != 0)
			{
				int error = Marshal.GetLastWin32Error();
				throw new System.ComponentModel.Win32Exception(error, "Error setting PIN");
			}

			/*byte[] link = new byte[32];

			//copy remote device address
			if(device != null)
			{
				Buffer.BlockCopy(device.ToByteArray(), 0, link, 8, 6);
			}

			//copy PIN
			if(pin!=null & pin.Length > 0)
			{
				if(pin.Length > 16)
				{
					throw new ArgumentOutOfRangeException("PIN must be between 1 and 16 characters");
				}
				//copy pin bytes
				byte[] pinbytes = System.Text.Encoding.ASCII.GetBytes(pin);
				Buffer.BlockCopy(pinbytes, 0, link, 16, pin.Length);
				BitConverter.GetBytes(pin.Length).CopyTo(link, 0);
			}	
				
			mSocket.SetSocketOption((SocketOptionLevel)BluetoothSocketOptionLevel.RFComm, (SocketOptionName)BluetoothSocketOptionName.SetPin, link);*/
		}

		/// <summary>
		/// Revoke a previously assigned PIN for connecting to the specified device.
		/// </summary>
		/// <param name="device">Address of Bluetooth device.</param>
		public void RevokePin(BluetoothAddress device)
		{
			int result = NativeMethods.BthRevokePIN(device.ToByteArray());

			if(result != 0)
			{
				int error = Marshal.GetLastWin32Error();
				throw new System.ComponentModel.Win32Exception(error, "Error setting PIN");
			}

			//SetPin(device, null);
		}
		#endregion

		#region Remote Machine Name
		/// <summary>
		/// Gets the name of the remote device.
		/// </summary>
		public string RemoteMachineName
		{
			get
			{
				return GetRemoteMachineName(mSocket);
			}
		}
		

		/// <summary>
		/// Gets the name of a device by a specified socket.
		/// </summary>
		/// <param name="s"> A <see cref="Socket"/>.</param>
		/// <returns>Returns a string value of the computer or device name.</returns>
		public static string GetRemoteMachineName(Socket s)
		{
			byte[] buffer = new byte[504];
			//copy remote device address to buffer
			Buffer.BlockCopy(((BluetoothEndPoint)s.RemoteEndPoint).Address.ToByteArray(), 0, buffer, 0, 6);

			try
			{
				s.SetSocketOption((SocketOptionLevel)BluetoothSocketOptionLevel.RFComm, (SocketOptionName)BluetoothSocketOptionName.ReadRemoteName, buffer);
				string name = System.Text.Encoding.Unicode.GetString(buffer, 8, 496);
				int offset = name.IndexOf('\0');
				if(offset > -1)
				{
					name = name.Substring(0, offset);
				}

				return name;
			}
			catch(SocketException ex)
			{
				return null;
			}

		}
		#endregion

		#region IDisposable Members

		protected virtual void Dispose(bool disposing)
		{
			if(mSocket != null)
			{
				if(mSocket.Connected)
				{
					mSocket.Close();
				}

				mSocket = null;
			}
		}

		void IDisposable.Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		/// <summary>
		/// Frees resources used by the <see cref="BluetoothClient"/> class.
		/// </summary>
		~BluetoothClient()
		{
			Dispose(false);
		}

		#endregion
	}

	

	
}

⌨️ 快捷键说明

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