如何查找已连接蓝牙设备的蓝牙信号强度
How to find Bluetooth signal strength of a connected bluetooth device
我需要获取已连接设备的蓝牙信号强度,我正在获取。我可以看到周围的设备,并且可以将 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
}
}
调用readRSSI()你应该先连接外设
我需要获取已连接设备的蓝牙信号强度,我正在获取。我可以看到周围的设备,并且可以将 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
}
}
调用readRSSI()你应该先连接外设