Using Corelocation.framework is easy to scan to get peripheral Bluetooth devices, Apple open source code airlocate has a specific implementation:
Https://developer.apple.com/library/ios/samplecode/AirLocate/Introduction/Intro.html
The obtained ibeacon is expressed in Clbeacon in Corelocation, which has the RSSI value (Received signal strength), which can be used to calculate the distance between the transmitting end and the receiving end.
Calculation formula:
D = 10^ ((ABS (RSSI)-A)/(Ten * N))
which
D-Calculate the resulting distance
RSSI-received signal strength (negative value)
A-Signal strength at a distance of 1 m from the transmitting and receiving ends
N-Environmental attenuation factor
Code implementation of the calculation formula
-(float) Calcdistbyrssi: (int) rssi{ int irssi = ABS (RSSI); Float power = (iRssi-59)/(10*2.0); Return pow (power);}
The Rssi value is passed in, and the distance (in meters) is returned. Where a parameter is assigned a 59,N 2.0.
Due to the different environment, each transmitter source (Bluetooth device) corresponding parameter values are not the same. According to the truth, each parameter in the formula should be tested (calibrated).
When you do not know the exact location of the surrounding Bluetooth devices, you can only give experience values for a and n (as in this example).
Modify Airlocate aplrangingviewcontroller.m Display part of the code, the output calculation distance
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{static NSString *identifier = @ "Cell"; UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:identifier]; Display the UUID, major, minor and accuracy for each beacon. NSNumber *sectionkey = [Self.beacons allkeys][indexpath.section]; Clbeacon *beacon = Self.beacons[sectionkey][indexpath.row]; Cell.textLabel.text = [Beacon.proximityuuid uuidstring];//NSLog (@ "%@", [Beacon.proximityuuid uuidstring]);//Nsstri ng *formatstring = nslocalizedstring (@ "Major:%@, Minor:%@, Acc:%.2FM, Rssi:%d, Dis:%.2f", @ "Format string for ranging Table cells. "); /Cell.detailTextLabel.text = [NSString stringwithformat:formatstring, Beacon.major, Beacon.minor, beacon.accuracy, be Acon.rssi, [self calcDistByRSSI:beacon.rssi]]; NSString *formatstring = nslocalizedstring (@ "ACC:%.2fm, Rssi:%d, Dis:%.2fm", @ "Format string for ranging table cells." ); Cell.detailtExtlabel.text = [NSString stringwithformat:formatstring, Beacon.accuracy, Beacon.rssi, [self CalcDistByRSSI: Beacon.rssi]]; return cell;}
Scan results
Shows the ACC (precision), Rssi (signal strength), and dis (distance) for each Bluetooth device.
Bluetooth RSSI calculation distance