私はBLE経由でモーションセンサー(IMUセンサー)にアクセスしようとしています。構造体は、1つのビューコントローラにセンサを接続/ペア設定し、その設定を変更し、別のビューコントローラ(その接続されていない)に出力データをアクセスして分析することです。View Controller間でBluetoothデータにアクセスするにはどうすればよいですか? in xcode
別のView Controllerで接続されているセンサーからデータにアクセスするにはどうすればよいですか?リアルタイムでプレゼンテーションを行い、生のデータを記録する必要がないため、Coredataは理想的ではありません。 segueを介してデータを渡すことはできません。セグが接続されているわけではなく(別のタブバービューコントローラを通じてアクセスされるため)、データを渡すこともできません。
CBCentralManagerなどをAppDelegateに配置することが可能であることを示すリンクが1つ見つかり、中央マネージャのプロパティ(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