2

I'm trying to see the peripherals via bluetooth using my iphone

in fact i write this code till now

class BlueT: ViewController, CBCentralManagerDelegate, CBPeripheralDelegate{

var centralManager: CBCentralManager!
var peripheral: CBPeripheral!

let BEAN_NAME = "Az200"
let BEAN_SCRATCH_UUID =
CBUUID(string: "A159EA45-A794-27F4-8947-1C0A5E3C70B0")
let BEAN_SERVICE_UUID =
    CBUUID(string: "A159EA45-A794-27F4-8947-1C0A5E3C70B0")

 override func viewDidLoad() {
    super.viewDidLoad()
    centralManager = CBCentralManager(delegate: self, queue: nil)

}


func centralManagerDidUpdateState(_ central: CBCentralManager) {

    if #available(iOS 10.0, *){
        switch (central.state) {
        case CBManagerState.poweredOff:
            print("CBCentralManagerState.PoweredOff")
        case CBManagerState.unauthorized:
            print("CBCentralManagerState.Unauthorized")
            break
        case CBManagerState.unknown:
            print("CBCentralManagerState.Unknown")
            break
        case CBManagerState.poweredOn:
            print("CBCentralManagerState.PoweredOn")
            centralManager.scanForPeripherals(withServices: nil, options: nil)
        case CBManagerState.resetting:
            print("CBCentralManagerState.Resetting")
        case CBManagerState.unsupported:
            print("CBCentralManagerState.Unsupported")
            break
        }}else{
        switch central.state.rawValue{
        case 0:
            print("CBCentralManagerState.Unknown")
            break
        case 1:
            print("CBCentralManagerState.Resetting")
        case 2:
            print("CBCentralManagerState.Unsupported")
            break
        case 3:
            print("This app is not authorised to use Bluetooth low energy")
            break
        case 4:
            print("Bluetooth is currently powered off.")
        case 5:
            print("Bluetooth is currently powered on and available to use.")
            centralManager.scanForPeripherals(withServices: nil, options: nil)

        default:break
        }}}


func centralManager(
    central: CBCentralManager,
    didDiscoverPeripheral peripheral: CBPeripheral,
    advertisementData: [String : AnyObject],
    RSSI: NSNumber) {
    let device = (advertisementData as NSDictionary)
        .object(forKey: CBAdvertisementDataLocalNameKey)
        as? NSString

    if device?.contains(BEAN_NAME) == true {
        self.centralManager.stopScan()

        self.peripheral = peripheral
        self.peripheral.delegate = self
        print("peripheral: \(peripheral)")
               } }
}

so in fact, the output is "Bluetooth is currently powered on and available to use." but the print("peripheral : (peripheral)") doesn't appear so what could i do to see my peripherals ?

nbloqs
  • 3,152
  • 1
  • 28
  • 49
Anthony Shahine
  • 2,477
  • 3
  • 18
  • 24
  • I've full code for connecting BLE to iphone in objective c. – Mohsin Sabasara Oct 17 '16 at 11:18
  • Take a look to this question: http://stackoverflow.com/questions/35794107/swift-ble-communications/39174444#39174444 – nbloqs Oct 17 '16 at 11:21
  • Is the peripheral advertising? Can the LightBlue app see it? – Paulw11 Oct 17 '16 at 11:29
  • yes, and in fact, setting -> bluetooth, i saw that my device is already connected @Paulw11 – Anthony Shahine Oct 17 '16 at 11:33
  • how could i check if this function work ? " self.centralManager.scanForPeripherals(withServices: nil, options: nil) " @Paulw11 and why do you think my code can't reach the function didDiscoverPeripheral ? – Anthony Shahine Oct 17 '16 at 11:59
  • How do you know that delegate function isn't being called? Have you set a breakpoint? It seems more likely that your `if` isn't matching. – Paulw11 Oct 17 '16 at 12:23
  • i set a breakpoint in the let device = (advertis... but nothing happen, the code doesn't reach the function didDiscoverPeripheral, and i don't really understand why @Paulw11 – Anthony Shahine Oct 17 '16 at 12:29
  • i just find that the function in swift 3 should be : func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { //code }, thank you anyway @Paulw11 – Anthony Shahine Oct 17 '16 at 13:53

0 Answers0