// "0006660309e8" // or 00.06.66.03.09.e8 // or 00:06:66:03:09:e8 public btCOM(string sBTaddress) { bta = BluetoothAddress.Parse(sBTaddress); bep = new BluetoothEndPoint(bta, BluetoothService.SerialPort); string cPre="COM"; int iIdx=0; bool bSuccess = false; for (iIdx = 9; iIdx >= 0; iIdx--) { try{ btSer = BluetoothSerialPort.CreateClient(cPre, iIdx, bep); bSuccess = true; break; } catch (Exception ex) { addLog("CreateClient failed for '" + cPre + iIdx.ToString() + ":', " + ex.Message); } } if (!bSuccess) { cPre = "BSP"; for (iIdx = 9; iIdx >= 0; iIdx--) { try { btSer = BluetoothSerialPort.CreateClient(cPre, iIdx, bep); bSuccess = true; break; } catch (Exception ex) { addLog("CreateClient failed for '" + cPre + iIdx.ToString() + ":', " + ex.Message); } } } if (bSuccess) { sCom = cPre + iIdx.ToString() + ":"; bRegistered = true; } }
/// <summary> /// Creates a copy of the <see cref="BluetoothAddress"/>. /// </summary> /// <remarks>Creates a copy including of the internal byte array. /// </remarks> /// <returns>A copy of the <see cref="BluetoothAddress"/>. /// </returns> public object Clone() { BluetoothAddress addr2 = new BluetoothAddress(); addr2.data = (byte[])this.data.Clone(); return addr2; }
//[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Returned to caller.")] static Exception ParseInternal(string bluetoothString, out BluetoothAddress result) { const Exception Success = null; result = null; if (bluetoothString == null) { return new ArgumentNullException("bluetoothString"); } if (bluetoothString.IndexOf(":", StringComparison.Ordinal) > -1) { //assume address in standard hex format 00:00:00:00:00:00 //check length if (bluetoothString.Length != 17) { return new FormatException("bluetoothString is not a valid Bluetooth address."); } try { byte[] babytes = new byte[8]; //split on colons string[] sbytes = bluetoothString.Split(':'); for (int ibyte = 0; ibyte < 6; ibyte++) { //parse hex byte in reverse order babytes[ibyte] = byte.Parse(sbytes[5 - ibyte], System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture); } result = new BluetoothAddress(babytes); return Success; } catch (Exception ex) { return ex; } } else if (bluetoothString.IndexOf(".", StringComparison.Ordinal) > -1) { //assume address in uri hex format 00.00.00.00.00.00 //check length if (bluetoothString.Length != 17) { return new FormatException("bluetoothString is not a valid Bluetooth address."); } try { byte[] babytes = new byte[8]; //split on periods string[] sbytes = bluetoothString.Split('.'); for (int ibyte = 0; ibyte < 6; ibyte++) { //parse hex byte in reverse order babytes[ibyte] = byte.Parse(sbytes[5 - ibyte], System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture); } result = new BluetoothAddress(babytes); return Success; } catch (Exception ex) { return ex; } } else { //assume specified as long integer if ((bluetoothString.Length < 12) | (bluetoothString.Length > 16)) { return new FormatException("bluetoothString is not a valid Bluetooth address."); } try { result = new BluetoothAddress(long.Parse(bluetoothString, System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture)); return Success; } catch (Exception ex) { return ex; } } }
/// <summary> /// Converts the string representation of an address to it's <see cref="BluetoothAddress"/> equivalent. /// A return value indicates whether the operation succeeded. /// </summary> /// <param name="bluetoothString">A string containing an address to convert.</param> /// <param name="result">When this method returns, contains the <see cref="BluetoothAddress"/> equivalent to the address contained in s, if the conversion succeeded, or null (Nothing in Visual Basic) if the conversion failed. /// The conversion fails if the s parameter is null or is not of the correct format.</param> /// <returns>true if s is a valid Bluetooth address; otherwise, false.</returns> public static bool TryParse(string bluetoothString, out BluetoothAddress result) { Exception ex = ParseInternal(bluetoothString, out result); if (ex != null) return false; else return true; }
/// <summary> /// Initializes a new instance of the <see cref="BluetoothEndPoint"/> class with the specified address, service and port number. /// </summary> /// <param name="address">The Bluetooth address of the device. A six byte array.</param> /// <param name="service">The Bluetooth service to use.</param> /// <param name="port">Radio channel to use, -1 for any.</param> /// - /// <remarks> /// <para>See the <see cref="T:InTheHand.Net.BluetoothEndPoint"/> documentation for /// how the combination of Service and Port are used when connecting with /// BluetoothClient. /// </para> /// </remarks> public BluetoothEndPoint(BluetoothAddress address, Guid service, int port) { //Debug.Assert(address != null, "NULL address"); m_id = address; m_service = service; m_port = port; }
/// <summary> /// Initializes a new instance of the <see cref="BluetoothEndPoint"/> class with the specified address and service. /// </summary> /// <param name="address">The Bluetooth address of the device. A six byte array.</param> /// <param name="service">The Bluetooth service to use.</param> public BluetoothEndPoint(BluetoothAddress address, Guid service) : this(address, service, defaultPort) { }
public PortStatusChangedEventArgs(bool connected, string portName, BluetoothAddress address) { Connected = connected; PortName = portName; Address = address; }