2

I am trying to advertise BLE with the following

var perMan: CBPeripheralManager!
let myCustomServiceUUID: CBUUID = CBUUID(string: "109F17E4-EF68-43FC-957D-502BB0EFCF46")
var myService: CBMutableService!

override func viewDidLoad() {
    super.viewDidLoad()
    perMan = CBPeripheralManager(delegate: self, queue: nil)
    myService = CBMutableService(type: myCustomServiceUUID, primary: true)
    perMan.addService(myService)
}

// Broadcast when Bluetooth is on
func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager!) {
    if peripheral.state == CBPeripheralManagerState.PoweredOn {
        // Start advertising over BLE
        self.perMan.startAdvertising([CBAdvertisementDataServiceUUIDsKey: [myService.UUID]])
        println("Adertising")
    } else if peripheral.state == CBPeripheralManagerState.PoweredOff {
        self.perMan.stopAdvertising()
    }
}

And want to discover the peripheral with

import UIKit
import CoreBluetooth

class SearchViewController: UIViewController, CBCentralManagerDelegate {

var centralManager: CBCentralManager!

@IBOutlet weak var beaconsTableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    centralManager = CBCentralManager(delegate: self, queue: nil)

    // Listen to BLE of IPhones
    let services: NSArray = ["7521105F-8937-48B7-A875-66E6FE21D714"]
    centralManager.scanForPeripheralsWithServices(nil, options: nil)
}

// Found IPhone
func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
    println("Hallo:")
    println(RSSI)
}

// CBCentralManagerDelegate
func centralManagerDidUpdateState(central: CBCentralManager!) {
}

However the didDiscoverPeripheral method is never called. I am testing on two IPhone5. Can anyone tell me what I am doing wrong?

SparkierFlunky
  • 482
  • 4
  • 18
  • 1
    Use the app LightBlue (no affiliation) to listen for your BTLE broadcaster. If LightBlue finds your service, you know the issue is on the detection; if not, you know you've not set up the broadcasting completely/correctly. – Brad Brighton Apr 12 '15 at 16:16
  • 2
    You aren't waiting for the CBcentral to be in the powered on state before scanning - you need code similar to that in the peripheral to start scanning once the state is powered on – Paulw11 Apr 12 '15 at 20:01

0 Answers0