我正在编写一个应用程序来连接具有可以写入的身份验证特性之一的外围设备。连接到特性的密码是123456.我使用LightBlue Explorer进行测试,它成功提示警报输入密码
但是,当我尝试使用我的应用程序传递值时,它不会弹出,我也看不到任何错误弹出。你们能告诉我应该采用哪种方法来实现这一目标。
这是我的代码
```
import UIKit
import CoreBluetooth
class MainViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
let xxxxxxx_SERVICE = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
let TX_CHARACTERISTIC = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
var manager : CBCentralManager? = nil
var mainPeripheral : CBPeripheral? = nil
var writeCharacteristic : CBCharacteristic? = nil
@IBOutlet weak var upButton: UIButton!
@IBOutlet weak var downButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
//addNavBarImage()
manager = CBCentralManager(delegate: self, queue: nil)
customiseNavigationBar()
// Continuous Hold UP Button
let upButtonlongGesture = UILongPressGestureRecognizer(target: self, action: #selector(MainViewController.upButtonlongTap))
upButtonlongGesture.minimumPressDuration = 0
upButton.addGestureRecognizer(upButtonlongGesture)
// Continuous Hold DOWN Button
let downButtonlongGesture = UILongPressGestureRecognizer(target: self, action: #selector(MainViewController.downButtonlongTap))
downButtonlongGesture.minimumPressDuration = 0
downButton.addGestureRecognizer(downButtonlongGesture)
}
// MARK: - Update Navigation Bar Button
func addNavBarImage() {
let navController = navigationController!
let image = #imageLiteral(resourceName: "xxxxxxx_logo")
let imageView = UIImageView(image: image)
let bannerWidth = navController.navigationBar.frame.size.width
let bannerHeight = navController.navigationBar.frame.size.height
let bannerX = bannerWidth / 2 - image.size.width / 2
let bannerY = bannerHeight / 2 - image.size.height / 2
print(bannerX)
print(bannerY)
imageView.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth, height: bannerHeight)
imageView.contentMode = .scaleAspectFit
navigationItem.titleView = imageView
}
func customiseNavigationBar() {
navigationController?.navigationBar.backgroundColor = .white
navigationController?.navigationBar.isTranslucent = false
self.navigationItem.rightBarButtonItem = nil
let rightButton = UIButton()
if (mainPeripheral == nil) {
rightButton.setTitle("Scan", for: [])
rightButton.setTitleColor(UIColor.orange, for: [])
rightButton.setTitleShadowColor(UIColor.black, for: [])
rightButton.frame = CGRect(origin: CGPoint(x : 0, y: 0), size: CGSize(width : 60, height: 30))
rightButton.addTarget(self, action: #selector(self.scanButtonPressed), for: .touchUpInside)
} else {
rightButton.setTitle("Disconnect", for: [])
rightButton.setTitleColor(UIColor.red, for: [])
rightButton.setTitleShadowColor(UIColor.black, for: [])
rightButton.frame = CGRect(origin: CGPoint(x : 0, y: 0), size: CGSize(width : 100, height: 30))
rightButton.addTarget(self, action: #selector(self.disconnectButtonPressed), for: .touchUpInside)
}
let rightBarButton = UIBarButtonItem()
rightBarButton.customView = rightButton
self.navigationItem.rightBarButtonItem = rightBarButton
}
@objc func scanButtonPressed() {
print("Scan Button Pressed")
performSegue(withIdentifier: "scan-segue", sender: nil)
}
@objc func disconnectButtonPressed() {
print("Disconnect Button Pressed")
manager?.cancelPeripheralConnection(mainPeripheral!)
//uncomment after implemented ScanTableViewController
//manager?.cancelPeripheralConnection(mainPeripheral)
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "scan-segue" {
let scanController : ScanTableViewController = segue.destination as! ScanTableViewController
manager?.delegate = scanController
scanController.manager = manager
scanController.parentView = self
}
}
// MARK - CBCentralManagerDelegate Methods
func centralManagerDidUpdateState(_ central: CBCentralManager) {
print("BLE Stack Started")
switch central.state {
case .poweredOff:
print("Power is OFF")
break
case .resetting:
print("Resetting")
break
case .poweredOn:
print("Power is ON")
break
case .unauthorized:
print("Unauthorized")
break
case .unsupported:
print("Unsupported")
break
default:
print("Unknown")
break
}
}
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
mainPeripheral = nil
customiseNavigationBar()
// Show Pop Up Alert
let alert = UIAlertController(title: "Notification", message: "Disconnected \(peripheral.name ?? "") Successfully!", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Dismiss", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
print("Disconnected " + peripheral.name!)
}
// MARK - CBPeripheralDelegate Methods
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
for service in peripheral.services! {
print("Services found with UUID: " + service.uuid.uuidString)
if (service.uuid.uuidString == "180A") {
peripheral.discoverCharacteristics(nil, for: service)
}
if (service.uuid.uuidString == "1800") {
peripheral.discoverCharacteristics(nil, for: service)
}
if ((service.uuid.uuidString == xxxxxx_SERVICE)) {
peripheral.discoverCharacteristics(nil, for: service)
}
}
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
for characteristic in service.characteristics! {
let aCharacteristic = characteristic as CBCharacteristic
print(aCharacteristic)
if (aCharacteristic.uuid.uuidString == TX_CHARACTERISTIC) {
writeCharacteristic = aCharacteristic
}
}
}
// MARK: - Button Pressed Characteristic
// Write Data
func writeBLEData(byte: [UInt8]) {
let writeData = Data(bytes: byte)
if mainPeripheral != nil {
mainPeripheral?.writeValue(writeData, for: writeCharacteristic!, type: CBCharacteristicWriteType.withoutResponse)
} else {
print("Haven't discovered any device yet")
}
}
// Write Value to Controller
@objc func upButtonlongTap(sender : UIGestureRecognizer){
if sender.state == .ended {
//Do Whatever You want on End of Gesture
writeBLEData(byte: [0x00])
}
else if sender.state == .began {
//Do Whatever You want on Began of Gesture
writeBLEData(byte: [0x01])
}
}
@objc func downButtonlongTap(sender : UIGestureRecognizer){
if sender.state == .ended {
//Do Whatever You want on End of Gesture
writeBLEData(byte: [0x00])
}
else if sender.state == .began {
//Do Whatever You want on Began of Gesture
writeBLEData(byte: [0x02])
}
}
先谢谢你们!