didUpdateValueForCharacteristic不在BLE系统中调用,swift 3

时间:2017-06-17 08:57:05

标签: ios swift3 bluetooth-lowenergy

我不明白为什么didUpdateValueForCharacteristic在我用BLE写作时不会打电话。

我的班级是

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

同样的身体帮助我...... 我在互联网上搜索了一个如何与ios和BLE建立通信的例子,但我找不到。

0 个答案:

没有答案