📄 bluetoothaddress.cs
字号:
using System;
namespace bluetoothX
{
public sealed class BluetoothAddress : IComparable, IFormattable
{
private byte[] data;
#region Constructor
internal BluetoothAddress()
{
data = new byte[8];
}
public BluetoothAddress(long address) : this()
{
BitConverter.GetBytes(address).CopyTo(data,0);
}
public BluetoothAddress(byte[] address) : this()
{
if (address == null) {
throw new ArgumentNullException("address");
}
if(address.Length == 6 || address.Length == 8)
{
Buffer.BlockCopy(address, 0, data, 0, 6);
}
else
{
throw new ArgumentException("address");
}
}
#endregion
#region Parse
public static bool TryParse(string s, out BluetoothAddress result)
{
try
{
result = Parse(s);
return true;
}
catch
{
result = null;
return false;
}
}
public static BluetoothAddress Parse(string bluetoothString)
{
if (bluetoothString == null)
{
throw new ArgumentNullException("bluetoothString");
}
BluetoothAddress ba;
if (bluetoothString.IndexOf(":") > -1)
{
//check length
if (bluetoothString.Length != 17)
{
throw new FormatException("bluetoothString is not a valid Bluetooth address.");
}
ba = new BluetoothAddress();
byte[] babytes = ba.ToByteArray();
string[] sbytes = bluetoothString.Split(':');
for(int ibyte = 0; ibyte < 6; ibyte++)
{
babytes[ibyte] = byte.Parse(sbytes[5 - ibyte],System.Globalization.NumberStyles.HexNumber);
}
}
else if (bluetoothString.IndexOf(".") > -1)
{
if (bluetoothString.Length != 17)
{
throw new FormatException("bluetoothString is not a valid Bluetooth address.");
}
ba = new BluetoothAddress();
byte[] babytes = ba.ToByteArray();
string[] sbytes = bluetoothString.Split('.');
for(int ibyte = 0; ibyte < 6; ibyte++)
{
babytes[ibyte] = byte.Parse(sbytes[5 - ibyte],System.Globalization.NumberStyles.HexNumber);
}
}
else
{
if ((bluetoothString.Length < 12) | (bluetoothString.Length > 16))
{
throw new FormatException("bluetoothString is not a valid Bluetooth address.");
}
ba = new BluetoothAddress(long.Parse(bluetoothString, System.Globalization.NumberStyles.HexNumber));
}
return ba;
}
#endregion
#region SAP
[CLSCompliant(false)]
public uint Sap
{
get
{
return BitConverter.ToUInt32(data, 0);
}
}
#endregion
#region LAP
#endregion
#region UAP
#endregion
#region NAP
[CLSCompliant(false)]
public ushort Nap
{
get
{
return BitConverter.ToUInt16(data, 4);
}
}
#endregion
#region To Byte Array
public byte[] ToByteArray()
{
return data;
}
#endregion
#region ToInt64
public long ToInt64()
{
return BitConverter.ToInt64(data, 0);
}
#endregion
#region Equals
public override bool Equals(object obj)
{
BluetoothAddress bta = obj as BluetoothAddress;
if(bta!=null)
{
return (this==bta);
}
return base.Equals (obj);
}
#endregion
#region Get Hash Code
public override int GetHashCode()
{
return this.ToInt64().GetHashCode();
}
#endregion
#region Operators
public static bool operator ==(BluetoothAddress x, BluetoothAddress y)
{
if(((object)x == null) && ((object)y == null))
{
return true;
}
if(((object)x != null) && ((object)y != null))
{
if(x.ToInt64()==y.ToInt64())
{
return true;
}
}
return false;
}
public static bool operator !=(BluetoothAddress x, BluetoothAddress y)
{
return !(x == y);
}
#endregion
#region To String
public override string ToString()
{
return this.ToString("N");
}
public string ToString(string format)
{
string separator;
if(format==null | format==string.Empty)
{
separator = string.Empty;
}
else
{
switch(format.ToUpper())
{
case "8":
case "N":
separator = string.Empty;
break;
case "C":
separator = ":";
break;
case "P":
separator = ".";
break;
default:
throw new FormatException("Invalid format specified - must be either \"N\", \"C\", \"P\", \"\" or null.");
}
}
System.Text.StringBuilder result = new System.Text.StringBuilder(18);
if (format == "8")
{
result.Append(data[7].ToString("X2") + separator);
result.Append(data[6].ToString("X2") + separator);
}
result.Append(data[5].ToString("X2") + separator);
result.Append(data[4].ToString("X2") + separator);
result.Append(data[3].ToString("X2") + separator);
result.Append(data[2].ToString("X2") + separator);
result.Append(data[1].ToString("X2") + separator);
result.Append(data[0].ToString("X2"));
return result.ToString();
}
#endregion
#region Static
public static readonly BluetoothAddress None = new BluetoothAddress();
internal const int IacFirst = 0x9E8B00;
internal const int IacLast = 0x9E8B3f;
public const int Liac = 0x9E8B00;
public const int Giac = 0x9E8B33;
#endregion
#region IComparable Members
int IComparable.CompareTo(object obj)
{
BluetoothAddress bta = obj as BluetoothAddress;
if (bta != null)
{
return this.ToInt64().CompareTo(bta.ToInt64());
}
return -1;
}
#endregion
#region IFormattable Members
public string ToString(string format, IFormatProvider formatProvider)
{
//for now just wrap existing ToString method
return ToString(format);
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -