2

I've managed to connect an ios app to my BLE hm-10 device and send it's characteristic a string. However I can't figure out how to send the string once a button is pressed using the @IBAction function.

func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error:NSError?)
{
    print("Found \(service.characteristics!.count) characteristics!: \(service.characteristics)")
    _peripheral = peripheral
    _characteristics = service.characteristics
    let string = "off"

    let data = string.dataUsingEncoding(NSUTF8StringEncoding)


    for characteristic in service.characteristics as [CBCharacteristic]!
    {

        if(characteristic.UUID.UUIDString == "FFE1")
        {
            print("sending data")
            peripheral.writeValue(data!, forCharacteristic: characteristic,type: CBCharacteristicWriteType.WithoutResponse)
        }
    }

}

@IBAction func onButtonTouch(sender: UIButton)
{
    let string = "on"

    let data = string.dataUsingEncoding(NSUTF8StringEncoding)

    _peripheral.writeValue(data!, forCharacteristic: _characteristics, type: CBCharacteristicWriteType.WithoutResponse)

}

When I put this code into the @IBAction function shown at the bottom it does not work as "characteristic" is an unresolved identifier. Is there a way to link this to the peripheral function? I'm relatively new to coding in Swift so any help would be really appreciated. Thanks.

DanB
  • 23
  • 1
  • 5

1 Answers1

3
class viewController: UIViewController {

    var _peripheral: CBPeripheral?
    var _characteristics: [CBCharacteristic]?

    func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error:NSError?)
    {
    print("Found \(service.characteristics!.count) characteristics!: \(service.characteristics)")
    _peripheral = peripheral
    _characteristics = service.characteristics
    let string = "off"

    let data = string.dataUsingEncoding(NSUTF8StringEncoding)


       for characteristic in service.characteristics as [CBCharacteristic]!
        {

            if(characteristic.UUID.UUIDString == "FFE1")
            {
                print("sending data")
                peripheral.writeValue(data!, forCharacteristic: characteristic,type: CBCharacteristicWriteType.WithoutResponse)
            }
        }

    }

    @IBAction func onButtonTouch(sender: UIButton)
    {
    let string = "on"

    let data = string.dataUsingEncoding(NSUTF8StringEncoding)

    let characteristic = //get right on out of _characteristics! array
    _peripheral!.writeValue(data!, forCharacteristic: characteristic, type: CBCharacteristicWriteType.WithoutResponse)

    }
}
D4ttatraya
  • 3,344
  • 1
  • 28
  • 50
  • This fixed my problem with the characteristic identifier however i'm now getting errors regarding the viewController saying "Class viewController has no initializers" and the CharacteristicsDataType/Class? section saying "consecutive declarations on a line must be seperated". Sorry to be a pain. – DanB Aug 24 '16 at 09:51
  • @DanB what error? `view controller` I mentioned just for an example. show your whole class that contains your code. – D4ttatraya Aug 24 '16 at 09:53
  • @DanB you have to replace `CharacteristicsDataType/Class` with the actual data type of `[CBCharacteristic]`. check updated answer – D4ttatraya Aug 24 '16 at 09:58
  • Getting an error at 'forCharacteristic: _characteristics!' of "Cannot convert value of type '[CBCharacteristic]" to expected argument type 'CBCharacteristic'". Really appreciate your help. – DanB Aug 24 '16 at 10:11
  • coz `_characteristics` is an array, you need to get right one characteristic from that array – D4ttatraya Aug 24 '16 at 10:12
  • Actually I can't say that, but you need to apply business logic to get right characteristic or allocate new one! – D4ttatraya Aug 24 '16 at 10:45