我需要获得连接设备的蓝牙信号强度,我才能获得它。我可以看到被包围的设备,我可以将RSSI值显示为UITableview
。
这里我需要根据我的设备位置改变RSSI值,我希望得到更新的RSSI值。我该如何实现呢?
我在下面发布我的代码
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
}
}
答案 0 :(得分:0)
要调用 readRSSI(),首先应连接外围设备