私は Swift に非常に慣れていないので、私の質問への答えは簡単に思えるかもしれません。BLE デバイスをスキャンし、それらを TableView に一覧表示し、この一覧で選択したデバイスに接続するためのインターフェイスを作成したいと考えています。これまでのところ、スキャンしてリストすることはできましたが、テーブルビューでクリックした目的のデバイスに接続するにはどうすればよいでしょうか。テーブルビューのDidSelectRowAtIndexPath関数に周辺オブジェクトを送信する必要があると思いますが、これを行う方法が本当にわかりません。それとも、これを行うためのよりスマートな方法がありますか? あなたの答えと時間をありがとう
これが私がこれまでに行ったことです:
import UIKit
import CoreBluetooth
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CBCentralManagerDelegate, CBPeripheralDelegate, UITextViewDelegate {
@IBOutlet var status: UITextView!
@IBOutlet
var manager:CBCentralManager!
var tableView: UITableView!
var items: [String] = [""]
override func viewDidLoad() {
super.viewDidLoad()
manager = CBCentralManager(delegate: self, queue: nil)
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
status!.delegate = self
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.items.count; }
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell!
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
status.text = "Connexion en cours";
self.manager.connectPeripheral(peripheral, options: nil) // HERE IS THE THING...
}
func centralManagerDidUpdateState(central: CBCentralManager) {
if central.state == CBCentralManagerState.PoweredOn {
central.scanForPeripheralsWithServices(nil, options: nil)
status.text = "Recherche d'appareil Bluetooth";
}
if central.state == CBCentralManagerState.PoweredOff { status.text = "Le Bluetooth est éteint"; }
if central.state == CBCentralManagerState.Unsupported { status.text = "Le Bluetooth n'est pas supporté"; }
if central.state == CBCentralManagerState.Unauthorized { status.text = "Le Bluetooth n'est pas autorisé"; }
}
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
items += ["\(peripheral.name)"]
self.tableView.reloadData()
self.manager.stopScan()
}
func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
status.text = "Connecté au périphérique";
//peripheral.discoverServices(nil)
}
}
ちなみに、CoreBluetoothがBLEを扱っていることはどこでも読んだ。では、BLEデバイスを接続しないために何を使用しますか??