【问题标题】:Swift 2.0 and BLESwift 2.0 和 BLE
【发布时间】:2016-07-26 10:05:43
【问题描述】:

您好,这是我关于堆栈溢出的第一个问题。

我有一个来自 RebLab 的 BLE Blend Micro,比如带有 BLE shield 的 Arduino。

我找到了这个:https://github.com/RedBearLab/iOS/tree/master/BLEFramework/BLE

但是我不知道如何集成或调用该函数,有人可以帮助我吗?

【问题讨论】:

  • 该组件只是 CoreBluetooth 框架的包装器。我建议先探索一下。

标签: swift arduino bluetooth-lowenergy


【解决方案1】:

将 BLE.swift 类添加到您的项目中。

假设您想在视图控制器中使用 BLE 对象:

import UIKit

class ViewController: UIViewController, BLEDelegate {

    var bluetoothManager : BLE!

    override func viewDidLoad() {
        super.viewDidLoad()

        bluetoothManager = BLE()
        bluetoothManager.delegate = self

        bluetoothManager.startScanning(10)
    }

    func bleDidUpdateState() {
        print("Called when ble update did state")
    }

    func bleDidConnectToPeripheral() {
        print("Called when ble did connect to peripheral")
    }

    func bleDidDisconenctFromPeripheral() {
        print("Called when ble did disconnect from peripheral")
    }

    func bleDidReceiveData(data: NSData?) {
        //method called when you receive some data from the peripheral
        print("Called when ble did receive data")
    }
}

您可以使用以下调用连接到设备:

bluetoothManager.connectToPeripheral(bluetoothManager.peripherals[index])

您可以使用以下方式断开与设备的连接:

bluetoothManager.disconnectFromPeripheral(bluetoothManager.peripherals[index])

发送数据使用:

bluetoothManager.send(...some NSDATE...)

【讨论】:

    猜你喜欢
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    相关资源
    最近更新 更多