如何仅在IOS Swift中显示设备名称?蓝牙(BLE)

时间:2019-03-14 20:23:11

标签: ios swift bluetooth ios-bluetooth

我正在尝试仅通过扫描显示我的设备名称。我需要它在最后显示设备(名称编号),具体取决于打开了多少个设备。现在,它显示了该范围内的所有设备,我只希望它显示我的设备名称-编号,而没有其他任何显示。有人可以帮我解决这个问题吗?

以下是一些代码:

这是我要扫描的第一堂课。

 /*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 )
        }
    }
}

我尝试了很多事情,但仍然可以看到蓝牙范围内的所有设备。有什么我可以做的,所以它只显示一个唯一的名称。提醒我,还需要显示所有具有相同设备名称但最后只是不同数字的设备。如果有人可以帮助那将是很好。谢谢。

0 个答案:

没有答案