I am new to programming on iOS as well as on the Bluetooth protocol. I found a sample code written in swift and trying to change it to work with my own Bluetooth module. The module I have is dorji DBM01.
I need to use FFF0 strong> and the characteristics of FFF1 to send the ASCII value.
When I use the LightBlue application on my macbook and connect to a board that I developed that has a DBM01 module, I can send the char value "1" and I get the expected response (Turn on the LED), and when I send the value "0" He turns off the LED.
Now with the code that I have, I can connect to the DBM01 module. I can print his name. However, I cannot disconnect from it with the following function. I'm also not sure if this is to disconnect from the device or it is called automatically when the device is disconnected. Despite this, it does not work anyway.
func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?)
My main problem is that I really donβt understand how I specify the service and characteristics that interest me, and connect to a specific device that has them.
I also can not send a message. When I try, I got a predefined error as the following condition did not hold back
if writeCharacteristic != nil
Below is my full code.
Evaluate whether you can indicate where I am doing wrong, and how I can achieve a connection to a specific device with specific information about services and features and 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 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. } }