我正在尝试制造一款可以通过iPhone上的应用程序控制的无人机。在无人机上,我有一个Adafruit羽毛32u4 BLE芯片与手机通信。我还没有找到任何方法从手机写入的特性中读取数据。该应用程序有四个前进,左,后和右按钮和一个控制油门的滑块。该应用程序的代码如下:
import CoreBluetooth
import UIKit
class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
var centralManager: CBCentralManager!
var peripheral: CBPeripheral!
var currentCharacteristic: CBCharacteristic! = nil
let txCharacteristic = CBUUID(string: "00001532-1212-EFDE-1523-785FEABCD123")
func centralManagerDidUpdateState(_ central: CBCentralManager) {
self.centralManager?.scanForPeripherals(withServices: nil, options: nil)
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
if (peripheral.name != nil && peripheral.name! == "Drone") {
self.peripheral = peripheral
self.centralManager.connect(self.peripheral, options: [CBConnectPeripheralOptionNotifyOnDisconnectionKey : true])
}
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
peripheral.delegate = self
peripheral.discoverServices(nil)
self.stopScan()
}
func stopScan() {
self.centralManager.stopScan()
}
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
if self.peripheral != nil {
self.peripheral.delegate = nil
self.peripheral = nil
}
self.startManager()
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
peripheral.discoverCharacteristics(nil, for: peripheral.services![0])
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
print(service)
for characteristic in service.characteristics! {
print(characteristic)
let thisCharacteristic = characteristic as CBCharacteristic
if thisCharacteristic.uuid == txCharacteristic {
self.currentCharacteristic = thisCharacteristic
let data = throttleValue.data(using: String.Encoding.utf8)!
self.peripheral.writeValue(data, for: self.currentCharacteristic, type: CBCharacteristicWriteType.withoutResponse)
self.label.text = "sent"
}
}
}
@IBOutlet weak var throttle: UISlider! {
didSet{
throttle.transform = CGAffineTransform(rotationAngle: CGFloat(-Double.pi / 2))
throttle.frame = CGRect(x: 100, y: 90, width: throttle.frame.size.width, height: throttle.frame.size.height)
}
}
@IBOutlet weak var throttleLabel: UILabel! {
didSet{
throttleLabel.frame = CGRect(x: 80, y: 40, width: throttleLabel.frame.size.width, height: throttleLabel.frame.size.height)
throttleLabel.font = throttleLabel.font.withSize(20)
}
}
@IBOutlet weak var upButton: UIButton! {
didSet{
upButton.frame = CGRect(x: 500, y: 50, width: upButton.frame.size.width, height: upButton.frame.size.height)
}
}
@IBOutlet weak var rightButton: UIButton! {
didSet{
rightButton.frame = CGRect(x: 600, y: 150, width: rightButton.frame.size.width, height: rightButton.frame.size.height)
}
}
@IBOutlet weak var downButton: UIButton! {
didSet{
downButton.frame = CGRect(x: 500, y: 250, width: downButton.frame.size.width, height: downButton.frame.size.height)
}
}
@IBOutlet weak var leftButton: UIButton! {
didSet{
leftButton.frame = CGRect(x: 400, y: 150, width: leftButton.frame.size.width, height: leftButton.frame.size.height)
}
}
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.startManager()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func startManager(){
centralManager = CBCentralManager(delegate: self, queue: nil)
}
var directNumber = 0
var throttleValue: String = "0"
@IBAction func goForward(_ sender: UIButton) {
directNumber = 1
}
@IBAction func stopForward1(_ sender: UIButton) {
directNumber = 0
}
@IBAction func stopForward2(_ sender: UIButton) {
directNumber = 0
}
@IBAction func goRight(_ sender: UIButton) {
directNumber = 2
}
@IBAction func stopRight1(_ sender: UIButton) {
directNumber = 0
}
@IBAction func stopRight2(_ sender: UIButton) {
directNumber = 0
}
@IBAction func goLeft(_ sender: UIButton) {
directNumber = 4
}
@IBAction func stopLeft1(_ sender: UIButton) {
directNumber = 0
}
@IBAction func stopLeft2(_ sender: UIButton) {
directNumber = 0
}
@IBAction func goBackward(_ sender: UIButton) {
directNumber = 3
}
@IBAction func stopBackward1(_ sender: UIButton) {
directNumber = 0
}
@IBAction func stopBackward2(_ sender: UIButton) {
directNumber = 0
}
@IBAction func changeThrottle(_ sender: UISlider) {
throttleValue = String(throttle.value)
}
}
代码未完成,因为我需要发送油门滑块数据以及按下哪个按钮。我相信我现在的代码是发送特性数据,因为当我运行应用程序时,arduino芯片上的蓝灯闪烁。刚连接时通常很稳定。总之,我有几个问题:
对这些问题的回答将对我有很大帮助。谢谢。