首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从iOS向Bluno发送数据

从iOS向Bluno发送数据
EN

Stack Overflow用户
提问于 2015-11-13 05:30:14
回答 1查看 663关注 0票数 2

我最近买了一个Bluno,并试图创建一个iPhone应用程序来与它交谈。Bluno制造商包括源代码,但它在objective中,我正在尝试将它移植到swift。目前,我可以发现Bluno,连接到它并查看它的服务。然而,我似乎没有看到任何特征,并得到一个零当我打印他们。我已经设置了Bluno,这样一旦我发送给它字符"5“,它就会闪烁,但是我似乎找不到正确的特性来做到这一点。任何帮助都将不胜感激。下面是我当前的代码,Obj代码可以找到这里

代码语言:javascript
复制
    //
//  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")
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-14 21:08:50

设法让这件事起作用了。代码可以找到这里

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33686494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档