我最近买了一个Bluno,并试图创建一个iPhone应用程序来与之对话。Bluno制造商包括源代码,但它在objective-c中,我正试图将其移植到swift。目前,我可以发现布鲁诺,连接到它,并看到它的服务。不过,我似乎看不到任何特征,当我打印它们的时候得到了零分。我已经设置了一个Bluno,当我发送字符“5”时它会闪烁,但我似乎找不到正确的字符。任何帮助都将不胜感激。这是我当前的代码,Obj-C代码可以找到here

    //
//  ViewController.swift
//  Bluetooth-Interaction
//
//  Created by Frederik Lohner on 26/Oct/15.
//  Copyright © 2015 JeongGroup. All rights reserved.
//

import UIKit
import CoreBluetooth
import SnapKit

class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
    let backgroundView = UIView()
    let scanButton =  UIButton()
    var DFRobotServiceId = "0000dfb0-0000-1000-8000-00805f9b34fb"
    var kBlunoService = "DFB1"
    var kBlunoDataCharacteristic = "dfb1"

    var DFRobotCharacteristicsNameSerialPortId = "0000dfb1-0000-1000-8000-00805f9b34fb"

    var NameCommandId = "0000dfb2-0000-1000-8000-00805f9b34fb"

    // BLE
    var centralManager: CBCentralManager!
    var sensorTagPeripheral: CBPeripheral!

    override func viewDidLoad() {
        scanButton.setTitle("Scan", forState: UIControlState.Normal)
        scanButton.addTarget(self, action: "startScanning", forControlEvents: UIControlEvents.TouchUpInside)
        scanButton.backgroundColor = UIColor.blackColor()
        backgroundView.addSubview(scanButton)
        self.view.addSubview(backgroundView)
        backgroundView.snp_makeConstraints { (make) -> Void in
            make.left.right.top.bottom.equalTo(self.view)
        }
        scanButton.snp_makeConstraints { (make) -> Void in
            make.left.bottom.equalTo(backgroundView)
            make.width.height.equalTo(60)
        }

        // Initialize central manager on load
        centralManager = CBCentralManager(delegate: self, queue: nil)
        //        self.centralManager.stopScan()
    }

    func startScanning() {
//        print("Started Scanning!")
//        //Could add service UUID here to scan for only relevant services
//        self.centralManager.scanForPeripheralsWithServices(nil, options: nil)
        let one = "1"
        let data = one.dataUsingEncoding(NSUTF8StringEncoding)
//        self.sensorTagPeripheral.writeValue(data!, forCharacteristic: , type: .WithoutResponse)
//        self.sensorTagPeripheral.writeValue(data!, forCharacteristic: CBCharacteristic., type: .WithoutResponse)
//        self.centralManager.
    }

    // Check status of BLE hardware
    func centralManagerDidUpdateState(central: CBCentralManager) {
        if central.state == CBCentralManagerState.PoweredOn {
            print("Bluetooth is ON")
            central.scanForPeripheralsWithServices(nil, options: nil)
        } else if central.state == CBCentralManagerState.Resetting {
            print("RESETTING")
        } else if central.state == CBCentralManagerState.Unauthorized {
            print("Not Authorized")
        } else {
            print("Bluetooth switched off or not initialized")
        }
    }
    func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
        //        print(peripheral)
        let deviceName = "Bluno"
        if let nameOfDeviceFound = peripheral.name {
            if (nameOfDeviceFound == deviceName) {
                print("Name was found")
                print("")
                print("")
                print(peripheral)
                //            for (key, value) in advertisementData {
                //                print("\(key) -> \(value)")
                //            }

                // Stop scanning
                self.centralManager.stopScan()
                print("Stopped Scanning")
                // Set as the peripheral to use and establish connection
                self.sensorTagPeripheral = peripheral
                self.sensorTagPeripheral.delegate = self
                self.centralManager.connectPeripheral(peripheral, options: nil)
                print("")
                print("")
                print("Connected")
                print("")
            }
            else {
                print("NOPE.EXE")
            }
        }
    }

    //    // Check if the service discovered is a valid IR Temperature Service
    func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
        if(error != nil) {
            print(error?.description)
        }
        for service in peripheral.services! {
            let thisService = service as CBService
            print("Discovered Service: \(thisService.description)")
            print("Discovered Characteristic: \(thisService.characteristics)")
        }
    }
    func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {
        if(error != nil) {
            print(error?.description)
        }
        for characteristic in service.characteristics! {
            print("Characteristic found: \(characteristic)")
            let one = "1"
            let data = one.dataUsingEncoding(NSUTF8StringEncoding)
            peripheral.writeValue(data!, forCharacteristic: characteristic, type: .WithoutResponse)
            if(String(characteristic.UUID) == kBlunoService) {
                print("Found")
            }
        }
    }

    func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
        print("Did connect to peripheral.", terminator:"")
        peripheral.delegate = self
        peripheral.discoverServices(nil)
        print(peripheral)

    }
    func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) {
        print("Failed to connect to peripheral.")
    }
    //    func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) {
    //        print("CONNECTION FAILED")
    //    }
    func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) {
        print("CONNECTION WAS DISCONNECTED")
    }
}

最佳答案

设法让它工作。代码可以找到here

10-06 03:56