Hi
I am making an app to be used with an Arduino but I am struggling bit. I have only been using Xcode for a short while. I am currently using Xcode 8. The app would be used to turn on and turn off an led light.
The idea is that I press a button and then it shows what bluetooth devices (Hm-10)are available. You can then choose which you would like to connect to.
I would like it so that my app is set up so I have a homepage, in the navigation bar at the top right there would be a connect button. When âConnectâ is pressed it goes to the second page where you can choose the bluetooth device to connect to.
Once connected, back on the home page you can press turn on and turn off for the LED light.
The connection to the bluetooth I can do. However I am unable to control it so that when I press the button the table view pops up and you can choose which device to connect to (at the moment it just defaults to choosing one)
Furthermore, how do I set it so that I can choose what info to send to the bluetooth and in turn to the Arduino? In the past I have done this, and I am able to send ASCII characters and that works well for me, but how do i send value from xCode?
My code looks like this at the moment. I only have the one view controller:
import UIKit
import CoreBluetooth
class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate
{
@IBOutlet var BLEStatus: UILabel!
@IBOutlet var ConnectionLabel: UILabel!
var manager: CBCentralManager!
var miband: CBPeripheral!
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
manager = CBCentralManager(delegate: self, queue: nil)
}
func centralManagerDidUpdateState(_ central: CBCentralManager)
{
var consoleMsg = ""
switch (central.state)
{
case.poweredOff:
consoleMsg = "BLE is Powered Off"
BLEStatus.text = "BLE is Powered Off"
case.poweredOn:
consoleMsg = "BLE is Powered On"
manager.scanForPeripherals(withServices: nil, options: nil)
consoleMsg = "Power On - Scanning for peripherals"
BLEStatus.text = "Power On - Scanning for peripherals"
case.resetting:
consoleMsg = "BLE is resetting"
BLEStatus.text = "BLE is resetting"
case.unknown:
consoleMsg = "BLE is in an unknown state"
BLEStatus.text = "BLE is in an unknown state"
case.unsupported:
consoleMsg = "This device is not supported by BLE"
BLEStatus.text = "This device is not supported by BLE"
case.unauthorized:
consoleMsg = "BLE is not authorised"
BLEStatus.text = "BLE is not authorised"
}
print("\(consoleMsg)")
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral)
{
print("did connect peripheral")
BLEStatus.text = "did connect peripheral"
peripheral.delegate = self
peripheral.discoverServices(nil)
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber)
{
print("Peripheral Bluetooth name is: \(peripheral.name)")
BLEStatus.text = ("Peripheral Bluetooth name is: \(peripheral.name)")
self.miband = peripheral
manager.connect(miband, options: nil)
manager.stopScan()
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?)
{
print("did discover services part")
BLEStatus.text = "did discover services part"
if let servicePeripherals = peripheral.services as [CBService]!
{
for service in servicePeripherals
{
peripheral.discoverCharacteristics(nil, for: service)
}
}
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?)
{
print("did discover charcteristics")
BLEStatus.text = "did discover charcteristics"
if let characterArray = service.characteristics as [CBCharacteristic]!
{
for cc in characterArray
{
if(cc.uuid.uuidString == "0xFFE1")
{
print ("discovered some characteristic")
peripheral.readValue(for: cc)
}
}
}
}
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?)
{
print("did update value part")
if (characteristic.uuid.uuidString == "FF06")
{
let value = characteristic.value!.withUnsafeBytes
{
(pointer: UnsafePointer<Int>) -> Int in
return pointer.pointee
}
print("the characteristic is sent over here: (\"(value)")
}
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Regarding table view, is there a really simple way of doing it?
My understanding so far goes as this:
Create View controller
Add table view
add new file (cocoa class table view controller) and link to the view controller
add relevant code
is that correct? are there any other steps i am missing?
i know this is an awful lot of questions, but I would really appreciate some help
Tej