BLE를 통해 모션 센서 (IMU 센서)에 액세스하려고합니다. 이 구조는 하나의보기 컨트롤러에서 센서를 연결/쌍으로 만들고 그 설정을 수정 한 다음 다른보기 컨트롤러에서 출력 데이터를 액세스하고 분석합니다 (연결되지 않음).보기 컨트롤러에서 블루투스 데이터에 액세스하는 방법은 무엇입니까? in xcode
다른보기 컨트롤러에 연결된 센서의 데이터에 계속 액세스하려면 어떻게해야합니까? Coredata는 실시간 프리젠 테이션이며 원시 데이터가 기록 할 필요가 없으므로 이상적이지 않습니다. segue를 통해 데이터를 전달할 수는 없습니다 (다른 탭 표시 줄 컨트롤러를 통해 액세스됩니다).
CBCentralManager 등을 AppDelegate에 넣고 중앙 관리자 속성 (How to continue BLE activities onto next view controller)이 될 수 있다고하는 링크가 하나 있습니다. 이것이 올바른 방법일까요? 그렇다면 중앙 관리자의 어떤 부분을 AppDelegate에 투입해야합니까?
여기 내 검색 및 블루투스 연결 구축을 포함한 코드입니다.
는var cManager = CBCentralManager()
var peripheralManager = CBPeripheralManager()
func centralManagerDidUpdateState(central: CBCentralManager!) {
var message: String = ""
switch cManager.state {
case .PoweredOff:
println("CoreBluetooth BLE hardware is powered off")
let alertView = UIAlertController(title: "", message: "Please enable Bluetooth to start the measurement", preferredStyle: .Alert)
alertView.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: nil))
presentViewController(alertView, animated: true, completion: nil)
break
case .PoweredOn:
println("CoreBluetooth BLE hardware is powered on and ready")
self.scanForWAX9(self)
break
case .Resetting:
println("CoreBluetooth BLE hardware is resetting")
break
case .Unauthorized:
println("CoreBluetooth BLE state is unauthorized")
break
case .Unknown:
println("CoreBluetooth BLE state is unknown")
break
case .Unsupported:
println("CoreBluetooth BLE hardware is unsupported on this platform")
break
default:
break
}
}
func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
println(peripheral.name);
//************************************************************************************
// Add some specification for bluetooth device
//************************************************************************************
if (peripheral.name != nil) && (peripheral.name.rangeOfString("WAX9") != nil) {
central.connectPeripheral(peripheral, options: nil)
self.connectedPeripheral = peripheral
println("PERIPHERAL NAME: \(peripheral.name)\n AdvertisementData: \(advertisementData)\n RSSI: \(RSSI)\n")
println("UUID DESCRIPTION: \(peripheral.identifier.UUIDString)\n")
println("IDENTIFIER: \(peripheral.identifier)\n")
cManager.stopScan()
}
}
@IBOutlet var connectNotice: UILabel!
func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!) {
peripheral.delegate = self
peripheral.discoverServices(nil)
// self.connectedPeripheral = peripheral
connectNotice.text = "\(peripheral.name) connected."
connectNotice.textColor = UIColor.whiteColor()
connectNotice.backgroundColor = UIColor(red:0.03, green:0.37, blue:0.5, alpha:0.5)
cManager.stopScan()
println("Scanning stopped")
println("Connected to peripheral")
}
// Alert message when fail to connect, e.g. when sensor goes out of range
func centralManager(central: CBCentralManager!, didFailToConnectPeripheral peripheral: CBPeripheral!, error: NSError!) {
println("FAILED TO CONNECT \(error)")
let alertView = UIAlertController(title: "", message: "Failed to connect.", preferredStyle: .Alert)
alertView.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: nil))
presentViewController(alertView, animated: true, completion: nil)
self.disconnect()
}
// Start to scan for sensor when disconnected
func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) {
println("Disconnected from peropheral")
let alertView = UIAlertController(title: "", message: "The connection is stopped.", preferredStyle: .Alert)
alertView.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: nil))
presentViewController(alertView, animated: true, completion: nil)
self.connectedPeripheral = nil
if scanAfterDisconnecting {
scanForWAX9(self)
}
}
func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager!) {
switch peripheralManager.state {
case .PoweredOff:
println("Peripheral - CoreBluetooth BLE hardware is powered off")
break
case .PoweredOn:
println("Peripheral - CoreBluetooth BLE hardware is powered on and ready")
break
case .Resetting:
println("Peripheral - CoreBluetooth BLE hardware is resetting")
break
case .Unauthorized:
println("Peripheral - CoreBluetooth BLE state is unauthorized")
break
case .Unknown:
println("Peripheral - CoreBluetooth BLE state is unknown")
break
case .Unsupported:
println("Peripheral - CoreBluetooth BLE hardware is unsupported on this platform")
break
default:
break
}
}
func peripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!) {
if (error != nil) {
println("ERROR: \(error)")
disconnect()
return
}
for service in peripheral.services
{
NSLog("Discovered service: \(service.UUID)")
peripheral.discoverCharacteristics(nil, forService: service as CBService)
}
}
아이디어에 관찰자을 설정 AppDelegate에서 액세스 할 수있는 속성으로 CBCentralManager는 AppDelegate가 sharedInstance (단독 패턴)라는 사실을 사용합니다. 하지만 AppDelegate에 너무 많은 관련이없는 작업을하고 있습니다. AppDelegate가 "AppDelegating"작업을 수행하게하십시오. BLE에 관련된 모든 것을 관리하는 싱글 톤 (Singleton + Swift)을보십시오. – Larme
정말 고마워요! – Mushroomcloud