// "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> /// Create a client port for connection to a remote device. Auto allocates a port from the COM0-9 range. /// </summary> /// <param name="endPoint">Remote device to connect to.</param> /// <returns></returns> public static BluetoothSerialPort CreateClient(BluetoothEndPoint endPoint) { BluetoothSerialPort bsp = new BluetoothSerialPort("COM", 9); bsp.pep.flocal = false; bsp.pep.device = endPoint.Address.ToInt64(); bsp.pep.uuidService = endPoint.Service; for (int iPort = 8; iPort > -1; iPort--) { try { bsp.Register(); break; } catch { bsp.portIndex = iPort; } } if (bsp.portIndex == 0) { throw new SystemException("Unable to create a Serial Port"); } return bsp; }
/// <summary> /// Creates a copy of the <see cref="BluetoothEndPoint"/>. /// </summary> /// <remarks>Creates a copy including of the internal <see cref="T:InTheHand.Net.BluetoothAddress"/> /// </remarks> /// <returns>A copy of the <see cref="BluetoothEndPoint"/>. /// </returns> public object Clone() { BluetoothEndPoint addr2 = new BluetoothEndPoint( (BluetoothAddress)this.Address.Clone(), this.Service, this.Port); return addr2; }
/// <summary> /// Create a client port for connection to a remote device. /// </summary> /// <param name="portName">Port name e.g. "COM4"</param> /// <param name="endPoint">Remote device to connect to</param> /// <returns>A BluetoothSerialPort.</returns> public static BluetoothSerialPort CreateClient(string portName, BluetoothEndPoint endPoint) { string portPrefix; int portIndex; SplitPortName(portName, out portPrefix, out portIndex); BluetoothSerialPort bsp = new BluetoothSerialPort(portPrefix, portIndex); bsp.pep.flocal = false; bsp.pep.device = endPoint.Address.ToInt64(); bsp.pep.uuidService = endPoint.Service; bsp.Register(); return bsp; }