ios didUpdateValueForCharacteristic未在BLE系统中调用,swift 3

u1ehiz5o  于 2022-12-15  发布在  iOS
关注(0)|答案(1)|浏览(480)

我不明白为什么在BLE中写入时didUpdateValueForCharacteristic不调用。
我的班级是

class MachineShopping: UIViewController, BluetoothDelegate, CBCentralManagerDelegate {

    fileprivate let bluetoothManager = BluetoothManager.getInstance()
    fileprivate var showAdvertisementData = false
    fileprivate var services : [CBService]?
    fileprivate var characteristicsDic = [CBUUID : [CBCharacteristic]]()

    var lastAdvertisementData : Dictionary<String, AnyObject>?
    fileprivate var advertisementDataKeys : [String]?

    var dataTableView: UITableView!
    var tableViewHeight: NSLayoutConstraint!
    var characteristic : CBCharacteristic?
    @IBOutlet weak var payBtn: UIButton!

    // var myCentralManager = CBCentralManager(delegate: self as! CBCentralManagerDelegate, queue: nil)

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        bluetoothManager.delegate = self
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.navigationItem.title = "Buy"
        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "backgroundapp.png")!)
        let button = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(MachineShopping.backbuttonTapped))
        button.tintColor = UIColor.white
        self.navigationItem.leftBarButtonItem = button

        payBtn.layer.cornerRadius = 10
        payBtn!.titleLabel!.font = UIFont.boldSystemFont(ofSize: 16.0)
        initAll()
    }

    // MARK: custom functions
    func initAll() {

        advertisementDataKeys = ([String](lastAdvertisementData!.keys)).sorted()
        bluetoothManager.discoverCharacteristics()
        services = bluetoothManager.connectedPeripheral?.services

    }

    func centralManagerDidUpdateState(_ central: CBCentralManager!) {
        print("I'm HERE 1")
    }

    public func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
        print("I'm HERE 2")

        let rxData = characteristic.value
        if let rxData = rxData {
            let numberOfBytes = rxData.count
            var rxByteArray = [UInt8](repeating: 0, count: numberOfBytes)
            (rxData as NSData).getBytes(&rxByteArray, length: numberOfBytes)
            print(rxByteArray)
        }
    }


    func didDiscoverCharacteritics(_ service: CBService) {

        //Active notifications
        characteristicsDic[service.uuid] = service.characteristics
        characteristic = service.characteristics![1]
        bluetoothManager.setNotification(enable: true, forCharacteristic: characteristic!)
    }

    @IBAction func payButtonTapped(_ sender: Any) {

        let textContent: String = "7E"

        var hexString = textContent.substring(from: textContent.characters.index(textContent.startIndex, offsetBy: 2))
        if hexString.characters.count % 2 != 0 {
            hexString = "0" + hexString
        }

        let data = hexString.dataFromHexadecimalString()
        bluetoothManager.writeValue(data: data!, forCharacteristic: characteristic!, type: CBCharacteristicWriteType.withResponse)

    }

    func backbuttonTapped(){

        let mapViewControllerObejct = self.storyboard?.instantiateViewController(withIdentifier: "ListIdentifier") as? List
        self.navigationController?.pushViewController(mapViewControllerObejct!, animated: true)
    }
}

Same Body Help Me ...我在互联网上搜索了如何与iOS和BLE建立通信的示例,但没有找到。

5lhxktic

5lhxktic1#

1.检查此特性是否支持通知-characteristic.isNotifying
1.尝试将写入类型更改为withoutResponse。bluetoothManager.writeValue(data: data!, forCharacteristic: characteristic!, type: CBCharacteristicWriteType.withoutResponse)

相关问题