Ios 未发送到外围设备的值
当我点击应用程序上的按钮时,没有值被发送到外围设备。我变得边缘化,特征=零。为什么会发生这种情况?我能做些什么来改变这种情况?我在AppDelegate中有所有蓝牙连接编码Ios 未发送到外围设备的值,ios,swift,Ios,Swift,当我点击应用程序上的按钮时,没有值被发送到外围设备。我变得边缘化,特征=零。为什么会发生这种情况?我能做些什么来改变这种情况?我在AppDelegate中有所有蓝牙连接编码 import UIKit import CoreBluetooth class TemperatureViewController: UIViewController { var centralManager: CBCentralManager! var heartRatePeripheral: CBPer
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)")
}
}
}
您从未在视图控制器实例上设置属性。我应该如何设置?抱歉,我对Swift和iOS不太熟悉。由于您的应用程序代理是单例的,因此从应用程序代理访问属性可能比在视图控制器中创建属性更简单。您可以通过
让appDelegate=UIApplication.shared.delegate作为获取对应用程序委托的引用!AppDelegate
我是否在AppDelegate中设置了属性?并且该引用会进入ViewController,对吗?您的应用程序委托中已经有一些属性,但没有特性
。属性不会在对象实例之间神奇地共享值,因为它们具有相同的名称。这不是斯威夫特独有的。所有面向对象的语言都是相同的。