iOS Swift - CoreBluetooth 无法发送数据

Posted

技术标签:

【中文标题】iOS Swift - CoreBluetooth 无法发送数据【英文标题】:iOS Swift - CoreBluetooth Unable to send out data 【发布时间】:2018-10-19 03:10:51 【问题描述】:

我正在尝试使用已实现的滑块发送我的数据,但是我无法这样做。然而,我要接收数据。

我正在使用两个函数来写入/输出数据“writeData”和“writeValue”。

这是完整的项目 https://github.com/brandonmaiwin/Raspberrypi-bluetooth

 //
 //  speed2ViewController.swift
 //  Raspberrypi
 //
 //  Created by Brandon Mai Nguyen on 10/14/18.
 //  Copyright © 2018 Brandon Mai Nguyen. All rights reserved.
 //

 import UIKit
 import CoreBluetooth

 class speedtwoViewController: UIViewController, CBPeripheralManagerDelegate, UITextViewDelegate, UITextFieldDelegate

//Initialize values
@IBOutlet weak var baseTextView: UITextView!
@IBOutlet weak var Speedometerdisplay: UILabel!
@IBOutlet weak var sliderOutlet: UISlider!
//Data in and Data out
@objc var peripheralManager: CBPeripheralManager?
@objc var peripheral: CBPeripheral!
private var consoleAsciiText:NSAttributedString? = NSAttributedString(string: "")

override func viewDidLoad()

    super.viewDidLoad()
    //Create and start the peripheral manager
    peripheralManager = CBPeripheralManager(delegate: self, queue: nil)
    //-Notification for updating the text view with incoming text
    updateIncomingData()


//this function recieves data from the arudino and displays it.
@objc func updateIncomingData ()

    NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "Notify"), object: nil , queue: nil)
    
        notification in
        let appendString = "\n"
        let myFont = UIFont(name: "Helvetica Neue", size: 15.0)
        let myAttributes2 = [NSAttributedStringKey.font: myFont!, NSAttributedStringKey.foregroundColor: UIColor.red]
        let attribString = NSAttributedString(string: "[Incoming]: " + (characteristicASCIIValue as String) + appendString, attributes: myAttributes2)
        let newAsciiText = NSMutableAttributedString(attributedString: self.consoleAsciiText!)
        self.baseTextView.attributedText = NSAttributedString(string: characteristicASCIIValue as String , attributes: myAttributes2)

        newAsciiText.append(attribString)

        self.consoleAsciiText = newAsciiText
        self.baseTextView.attributedText = self.consoleAsciiText
    


//this function sends data out
func outgoingData (data: String)

    let appendString = "\n"

    let inputText = String(data)

    let myFont = UIFont(name: "Helvetica Neue", size: 15.0)
    let myAttributes1 = [NSAttributedStringKey.font: myFont!, NSAttributedStringKey.foregroundColor: UIColor.blue]

    writeValue(data: inputText)

    let attribString = NSAttributedString(string: "[Outgoing]: " + inputText + appendString, attributes: myAttributes1)
    let newAsciiText = NSMutableAttributedString(attributedString: self.consoleAsciiText!)
    newAsciiText.append(attribString)

    consoleAsciiText = newAsciiText
    baseTextView.attributedText = consoleAsciiText



// Write functions
func writeValue(data: String)

    let data = data
    let valueString = (data as NSString).data(using: String.Encoding.utf8.rawValue)
    //change the "data" to valueString
    if let blePeripheral = blePeripheral
    
        if let txCharacteristic = txCharacteristic
        
            blePeripheral.writeValue(valueString!, for: txCharacteristic, type: CBCharacteristicWriteType.withResponse)
        
    


//slider changes the speed and sends a integer value
@IBAction func Speed(_ sender: UISlider)


    Speedometerdisplay.text = String(Int(sliderOutlet.value))

    outgoingData(data: String(Int(sliderOutlet.value)))


    writeCharacteristic(val: Int8(Int(sliderOutlet.value)))


//send the slider value out using "writeCharacteristic"
func writeCharacteristic(val: Int8)

    var val = val
    let ns = NSData(bytes: &val, length: MemoryLayout<Int8>.size)
    blePeripheral?.writeValue(ns as Data, for: txCharacteristic!, type: CBCharacteristicWriteType.withResponse)




//prints out an error if not detected
func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?)

    if let error = error
    
        print("\(error)")
        return
    


//Prints a message stating that the device is on and connected
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager)

    if peripheral.state == .poweredOn
    
        return
    
    print("Peripheral manager is running")


//Check when someone subscribe to our characteristic, start sending the data
func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didSubscribeTo characteristic: CBCharacteristic)

    print("Device subscribe to characteristic")



【问题讨论】:

【参考方案1】:

由于 UISlider 的 UIControlEventValueChanged 非常敏感,它可能会为蓝牙带来开销。你可以试试这个:

slider.isContinuous = false

只有在编辑结束后才会发送新值。如果您想要一些连续且灵敏度较低的东西,您可以在@IBAction func Speed(_ sender: UISlider) 中处理它。

【讨论】:

以上是关于iOS Swift - CoreBluetooth 无法发送数据的主要内容,如果未能解决你的问题,请参考以下文章

CoreBluetooth:scanForPeripheralsWithServices 不使用服务数组

从字节中获取字符串(Corebluetooth,Swift)

而委托为 nil 或未实现外围设备:didDiscoverCharacteristicsForService:error: (Corebluetooth, Swift)

监听蓝牙外设按钮事件 iOS Swift

CoreBluetooth [警告] 未知错误:311 在 iOS 中使用 CoreBluetooth 框架重复连接和断开连接时发生

Swift语言iOS8的蓝牙Bluetooth解析(转帖)