无法使用Swift广告和发现BLE

时间:2015-04-12 13:16:22

标签: ios swift bluetooth-lowenergy cbcentralmanager cbperipheral

我正在尝试使用以下

来宣传BLE
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()
    }
}

想要发现

的外围设备
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!) {
}

但是从不调用didDiscoverPeripheral方法。我正在测试两个IPhone5。 谁能告诉我我做错了什么?

0 个答案:

没有答案