2016-07-26 106 views

回答

0

将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...)