Distance estimation methods arise in many applications, such as indoor positioning and Covid-19 contact tracing. The received signal strength indicator (RSSI) is favored in distance estimation. However, the accuracy is not satisfactory due to the signal fluctuation. Besides, the RSSI-only method has a large ranging error because it uses fixed parameters of the path loss model. Here, we propose an optimization method combining RSSI and pedestrian dead reckoning (PDR) data to estimate the distance between smart devices. The PDR may provide the high accuracy of walking distance and direction, which is used to compensate for the effects of interference on the RSSI. Moreover, the parameters of the path loss model are optimized to dynamically fit to the complex electromagnetic environment. The proposed method is evaluated in outdoor and indoor environments and is also compared with the RSSI-only method. The results show that the mean absolute error is reduced up to 0.51 m and 1.02 m, with the improvement of 10.60% and 64.55% for outdoor and indoor environments, respectively, in comparison with the RSSI-only method. Consequently, the proposed optimization method has better accuracy of distance estimation than the RSSI-only method, and its feasibility is demonstrated through real-world evaluations.