gpt4 book ai didi

ios - 如何查找已连接蓝牙设备的蓝牙信号强度

转载 作者:行者123 更新时间:2023-11-28 09:13:41 37 4
gpt4 key购买 nike

我需要获取已连接设备的蓝牙信号强度,我正在获取。我可以看到周围的设备,并且可以将 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(),您应该先连接外设

关于ios - 如何查找已连接蓝牙设备的蓝牙信号强度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28450392/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com