Basic BLE communication app for iOS with swift -


i quite new ios programming , bluetooth protocol. have found sample code written in swift , trying modify work own bluetooth module. module have dbm01 dorji.

the service need use fff0 , characteristics fff1 sending ascii value.

when use lightblue app on macbook , connect board have designed, has dbm01 module on it, can send char value of "1" , expected response (turning on led) , when send value of "0" turns led off.

now code have, can connect dbm01 module. can print name. however, cannot disconnect following function. not sure if disconnecting device or called automatically when device disconnected. regardless, not work either way.

func centralmanager(central: cbcentralmanager,             diddisconnectperipheral peripheral: cbperipheral,             error: nserror?) 

my main problem didn't understand how specify service , characteristics interested in , connect specific device has them.

i unable send message. when try got predefined error, since following condition did't hold

if writecharacteristic != nil 

below full code.

appreciate if can point out doing wrong , how can achieve connecting specific device specific service , characteristics information , sending data.

// //  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 powered off")          case .poweredon:             print("corebluetooth ble hardware powered on , ready")             bluetoothavailable = true;          case .resetting:             print("corebluetooth ble hardware resetting")          case .unauthorized:             print("corebluetooth ble state unauthorized")          case .unknown:             print("corebluetooth ble state unknown");          case .unsupported:             print("corebluetooth ble hardware unsupported on 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 peripheral use , 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 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()         // additional setup after loading view, typically nib.     }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     }   } 

in order send data ble peripheral, should:

  1. start scanning.
  2. in ble central manager delegate you'll recieve diddiscoverperipheral callback discovered peripheral. set delegate , connect it: centralmanager.connectperipheral(...)
  3. receive didconnectperipheral in delegate. call discoverservices peripheral.
  4. recieve diddiscoverservices in delegate. finaly, call discovercharacteristics service.
  5. you'll recieve characteristic in diddiscovercharacteristic delegate method. after that, you'll able send data exact characteristic of peripheral.
  6. to disconnect peripheral, call method centralmanager.cancelperipheralconnection(...)

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -