Device-to-device (D2D) or Sidelink aided communication is regarded as one of the most promising technologies to improve the spectral efficiency of the 5G and beyond communication system. However, two main challenges exist: 1) the selection of the optimal number of devices for improving the spectral efficiency, and 2) improving the physical layer security of such a communication system. The optimal device improves the secrecy capacity, and the selection of optimal devices enhances the physical layer security. Therefore, in this paper, we propose a double threshold-based optimal device selection (ODS) scheme for a cooperative wireless network with amplify and forward (AF) protocol in the presence and absence of an eavesdropper to enhance the physical layer security for the D2D network. The proposed scheme is analyzed with different distance cases, device scenarios, and modulation schemes. The bit error rate (BER) performance analysis concludes that a performance gain of more than 4 dB is achieved at the BER of 0.004 for the proposed double threshold-based ODS scheme compared to the existing ODS scheme at a high signal to noise ratio (SNR). Furthermore, the proposed scheme enhances the physical layer security.