0

私は 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デバイスを接続しないために何を使用しますか??

4

1 に答える 1

1

name ではなく配列に CBPeripheral アイテムを追加し、そのオブジェクトを connect に渡します。次のようなものはあなたのために修正されるかもしれません。最初に didDiscoverPeripheral のこの行を変更します

items += ["\(peripheral.name)"]

items += [peripheral]

次に、 cellForRowAtIndexPath でこの行を変更します

cell.textLabel?.text = self.items[indexPath.row]

cell.textLabel?.text = self.items[indexPath.row].name

その後、あなたの didSelectRowAtIndexPath

self.manager.connectPeripheral(peripheral, options: nil)

self.manager.connectPeripheral(self.items[indexPath.row], options: nil)
于 2015-10-07T13:55:42.460 に答える