iOS自定义键盘扩展蓝牙访问
我想从iOS自定义键盘扩展蓝牙访问,ios,swift,bluetooth,custom-keyboard,cbcentralmanager,Ios,Swift,Bluetooth,Custom Keyboard,Cbcentralmanager,我想从自定义键盘扩展插件中访问蓝牙设备。 但是,CBCentralManager将始终处于不受支持的状态。我在info.plist>NSExtension中启用了RequestsOpenAccess 还有其他人面临同样的问题吗 我的代码如下。它基于苹果默认的自定义键盘扩展名目标代码 class KeyboardViewController: UIInputViewController { @IBOutlet var nextKeyboardButton: UIButton! in
自定义键盘扩展插件中访问蓝牙设备。
但是,CBCentralManager
将始终处于不受支持的状态。我在info.plist>NSExtension
中启用了RequestsOpenAccess
还有其他人面临同样的问题吗
我的代码如下。它基于苹果默认的自定义键盘扩展名
目标代码
class KeyboardViewController: UIInputViewController {
@IBOutlet var nextKeyboardButton: UIButton!
internal var manager: BLManager?
override func viewDidLoad() {
super.viewDidLoad()
// Perform custom UI setup here
self.nextKeyboardButton = UIButton(type: .system)
self.nextKeyboardButton.setTitle(NSLocalizedString("Next Keyboard", comment: "Title for 'Next Keyboard' button"), for: [])
self.nextKeyboardButton.sizeToFit()
self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false
self.nextKeyboardButton.addTarget(self, action: #selector(handleInputModeList(from:with:)), for: .allTouchEvents)
self.view.addSubview(self.nextKeyboardButton)
self.nextKeyboardButton.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
self.nextKeyboardButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("> BL - Keyboard")
manager = BLManager.shared
}
override func viewWillLayoutSubviews() {
self.nextKeyboardButton.isHidden = !self.needsInputModeSwitchKey
super.viewWillLayoutSubviews()
}
}
具有相应的BLManager
对象
internal class BLManager: NSObject, CBCentralManagerDelegate {
public static var shared: BLManager = BLManager()
public var manager: CBCentralManager
override init() {
manager = CBCentralManager(delegate: nil, queue: nil)
super.init()
manager.delegate = self
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if central.state == .poweredOn {
manager.scanForPeripherals(withServices: nil, options: nil)
}
print("> BL \(central.state)")
}
}
你是在模拟器中测试的吗?不是,是在真正的设备上(iPhone12Pro/iPhone8)。在一个常规项目中使用相同的代码,一切都按照预期工作。我在使用模拟器时遇到了。不受支持的。