小编lak*_*ntu的帖子

iOS蓝牙委托连接功能未被调用

我正在尝试收听所有蓝牙连接/断开事件.即使centralManagerDidUpdateState调用了委托的功能,当我连接或断开蓝牙设备时也没有任何反应.

我对于出了什么问题很困惑.我像这样初始化Central Manager/delegate:

var btDelegate: CBCentralManagerDelegate = BluetoothDelegate()
var btManager = CBCentralManager(delegate: btDelegate, queue: nil)
Run Code Online (Sandbox Code Playgroud)

BluetoothDelegate的定义如下:

import Foundation
import CoreBluetooth

class BluetoothDelegate : NSObject, CBCentralManagerDelegate {

    func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!) {
        println("connect") //this line is not called
    }
    func centralManager(central: CBCentralManager!, didDisconnectPeripheral peripheral: CBPeripheral!, error: NSError!) {
        println("disconnect") //this line is not called
    }

    func centralManagerDidUpdateState(central: CBCentralManager!) {
        println("state update") //this line is called
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:我可以不断收到更多状态更新事件,例如我切换蓝牙时,即使没有调用连接和断开连接.

bluetooth ios swift

8
推荐指数
1
解决办法
4859
查看次数

标签 统计

bluetooth ×1

ios ×1

swift ×1