值未发送到外围设备

时间:2018-04-25 21:27:19

标签: ios swift

当我点击应用程序上的按钮时,没有任何值发送到外围设备。我正在获得外围设备和特征=零。为什么会发生这种情况,我该怎么做才能改变这种情况?我在AppDelegate中拥有所有蓝牙连接编码。

import UIKit
import CoreBluetooth

class TemperatureViewController: UIViewController {
    var centralManager: CBCentralManager!
    var heartRatePeripheral: CBPeripheral!
    var characteristic: CBCharacteristic?

    func writeValue(value: Int8) {
        let peripheral = heartRatePeripheral  //, characteristic = self.characteristic
        let data = Data.dataWithValue(value: value)

        print("peripheral: \(String(describing: peripheral))")
        print("data: \(data)")
        print("characteristic: \(String(describing: characteristic))")
        peripheral?.writeValue(data, for: characteristic!, type: .withResponse)
        print("Working")

    }

    @IBAction func highButtonTapped(_ sender: UIButton) {
        writeValue(value: 2)
    }


    @IBAction func lowButtonTapped(_ sender: UIButton) {
        writeValue(value: 1)
    }


    @IBAction func offButtonTapped(_ sender: UIButton) {
        writeValue(value: 0)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }



}

extension Data {
    static func dataWithValue(value: Int8) -> Data {
        var variableValue = value
        return Data(buffer: UnsafeBufferPointer(start: &variableValue, count: 1))
    }

    func int8Value() -> Int8 {
        return Int8(bitPattern: self[0])
    }
}

这是我的AppDelegate,其中包含所有蓝牙连接代码。不确定这是否有任何不妥之处:

import UIKit
import CoreBluetooth
let heartRateServiceCBUUID = CBUUID(string: "0x180D")
let heartRateMeasurementCharacteristicCBUUID = CBUUID(string: "2A37")
let bodySensorLocationCharacteristicCBUUID = CBUUID(string: "2A38")
var heartRatePeripheral: CBPeripheral!
var centralManager: CBCentralManager!

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    var centralManager: CBCentralManager!
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        centralManager = CBCentralManager(delegate: self as! CBCentralManagerDelegate, queue: nil)

        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {

    }

    func applicationDidEnterBackground(_ application: UIApplication) {

    }

    func applicationWillEnterForeground(_ application: UIApplication) {

    }

    func applicationDidBecomeActive(_ application: UIApplication) {

    }

    func applicationWillTerminate(_ application: UIApplication) {

    }


}
extension AppDelegate: CBCentralManagerDelegate {
    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        switch central.state {
        case .unknown:
            print("central.state is .unknown")
        case .resetting:
            print("central.state is .resetting")
        case .unsupported:
            print("central.state is .unsupported")
        case .unauthorized:
            print("central.state is .unauthorized")
        case .poweredOff:
            print("central.state is .poweredOff")
        case .poweredOn:
            print("central.state is .poweredOn")
            centralManager.scanForPeripherals(withServices: [heartRateServiceCBUUID])
        }
    }


    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral,
                        advertisementData: [String: Any], rssi RSSI: NSNumber) {
        print(peripheral)
        heartRatePeripheral = peripheral
        heartRatePeripheral.delegate = self as CBPeripheralDelegate
        centralManager.stopScan()
        centralManager.connect(heartRatePeripheral)
    }

    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        print("Connected!")
        heartRatePeripheral.discoverServices([heartRateServiceCBUUID])
    }
}
extension AppDelegate: CBPeripheralDelegate {
    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
        let services = peripheral.services

        for service in services! {
            print(service)
            peripheral.discoverCharacteristics(nil, for: service)
        }

    }


    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService,
                    error: Error?) {
        let characteristics = service.characteristics

        for characteristic in characteristics! {
            print(characteristic)
            if characteristic.properties.contains(.read) {
                print("\(characteristic.uuid): properties contains .read")
                peripheral.readValue(for: characteristic)
            }
            if characteristic.properties.contains(.notify) {
                print("\(characteristic.uuid): properties contains .notify")
            }
        }
    }
    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic,
                    error: Error?) {
        switch characteristic.uuid {
        case bodySensorLocationCharacteristicCBUUID:
            print(characteristic.value ?? "no value")
        default:
            print("Unhandled Characteristic UUID: \(characteristic.uuid)")
        }
    }

}

0 个答案:

没有答案