我是iOS + BLE开发的新手。我正在尝试做一些非常基本的事情 - 将BLE设备连接到应用程序。这个应用程序有两个屏幕。
第二个屏幕是需要显示特征数据的位置 在“amt_consumed”标签上。
我创建了一个新的cocoa类,并使用prepareforsegue将数据传递到下一个屏幕。它有效但只显示一个值。 ble的值是动态的并且在第一个屏幕上相应地改变,但是在第二个屏幕上,amt_consumed显示了触发segue时的值。如何不断更新此值?
以下是代码:
ViewController.swift
import UIKit
import CoreBluetooth
import Foundation
//Define BLE UUID Variables
var bleServiceUUID: CBUUID!
var bleCharUUID: CBUUID!
class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
//Define BLE Manager/Peripheral Variables
var cManager : CBCentralManager!
var blePeripheral : CBPeripheral!
//Define BLE Outlets
@IBOutlet weak var ble_Status: UILabel!
@IBOutlet weak var amt_consumed: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//------->BLE<--------//
bleServiceUUID = CBUUID(string:"FFF0")
bleCharUUID = CBUUID(string:"FFF1")
cManager = CBCentralManager(delegate: self, queue: nil)
//------->BLE END<--------//
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//-------->BLE CODE START<-------//
//Check HW for BLE
func centralManagerDidUpdateState(central: CBCentralManager!) {
self.ble_Status.text = "Searching for BLE Devices"
switch (central.state) {
case .PoweredOff:
self.ble_Status.text = "BLE Off"
case .PoweredOn:
self.ble_Status.text = "BLE ON and searching..."
central.scanForPeripheralsWithServices(nil, options: nil)
default:
self.ble_Status.text = "Cannot find"
}
}
func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: (NSDictionary), RSSI: NSNumber!) {
if peripheral.name == "Nishant's Flow Bottle" {
println("Discovered: \(peripheral.name)")
ble_Status.text = "Discovered: \(peripheral.name)"
self.blePeripheral = peripheral
cManager.stopScan()
self.cManager.connectPeripheral(peripheral, options: nil)
}
else {
central.scanForPeripheralsWithServices(nil, options: nil)
}
}
// Check out the discovered peripherals to find Sensor Tag
func centralManager(central: CBCentralManager!,didConnectPeripheral peripheral: CBPeripheral!)
{
peripheral.delegate = self
peripheral.discoverServices([CBUUID(string: "0F37491A-AA8E-51B4-0252-F5F0857134D9")])
self.ble_Status.text = "Connected to \(peripheral.name)"
//---discover the specified service---
var services = [bleServiceUUID]
peripheral.discoverServices(services)
}
// If disconnected, start searching again
func centralManager(central: CBCentralManager!, didDisconnectPeripheral peripheral: CBPeripheral!, error: NSError!) {
self.ble_Status.text = "Disconnected"
central.scanForPeripheralsWithServices(nil, options: nil)
}
//---fired when services are discovered---
func peripheral(peripheral: CBPeripheral!,
didDiscoverServices error: NSError!) {
for service in peripheral.services {
println(
"P: \(peripheral.name) - Discovered service S:'\(service.UUID)'")
if service.UUID == bleServiceUUID {
//---discover the specified characteristic of the service---
var characteristics =
[bleCharUUID]
//---discover the characteristics of the service---
peripheral.discoverCharacteristics(
characteristics, forService: service as CBService)
}
}
}
func peripheral(peripheral: CBPeripheral!,
didDiscoverCharacteristicsForService service: CBService!,
error: NSError!) {
// 0x01 data byte to enable sensor
var enableValue = NSInteger(1)
for characteristic in service.characteristics {
//---look for the characteristic that allows you to subscribe to---
let thischaracteristic = characteristic as CBCharacteristic
if thischaracteristic.UUID == bleCharUUID {
//---subscribe to the characteristic---
self.blePeripheral.setNotifyValue(true, forCharacteristic: thischaracteristic)
}
blePeripheral.readValueForCharacteristic(thischaracteristic)
}
}
func peripheral(peripheral: CBPeripheral!, didUpdateValueForCharacteristic characteristic: CBCharacteristic!, error: NSError!) {
if var data :NSData = characteristic.value {
output("Data", data: characteristic.value)
}
}
func output(description: String, data: AnyObject){
println("\(description): \(data)")
amt_consumed.text = "\(data)"
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "btnSubmitSegue") {
var svc = segue.destinationViewController as NextScreen;
svc.dataPassed = CharReadData.text
}
}
}
Nextscreen.swift
import UIKit
class NextScreen: UIViewController {
@IBOutlet weak var amt_consumed: UILabel!
var dataPassed: String!
override func viewDidLoad() {
super.viewDidLoad()
amt_consumed.text = dataPassed
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}