Hey,
The formula you’re using is indeed one of the most popular (slightly more about where it comes from in this post: Determine accurate distance of signal)
From now on you have everything you need to calculated distance. P in your formula is TxPower which beacons are transmitting as part of the package and Rssi you should get from your device. The most common n I’ve seen is 2-2.5.
As of accurate distance that’s longer story. Beacon signal is quite “unstable” and you’ll get a lot of jumping of values. So you should probably take a few values and average them over time, i.e. over last second (so called “smoothing”). And still if you have a lot of interference in your area, it’s still gonna be a bit off. Beacons are not great for calculating accurate distance. They’re more about saying you’re in close proximity or not of some object.