Device-to-Device (D2D) based communication plays a major part in improving the 5G network’s system capacity. In pursuance of accomplishing this, effective resource and power allocation algorithms have to be designed for D2D users. As D2D users are considered as secondary users, their interference with cellular users (CUs) should not restrict the communication which is performed based on CU. Most of the existing methodologies performed on bandwidth allocation assume full Channel State Information (CSI) at the base station (BS), which resulted in uncertainty resource allocation. Hence to resolve this, the proposed scheme is developed with an efficient handoff strategy based on bandwidth allocation using Thompson Sampling Bandwidth (TSB) and Magnetic Force Optimization (MFO) for D2D and CU communications along with guaranteed QoS constraint. Here, the available free bandwidth is selected using the TSB technique which in turn is based on overall data rate, level of interference and number of CU users. In this, Magnetic Force optimization (MFO) is utilized for base station selection. This is done based on signal strength variation, coverage area between user and base station, and bandwidth capacity. Thus it results in guaranteed QoS constraint, i.e., parameters like Packet delay; throughput, data rate and reduction in allocated sub-carriers are attained. Finally, the simulation outcomes are performed and the results are estimated in terms of average delay, throughput, node deployment, and data rate. Therefore, the comparative analysis is carried out between the attained outcomes with that of the existing techniques to prove the efficacy of the proposed TSB-MFO technique.