[轉] 藍牙RSSI計算距離


利用CoreLocation.framework很容易掃描獲得周邊藍牙設備,蘋果開源代碼AirLocate有具體實現,下載地址:

https://developer.apple.com/library/ios/samplecode/AirLocate/Introduction/Intro.html

所獲得的iBeacon在CoreLocation里以CLBeacon表示,其中有RSSI值(接收信號強度),可以用來計算發射端和接收端間距離。

 

計算公式:

    d = 10^((abs(RSSI) - A) / (10 * n))

其中:

    d - 計算所得距離

    RSSI - 接收信號強度(負值)

    A - 發射端和接收端相隔1米時的信號強度

    n - 環境衰減因子

 

 

計算公式的代碼實現

 

[objc]  view plain copy
  1. - (float)calcDistByRSSI:(int)rssi  
  2. {  
  3.     int iRssi = abs(rssi);  
  4.     float power = (iRssi-59)/(10*2.0);  
  5.     return pow(10, power);  
  6. }  

傳入RSSI值,返回距離(單位:米)。其中,A參數賦了59,n賦了2.0。

 

由於所處環境不同,每台發射源(藍牙設備)對應參數值都不一樣。按道理,公式里的每項參數都應該做實驗(校准)獲得。

當你不知道周圍藍牙設備准確位置時,只能給A和n賦經驗值(如本例)。

 

修改AirLocate的APLRangingViewController.m展現部分代碼,輸出計算距離

 

[objc]  view plain copy
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     static NSString *identifier = @"Cell";  
  4.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];  
  5.       
  6.     // Display the UUID, major, minor and accuracy for each beacon.  
  7.     NSNumber *sectionKey = [self.beacons allKeys][indexPath.section];  
  8.     CLBeacon *beacon = self.beacons[sectionKey][indexPath.row];  
  9.     cell.textLabel.text = [beacon.proximityUUID UUIDString];  
  10. //    NSLog(@"%@", [beacon.proximityUUID UUIDString]);  
  11.   
  12.   
  13. //    NSString *formatString = NSLocalizedString(@"Major: %@, Minor: %@, Acc: %.2fm, Rssi: %d, Dis: %.2f", @"Format string for ranging table cells.");  
  14. //    cell.detailTextLabel.text = [NSString stringWithFormat:formatString, beacon.major, beacon.minor, beacon.accuracy, beacon.rssi, [self calcDistByRSSI:beacon.rssi]];  
  15.       
  16.     NSString *formatString = NSLocalizedString(@"Acc: %.2fm, Rssi: %d, Dis: %.2fm", @"Format string for ranging table cells.");  
  17.     cell.detailTextLabel.text = [NSString stringWithFormat:formatString, beacon.accuracy, beacon.rssi, [self calcDistByRSSI:beacon.rssi]];  
  18.       
  19.     return cell;  
  20. }  

掃描結果

 

技術分享

展現了每台藍牙設備的Acc(精度)、Rssi(信號強度)和Dis(距離)。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM