0
votes

if (central.state == .poweredOn) kontrolune girmiyor , bluetooth listesi gelmiyor ne yapabilirim ?

import CoreBluetooth import UIKit

class ViewController: UIViewController { var centralManager: CBCentralManager? var peripherals = Array()

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

     centralManager = CBCentralManager(delegate: self, queue: DispatchQueue.main)
}

}

extension ViewController: CBCentralManagerDelegate { func centralManagerDidUpdateState(_ central: CBCentralManager) {

    print(central.state)
    if (central.state == .poweredOn){
        self.centralManager?.scanForPeripherals(withServices: nil, options: nil)


    }
    else {
        print("nil")
    }


}

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
    peripherals.append(peripheral)
     print(peripheral)

}

}

2
"But the idea that we should be forced to accommodate random human languages in our community is completely unsustainable." - stackoverflow.blog/2009/07/23/non-english-question-policy Make an effort and ask in English, it's the very least you can do.backslash-f

2 Answers

1
votes

swift 4

import CoreBluetooth

var bleCentralManager: CBCentralManager!
var arrayPeripheralList = [CBPeripheral]()

viewDidLoad(){

   self.bleCentralManager = CBCentralManager(delegate: self, queue: nil)
}

func centralManagerDidUpdateState(_ central: CBCentralManager) {

    switch central.state{

        case .poweredOn:  

           central.scanForPeripherals(withServices: nil, options: nil)

        default:  

           bleCentralManager.stopScan()
    }
}

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {

     if !(self.arrayPeripheralList.contains(peripheral)){

        self.arrayPeripheralList.append(peripheral)
    }
}
0
votes

You have to call scanForPeripheralsWithServices from outside the delegate method firstly

override func viewDidLoad() {
    super.viewDidLoad()

     manager = CBCentralManager(delegate: self, queue: GlobalBackgroundQueue, options: [CBCentralManagerOptionShowPowerAlertKey: true])


     if self.manager!.state == CBCentralManagerState.PoweredOn {
        self.manager?.scanForPeripheralsWithServices(nil, options: nil)

            delay(2, closure: {
                            self.manager?.stopScan()
                            print("scanning stopped.")



                           })
    }
}

func centralManagerDidUpdateState(central: CBCentralManager) {
        if (central.state != CBCentralManagerState.PoweredOn) {
                self.bluetoothStatus = false
        }
    }



 func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
                peripherals.append(["p": peripheral, "a": advertisementData, "r": RSSI])
    }