1

How can I make a BLE communication with swift and arduino device? Now with my code I'm able to send value to my BLE shield but I don't know how to send back a report to my swift application!

I use a HM-10 BLE module and Swift 2.0.

This is my arduino code:

  #include<SoftwareSerial.h>

  SoftwareSerial Try(1,0);

  void setup() {
  pinMode(3, OUTPUT);
  Try.begin(9600);
}

void loop() {
  if(Try.available()){
    switch(Try.read()){
      case('a') :{
        digitalWrite(3, HIGH);
      }
    }
  }
}

This is an easy code that when receive a char 'a' from my application, it switch on a green LED. With that is all ok but i want to send back the report 'Green LED switched on!' to my Swift app.

So this is the Swift code to send the 'a' value to my BLE module.

import UIKit
import CoreBluetooth


class Connection: UIViewController{

    var connee: CBCharacteristic!
    var per: CBPeripheral!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func SendingVal(sender: AnyObject) {

        let Sen = "a"
        let Value: NSData = Sen.dataUsingEncoding(NSUTF8StringEncoding)!

        sendDataToCentral(per, characteristic: connee, data:  Value)

    }


    private func sendDataToCentral(peripheral: CBPeripheral, characteristic: CBCharacteristic, data: NSData) {
        peripheral.writeValue(data, forCharacteristic: characteristic, type: CBCharacteristicWriteType.WithoutResponse)    }

}

So, how can I send back a report from BLE module to my swift app?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Davide95
  • 19
  • 1
  • We guess that you did all the connection/reading services/characteristics. Do you see the value sent in your Arduino? Even if it's wrong? – Larme Mar 08 '16 at 10:26
  • I'm able to see all value in my arduino but... I don't know how to send back a report or a value – Davide95 Mar 08 '16 at 12:12
  • Take a look to this question: http://stackoverflow.com/questions/35794107/swift-ble-communications/39174444#39174444 – nbloqs Aug 26 '16 at 20:56

1 Answers1

0
 func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
   print(characteristic)
}

get value from characteristic This delegate call when value is change. peripheral discover this updated value.

Deepak Tagadiya
  • 2,187
  • 15
  • 28