// "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 BluetoothSerialPort instance from an existing open virtual serial port handle. /// </summary> /// <param name="handle">Handle value created previously by BluetoothSerialPort.</param> /// <returns>BluetoothSerialPort wrapper around handle.</returns> public static BluetoothSerialPort FromHandle(IntPtr handle) { BluetoothSerialPort bsp = new BluetoothSerialPort("COM", 0); bsp.handle = handle; return bsp; }
/// <summary> /// Create a virtual server port to listen for incoming connections. Auto allocates a port from the COM0-9 range. /// </summary> /// <param name="service">Service GUID to listen on.</param> /// <returns></returns> public static BluetoothSerialPort CreateServer(Guid service) { BluetoothSerialPort bsp = new BluetoothSerialPort("COM", 9); bsp.pep.flocal = true; bsp.pep.uuidService = 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> /// Create a virtual server port to listen for incoming connections. /// </summary> /// <param name="portName">Port name e.g. "COM4"</param> /// <param name="service">Bluetooth service to listen on.</param> /// <returns></returns> public static BluetoothSerialPort CreateServer(string portName, Guid service) { string portPrefix; int portIndex; SplitPortName(portName, out portPrefix, out portIndex); BluetoothSerialPort bsp = new BluetoothSerialPort(portPrefix, portIndex); bsp.pep.flocal = true; bsp.pep.uuidService = service; bsp.Register(); return bsp; }
/// <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> /// 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; }