/// <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 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 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. /// </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; }