ios - 如何仅在 IOS Swift 中显示您的设备名称?蓝牙 (BLE)
问题描述
我试图仅通过扫描显示我的设备名称。我需要它根据打开的设备数量在最后显示设备(名称编号)。现在它显示了该范围内的所有设备,我只希望它显示我的设备名称编号,仅此而已。有人可以帮我解决这个问题吗?
这是一些代码:
这是我的第一堂扫描课。
/*function that gets called if we get a peripheral found notification*/
@objc func peripheralFound(notification: NSNotification){
tableView.reloadData()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return bluetoothConnection.peripherals.count
}
/*setting up a table cell which will display the name of the peripheral*/
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// return a cell with the peripheral name as text in the label
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
let label = cell.viewWithTag(1) as! UILabel
label.text = bluetoothConnection.peripherals[(indexPath as NSIndexPath).row].peripheral.name
cell.textLabel?.font = UIFont.systemFont(ofSize: 14)
return cell
}
/*if we select a given peripheral stop scanning for new ones. set selectedPeripheral to what we selected. try to connect to selected peripheral*/
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
// the user has selected a peripheral, so stop scanning and proceed to the next view
bluetoothConnection.manager.stopScan()
selectedPeripheral = bluetoothConnection.peripherals[(indexPath as NSIndexPath).row].peripheral
bluetoothConnection.manager.connect(selectedPeripheral!, options: nil)
connectionTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(checkIfConnectSuccessful), userInfo: nil, repeats: false)
}
这是我为不同的班级所做的。
//initialize core bluetooth
init(delegate: BluetoothClassDelegate){
super.init()
//change later
self.delegate = delegate
manager = CBCentralManager(delegate:self, queue: nil)
}
/// Start scanning for peripherals
func startScan() {
guard manager.state == .poweredOn else { return }
// start scanning for peripherals with correct service UUID
manager.scanForPeripherals(withServices: nil, options: [CBCentralManagerScanOptionAllowDuplicatesKey: false])
}
/* Called when BLE turns on or off*/
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if central.state == .poweredOn {
central.scanForPeripherals(withServices: nil, options: nil)
}
else{
print("Bluetooth not available")
}
}
/* Called any time a peripheral is discovered
discovered a peripheral with the defined uuid so add it to the list of peripherals available, reorganize by RSSI, dont allow for duplicates*/
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
// check whether it is a duplicate
for exisiting in peripherals {
if exisiting.peripheral.identifier == peripheral.identifier { return }
}
if peripheral.name == nil {return}
// add to the array, next sort & reload
let theRSSI = RSSI.floatValue
peripherals.append((peripheral: peripheral, RSSI: theRSSI))
peripherals.sort { $0.RSSI < $1.RSSI }
NotificationCenter.default.post(name: .foundPeripheral, object: nil)
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
peripheral.delegate = self
peripheral.discoverServices([myisscServiceUUID])
}
/* Called any time a service has been discovered for a given peripheral
we have discovered a peripheral with a matching service uuid*/
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
for service in peripheral.services! {
if service.uuid == myisscServiceUUID {
peripheral.discoverCharacteristics([myisscTxUUID, myisscRxUUID], for: service )
}
}
}
我已经尝试了很多东西,但它仍然向我展示了蓝牙范围内的所有设备。有什么我可以做的,所以它只是向我展示了一个独特的名字。提醒我还需要显示所有设备名称相同但最后数字不同的设备。如果有人可以提供帮助,那就太好了。谢谢。
解决方案
推荐阅读
- javascript - Semantic-ui 表格菜单未按预期工作
- c++ - c ++中的全局范围变量未正确初始化
- .net-core - .NET Core 3.1 GetService 返回 null
- python - 一个函数的 Python 生成线程
- python - 泡菜数据未加载
- c# - C# 类型安全的 JSON 行反序列化
- c# - SSL/TLS (HTTPS) 的 C# 目标主机名始终为空
- clojure - 为什么不能在 deftest 周围应用 with-redefs?
- typescript - 如何在 Typescript 中将具有给定类型的所有属性转换为另一个属性?
- cordova - Cordova 请求转发代理