2016-01-16 93 views
4

我對iOS編程和藍牙協議都很陌生。 我發現了一個快速編寫的示例代碼,並試圖修改它以使用我自己的藍牙模塊。我有的模塊是dorji的DBM01適用於iOS的基本BLE通信應用程序swift

我需要使用該服務FFF0和特點是FFF1用於發送ASCII值。

當我在我的MacBook上使用LightBlue應用程序並連接到我設計的,其上有DBM01模塊的電路板時,我可以發送「1」的char值並獲得期望的響應(打開LED),當我發送「0」的值時,它關閉LED。

現在用我的代碼,我可以連接到DBM01模塊。我可以打印它的名字。但是,我不能使用以下功能從它斷開連接。我也不確定這是否與設備斷開連接,或者在設備斷開連接時自動呼叫。無論如何,這種方式無法奏效。

func centralManager(central: CBCentralManager, 
      didDisconnectPeripheral peripheral: CBPeripheral, 
      error: NSError?) 

我的主要問題是,我真的不明白,我怎麼指定服務和我感興趣的特點和連接到具有他們特定的設備。

我也無法發送消息。當我嘗試,我得到了預定義的錯誤,因爲以下條件我以前不抱

if writeCharacteristic != nil 

下面是我的全部代碼。

如果您能指出我做錯了什麼,以及如何通過特定的服務和特性信息以及發送數據實現與特定設備的連接,請注意。

// 
// ViewController.swift 
// bleSwift 
// 

import UIKit 
import CoreBluetooth 

class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate { 

    var centralManager: CBCentralManager! 
    var peripheral: CBPeripheral! 
    var writeCharacteristic: CBCharacteristic! 
    var service: CBService! 
    var characteristic: CBCharacteristic! 

    var bluetoothAvailable = false 
    let message = "1" 

    @IBOutlet weak var deviceName: UILabel! 
    @IBOutlet weak var ServiceName: UILabel! 
    @IBOutlet weak var CharacteristicsName: UILabel! 

    func centralManagerDidUpdateState(central: CBCentralManager) 
    { 
     print("Checking state") 
     switch (central.state) 
     { 
     case .PoweredOff: 
      print("CoreBluetooth BLE hardware is powered off") 

     case .PoweredOn: 
      print("CoreBluetooth BLE hardware is powered on and ready") 
      bluetoothAvailable = true; 

     case .Resetting: 
      print("CoreBluetooth BLE hardware is resetting") 

     case .Unauthorized: 
      print("CoreBluetooth BLE state is unauthorized") 

     case .Unknown: 
      print("CoreBluetooth BLE state is unknown"); 

     case .Unsupported: 
      print("CoreBluetooth BLE hardware is unsupported on this platform"); 

     } 
     if bluetoothAvailable == true 
     { 
      discoverDevices() 
     } 
    } 

    func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) 
    { 
       // Stop scanning 
       self.centralManager.stopScan() 
       print("Stopped Scanning") 
       // Set as the peripheral to use and establish connection 
       //self.peripheral = peripheral 
       //self.peripheral.delegate = self 
       //self.centralManager.connectPeripheral(peripheral, options: nil) 
       peripheral.discoverServices([CBUUID(string: "FFF0")]) 
       print("CONNECTED!!") 
       print(peripheral.name) 
       deviceName.text = peripheral.name 
    } 



    func discoverDevices() { 
     print("Discovering devices") 
     centralManager.scanForPeripheralsWithServices(nil, options: nil) 

    } 

    @IBAction func disconnectDevice(sender: AnyObject) { 

     func centralManager(central: CBCentralManager, 
      didDisconnectPeripheral peripheral: CBPeripheral, 
      error: NSError?) 
     { 
      print("CONNECTION WAS DISCONNECTED") 
      deviceName.text = "Disconnected" 
     } 
    } 

    @IBAction func Scan(sender: AnyObject) 
    { 
     print("Scan") 
     centralManager = CBCentralManager(delegate: self, queue: nil) 
    } 



    @IBAction func Send(sender: AnyObject) 
    { 
     let data = message.dataUsingEncoding(NSUTF8StringEncoding) 
     if writeCharacteristic != nil 
     { 
      print("Sent") 
      peripheral!.writeValue(data!, forCharacteristic: writeCharacteristic, type: CBCharacteristicWriteType.WithoutResponse) 
     } 
     else 
     { 
      print("Couldn't Send") 
     } 
    } 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


} 
+0

我創建了一個GitHub的演示應用程序,它列出了藍牙LE設備。一旦你有了'CBPeripheral'對象,就可以連接它併發送數據。 (查看[這個答案](https://stackoverflow.com/a/44782698/784318)) – Besi

回答

4

爲了將數據發送到您的BLE外設,你應該:

  1. 開始掃描。
  2. 在您的中央管理器代表中,您將通過發現的外設接收didDiscoverPeripheral回調。設置自己作爲其代表並連接到它:centralManager.connectPeripheral(...)
  3. 在委託中接收didConnectPeripheral。現在請爲這個外設調用discoverServices。
  4. 在您的委託中接收didDiscoverServices。最後,請爲您的服務調用discoverCharacteristics。
  5. 您將在didDiscoverCharacteristic委託方法中接收特徵。之後,你將能夠發送數據到你的外設的確切特徵。
  6. 要斷開外圍,調用方法centralManager.cancelPeripheralConnection(...)
相關問題