Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在swift中侦听事件prom蓝牙外围设备_Swift_Bluetooth_Bluetooth Lowenergy_Core Bluetooth_Cbperipheral - Fatal编程技术网

在swift中侦听事件prom蓝牙外围设备

在swift中侦听事件prom蓝牙外围设备,swift,bluetooth,bluetooth-lowenergy,core-bluetooth,cbperipheral,Swift,Bluetooth,Bluetooth Lowenergy,Core Bluetooth,Cbperipheral,我有一个破折号按钮,可与蓝牙低能量工作。我可以扫描并找到它,连接到它并发现它的服务 现在我想听听它的按钮是否被按下,但似乎我的部件有一些严重的问题。我是Swift的新手,所以如果你能帮我解决这个问题,我真的很感激 这是我的swift代码: import CoreBluetooth import UIKit struct DisplayPeripheral{ var peripheral: CBPeripheral? var lastRSSI: NSNumber? var

我有一个破折号按钮,可与蓝牙低能量工作。我可以扫描并找到它,连接到它并发现它的服务

现在我想听听它的按钮是否被按下,但似乎我的部件有一些严重的问题。我是Swift的新手,所以如果你能帮我解决这个问题,我真的很感激

这是我的swift代码:

import CoreBluetooth
import UIKit

struct DisplayPeripheral{
    var peripheral: CBPeripheral?
    var lastRSSI: NSNumber?
    var isConnectable: Bool?
}

class PeripheralViewController: UIViewController {

    @IBOutlet weak var statusLabel: UILabel!
    @IBOutlet weak var bluetoothIcon: UIImageView!
    @IBOutlet weak var scanningButton: ScanButton!

    var centralManager: CBCentralManager?
    var peripherals: [DisplayPeripheral] = []
    var viewReloadTimer: Timer?
    let BEAN_NAME = "Security Tag"
    let BEAN_SCRATCH_UUID = CBUUID(string: "90946c81-e466-4a43-9974-949e465d35a1")
    let BEAN_SERVICE_UUID = CBUUID(string: "00001c00-d102-11e1-9b23-000efb0000a7")

    var selectedPeripheral: CBPeripheral?

    @IBOutlet weak var tableView: UITableView!

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        //Initialise CoreBluetooth Central Manager
        centralManager = CBCentralManager(delegate: self, queue: DispatchQueue.main)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        viewReloadTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(PeripheralViewController.refreshScanView), userInfo: nil, repeats: true)
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        viewReloadTimer?.invalidate()
    }

    func updateViewForScanning(){
        statusLabel.text = "Scanning BLE Devices..."
        bluetoothIcon.pulseAnimation()
        bluetoothIcon.isHidden = false
        scanningButton.buttonColorScheme(true)
    }

    func updateViewForStopScanning(){
        let plural = peripherals.count > 1 ? "s" : ""
        statusLabel.text = "\(peripherals.count) Device\(plural) Found"
        bluetoothIcon.layer.removeAllAnimations()
        bluetoothIcon.isHidden = true
        scanningButton.buttonColorScheme(false)
    }

    @IBAction func scanningButtonPressed(_ sender: AnyObject){
        if centralManager!.isScanning{
            centralManager?.stopScan()
            updateViewForStopScanning()
        }else{
            startScanning()
        }
    }

    func startScanning(){
        peripherals = []
        self.centralManager?.scanForPeripherals(withServices: nil, options: [CBCentralManagerScanOptionAllowDuplicatesKey: true])
        updateViewForScanning()
        let triggerTime = (Int64(NSEC_PER_SEC) * 10)
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(triggerTime) / Double(NSEC_PER_SEC), execute: { () -> Void in
            if self.centralManager!.isScanning{
                self.centralManager?.stopScan()
                self.updateViewForStopScanning()
            }
        })
    }

    func refreshScanView()
    {
        if peripherals.count > 1 && centralManager!.isScanning{
            tableView.reloadData()
        }
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destinationViewController = segue.destination as? PeripheralConnectedViewController{
            destinationViewController.peripheral = selectedPeripheral
        }
    }
}

extension PeripheralViewController: CBCentralManagerDelegate{
    func centralManagerDidUpdateState(_ central: CBCentralManager){
        //if (central.state == CBCentralManagerState.poweredOn){
            startScanning()
        //}else{
            // do something like alert the user that ble is not on
        //}
    }

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber){

        for (index, foundPeripheral) in peripherals.enumerated(){
            if foundPeripheral.peripheral?.identifier == peripheral.identifier{
                peripherals[index].lastRSSI = RSSI
                return
            }
        }

        let isConnectable = advertisementData["kCBAdvDataIsConnectable"] as! Bool
        if(peripheral.name == BEAN_NAME)
        {
            print(peripheral.name)
            print(peripheral.identifier)
            print("is?",isConnectable)
            let displayPeripheral = DisplayPeripheral(peripheral: peripheral, lastRSSI: RSSI, isConnectable: isConnectable)
            peripherals.append(displayPeripheral)
        }

        tableView.reloadData()
    }


}

extension PeripheralViewController: CBPeripheralDelegate {
    func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
        print("Error connecting peripheral: \(error?.localizedDescription)")
    }

    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral)
    {
        print("Peripheral connected")
        performSegue(withIdentifier: "PeripheralConnectedSegue", sender: self)
        peripheral.discoverServices(nil)

    }
    func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?)
    {
        print("jdbsud")

        for service in peripheral.services!
        {
            let thisService = service as CBService

            if service.uuid == BEAN_SERVICE_UUID {
                peripheral.discoverCharacteristics(nil,for: thisService)
            }
        }
    }


}

extension PeripheralViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

        let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell")! as! DeviceTableViewCell
        cell.displayPeripheral = peripherals[indexPath.row]
        cell.delegate = self
        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return peripherals.count
    }
}

extension PeripheralViewController: DeviceCellDelegate{
    func connectPressed(_ peripheral: CBPeripheral) {
        if peripheral.state != .connected {
            selectedPeripheral = peripheral
            peripheral.delegate = self
            centralManager?.connect(peripheral, options: nil)
            //you can listen to the commands here


        }
    }

    }

我在func外围设备(外围设备:CBPeripal,didDiscoverServices错误:NSError?)中打印了一些内容,以检查我是否在那里输入,显然我没有输入代码的那部分。

在努力解决这个问题后,我自己能够解决它

找到服务后,我应该将这两个函数添加到代码中,它就像一个符咒:

  func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
    debugPrint("Enabling ...")

    for characteristic in service.characteristics! {
        let thisCharacteristic = characteristic as CBCharacteristic

        debugPrint("Characteristic: ", thisCharacteristic.uuid)

        if thisCharacteristic.uuid == BEAN_SCRATCH_UUID {
            debugPrint("Set to notify: ", thisCharacteristic.uuid)

            // Prepare to show data

            self.peripheral.setNotifyValue(true, for: thisCharacteristic)
       }
    }
}

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
    if characteristic.uuid == BEAN_SCRATCH_UUID {
        let content = String(data: characteristic.value!, encoding: String.Encoding.utf8)

        debugPrint("Notified.")
    }
}