title: Basic BLE communication app for iOS with swift
body: <p>I am quite new to iOS programming and also to bluetooth protocol.
  I have found a sample code written in swift and trying to modify it to work with my own bluetooth module. The module I have is <a href="http://dorji.com/docs/data/DBM01.pdf" rel="noreferrer">DBM01</a> from dorji.</p>
  
  <p>The service I need to use is <strong>FFF0</strong> and the characteristics is <strong>FFF1</strong> for sending a ASCII value.</p>
  
  <p>When I use the LightBlue app on my macbook and connect to the board I have designed, which has the DBM01 module on it, I can send the char value of "1" and I get the expected response (Turning on a LED) and when I send value of "0" it turns the LED off.</p>
  
  <p>Now with the code I have, I can connect to the DBM01 module. I can print its name. However, I cannot disconnect from it with the following function. I am also not sure if this is for disconnecting from the device or it is called automatically when the device is disconnected. Regardless, it does not work either way.</p>
  
  <pre><code>func centralManager(central: CBCentralManager,
              didDisconnectPeripheral peripheral: CBPeripheral,
              error: NSError?)
  </code></pre>
  
  <p>My main problem is I really didn't understand how I specify the service and the characteristics that I am interested in and connect to specific device that has them.</p>
  
  <p>I am also unable to send a message. When I try I got the predefined error, since the following condition did't hold</p>
  
  <pre><code>if writeCharacteristic != nil
  </code></pre>
  
  <p>Below is my full code.</p>
  
  <p>Appreciate if you can point out where I am doing wrong and how I can achieve connecting to specific device with specific service and characteristics information and sending data.</p>
  
  <pre><code>//
  //  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.
      }
  
  
  }
  </code></pre>