周辺BLEデバイスをスキャンするアプリケーションを作成しようとしています。私はLightBlueとRedBear iPhoneアプリケーションの両方で見ることができるBLE Miniを使用しています。 BLEの電源が入っているときにスキャンが始まることを確認しましたが、プログラムを実行してもBLEデバイスは検出されません。私はiOSでCorebluetoothを実装するための多くの例を見てきましたが、必要な機能をすべて持っているようです。どこが間違っていますか?お手伝いありがとう。iOS Bluetoothがデバイスを検出しない
// BlueToothVC.swift
import UIKit
import CoreBluetooth
class BlueToothVC: UIViewController, UITableViewDataSource, UITableViewDelegate, CBCentralManagerDelegate
{
@IBOutlet weak var tableView: UITableView!
var centralManager: CBCentralManager!
var peripherals: Array<CBPeripheral> = Array<CBPeripheral>()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
centralManager = CBCentralManager(delegate: self, queue: DispatchQueue.main)
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//CoreBluetooth methods
func centralManagerDidUpdateState(_ central: CBCentralManager)
{
if (central.state == CBManagerState.poweredOn)
{
print("scanning")
self.centralManager?.scanForPeripherals(withServices: nil, options: nil)
}
else
{
// do something like alert the user that ble is not on
}
}
private func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber)
{
peripherals.append(peripheral)
tableView.reloadData()
print("saw something")
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return peripherals.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "BTCell")! as! BTCell
let peripheral = peripherals[indexPath.row]
cell.label.text = peripheral.name
return cell
}
@IBAction func touchCancel(_ sender: AnyObject) {
self.navigationController?.popViewController(animated: true)
}
}
はどうもありがとうございます前に、関数のシグネチャで
_
を必要とします!それが私の問題を解決しました。 – KaplanAlex