我正在使用CoreBluetooth编写一个应用程序,该应用程序使用一个属性订阅一个特征:'通知'这将允许Raspberry Pi 3发送一个整数位以显示在应用程序上。我使用Pybleno创建了具有此特性的服务,Pybleno是Bleno的Python直接端口。我能够连接到服务并读取其特性,但仅持续30秒 - 之后将触发didDisconnect方法。在我的中心,我使用time.sleep()来改变数据发送的频率。我注意到没有延迟导致外围设备在大约10秒后断开,而20-30ms延迟导致30秒后断开连接。任何帮助都会很棒!谢谢。 (我按照核心蓝牙教程阅读心率监测器)
import UIKit
import CoreBluetooth
let TranslatorServiceCBUUID = CBUUID(string: "16dedcf4-027f-435f-b1e6-22e601276949")
let PredictionCharacteristicCBUUID = CBUUID(string: "16DEDCF4-027F-435F-B1E6-22E601276950")
var raspberryAsPeripheral: CBPeripheral!
class HRMViewController: UIViewController {
@IBOutlet weak var heartRateLabel: UILabel!
var centralManager: CBCentralManager!
override func viewDidLoad() {
super.viewDidLoad()
centralManager = CBCentralManager(delegate: self, queue: nil)
// Make the digits monospaces to avoid shifting when the numbers change
heartRateLabel.font = UIFont.monospacedDigitSystemFont(ofSize: heartRateLabel.font!.pointSize, weight: .regular)
}
func onDigitReceived(_ digit: Int) {
var predictedNumber : String
if (digit == 16) {
predictedNumber = ""
}
else {
predictedNumber = String(digit)
}
heartRateLabel.text = predictedNumber
print("Predicted digit: \(digit)")
}
}
extension HRMViewController : CBCentralManagerDelegate {
func centralManagerDidUpdateState(_ central: CBCentralManager) {
switch central.state {
case .unknown:
print("central.state is .unknown")
case .resetting:
print("central.state is .resetting")
case .unsupported:
print("central.state is .unsupported")
case .unauthorized:
print("central.state is .unauthorized")
case .poweredOff:
print("central.state is .poweredOff")
case .poweredOn:
print("central.state is .poweredOn")
centralManager.scanForPeripherals(withServices: [TranslatorServiceCBUUID])
}
}
func centralManager(_ central : CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) {
print(peripheral)
raspberryAsPeripheral = peripheral
raspberryAsPeripheral.delegate = self
centralManager.stopScan()
centralManager.connect(raspberryAsPeripheral)
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("you connected with the raspberry pi")
raspberryAsPeripheral.discoverServices([TranslatorServiceCBUUID])
}
}
extension HRMViewController : CBPeripheralDelegate {
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
guard let services = peripheral.services else { return }
for service in services {
print(service)
peripheral.discoverCharacteristics(nil, for: service)
}
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
guard let characteristics = service.characteristics else { return }
for characteristic in characteristics {
print(characteristic)
if characteristic.properties.contains(.read) {
print("\(characteristic.uuid): properties contains .read")
peripheral.readValue(for: characteristic)
}
if characteristic.properties.contains(.notify) {
print("\(characteristic.uuid): properties contains .notify")
peripheral.setNotifyValue(true, for: characteristic)
}
}
}
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic,
error: Error?) {
switch characteristic.uuid {
case PredictionCharacteristicCBUUID:
let whichNumber = predictedValue(from: characteristic)
onDigitReceived(whichNumber)
default:
print("Unhandled Characteristic UUID: \(characteristic.uuid)")
}
}
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
print("DISSSCONNNNECCCTEDDDDD")
}
private func predictedValue(from characteristic: CBCharacteristic) -> Int {
guard let characteristicData = characteristic.value else { return -1 }
let byteArray = [UInt8](characteristicData)
return Int(byteArray[0])
/*
let firstBitValue = byteArray[0] & 0x01
if firstBitValue == 0 {
// Heart Rate Value Format is in the 2nd byte
return Int(byteArray[1])
} else {
// Heart Rate Value Format is in the 2nd and 3rd bytes
return (Int(byteArray[1]) << 8) + Int(byteArray[2])
}
*/
}
}