Reputation: 1335
I need to get the Bluetooth signal strength of a connected device and I'm getting it. I can see the surrounded devices and I could display the RSSI value to a UITableview
.
Here I need to make the RSSI value changing according to my device position by doing this I hope I will get the updated RSSI values. How can I implement this?
I'm posting my code below
import UIKit
import CoreBluetooth
import Foundation
class Bluetooth_ViewController: BaseViewController,CBCentralManagerDelegate,CBPeripheralDelegate, UITableViewDelegate,UITableViewDataSource{
// @IBOutlet weak var bluetoothset: UILabel!
@IBOutlet weak var bluetoothlog: UITableView!
@IBOutlet var blucell: UITableViewCell!
@IBOutlet weak var devname: UILabel!
@IBOutlet weak var devstrength: UILabel!
//var timer = NSTimer.scheduledTimerWithTimeInterval(target: self, selector: Selector("result"), userInfo: nil, repeats: true)
private var centralManager: CBCentralManager?
var peri: [NSString] = []
var signalstrength: [NSString] = []
var rssvalue: NSNumber!
override func viewDidLoad()
{
super.viewDidLoad()
// var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self,selector:, userInfo: nil, repeats: true)
bluetoothlog.registerNib(UINib(nibName: "BlueCellTableViewCell", bundle: nil), forCellReuseIdentifier: "bluecell")
bluetoothlog.backgroundColor = UIColor.clearColor()
centralManager = CBCentralManager(delegate: self, queue: nil)
// Do any additional setup after loading the view.
var change = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector (result()), userInfo: nil, repeats: true)
}
//MARK:- Checking state of bluetooth
func centralManagerDidUpdateState(central: CBCentralManager!)
{
println("\(__LINE__) \(__FUNCTION__)")
println("checking state")
//var refresh = NSTimer.scheduledTimerWithTimeInterval( 1.0, target: self, Selector:"result" , repeats: true)
if central.state != .PoweredOn
{
return
}
central.scanForPeripheralsWithServices(nil,options:nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK:- discovering peripherals
func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!)
{
var localname: NSString = peripheral.name //advertisementData[CBAdvertisementDataLocalNameKey]! as NSString
println("Discovered: \(peripheral.name)")
var per : NSString = "\(peripheral.name)"
peri.append(per)
signalstrength.append(RSSI.stringValue)
rssvalue = peripheral.RSSI
println("RSSI!:\(rssvalue)")
println("RSI:\(peripheral.RSSI)")
// var rsstring: NSString = peripheral.RSSI.stringValue
//var rssvalue: NSNumber = peripheral.RSSI
// println("DiscRSSI:\(rssvalue)")
self.bluetoothlog.reloadData()
if localname != ""
{
//centralManager!.stopScan()
//self.peripheral = peripheral
peripheral.delegate = self
centralManager!.connectPeripheral(peripheral, options: nil)
}
}
//MARK:- tableview required methods
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return peri.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell :BlueCellTableViewCell = tableView.dequeueReusableCellWithIdentifier("bluecell") as BlueCellTableViewCell!
cell.lblName.text = peri[indexPath.row]
cell.lblSignal.text = signalstrength[indexPath.row]
bluetoothlog.backgroundColor = UIColor.clearColor()
return cell
}
}
Upvotes: 1
Views: 3429
Reputation: 1425
For calling readRSSI() you should connect the peripheral first
Upvotes: 2