2

I'm writing ann application in Swift that requires I be able to send and receive data from an iOS device to an RFDuino. Building on top of cconway's UBP library, I've been able to get my RFDuino's temperature measurements to display on my iPad, but now I'm working on getting the iPad to send a simple message to the RFduino.

I'm using the original Objective-C code as a reference. It can be found here.

Here's how I'm looking for characteristics:

func peripheral(peripheral: CBPeripheral!, didDiscoverCharacteristicsForService service: CBService!, error: NSError!) {
    if let characteristicsArr = service.characteristics  as? [CBCharacteristic]
    {

        println("Discovered characteristics on RFduino");

        for aCharacteristic in characteristicsArr {

                if (aCharacteristic.properties & CBCharacteristicProperties.Notify) == CBCharacteristicProperties.Notify {

                    // Register to be notified whenever the RFduino transmits
                    peripheral.setNotifyValue(true, forCharacteristic: aCharacteristic)
                }

                if (aCharacteristic.properties & CBCharacteristicProperties.Write) == CBCharacteristicProperties.Write {

                    // Register to be notified whenever the RFduino transmits
                    loadedService = true
                    sendCharacteristic = aCharacteristic
                    peripheral.setNotifyValue(true, forCharacteristic: aCharacteristic)
                }

        }      

And here's my function for sending when a button is pressed:

func send() {        
    if let service = loadedService{
        if _selectedPeripheral?.state == CBPeripheralState.Connected {
            var parameter = NSInteger(1)
            let data = NSData(bytes: &parameter, length: 1)
            if let characteristic:CBCharacteristic? = sendCharacteristic{
                _selectedPeripheral?.writeValue(data, forCharacteristic: characteristic!, type: CBCharacteristicWriteType.WithResponse)
            }
        }
    }
}

Now if I actually push the send button, I get disconnected from the RFDuino. Any ideas what I might be doing wrong?

EDIT

Here's how you should look for the write characteristic of the RFDuino. This is pretty half baked, will repost if i come up with a better scheme.

    func peripheral(peripheral: CBPeripheral!, didDiscoverCharacteristicsForService service: CBService!, error: NSError!) {
    if let characteristicsArr = service.characteristics  as? [CBCharacteristic]
    {


        println("Discovered characteristics on RFduino");
        for (number, aCharacteristic) in enumerate(characteristicsArr){

                println("charc type: \(_stdlib_getTypeName(aCharacteristic.UUID))")

                if (aCharacteristic.properties & CBCharacteristicProperties.Notify) == CBCharacteristicProperties.Notify {

                    // Register to be notified whenever the RFduino transmits
                    peripheral.setNotifyValue(true, forCharacteristic: aCharacteristic)
                }

            if compareID(aCharacteristic.UUID, secondID: writeUUID()){
                    println("\(aCharacteristic) Found Something!")
                    // Register to be notified whenever the RFduino transmits
                    loadedService = true
                    sendCharacteristic = aCharacteristic
                    peripheral.setNotifyValue(true, forCharacteristic: aCharacteristic)
                }

        }
    }
}

where compareUUID and writeUUID are defined as:

private func compareID(firstID:CBUUID, secondID:CBUUID)->Bool {
    return firstID.UUIDString == secondID.UUIDString

}

func writeUUID() -> CBUUID{
    return CBUUID(string:"2222") //write char' of rfduino - how to determine 'on the fly'?
}
RYS
  • 442
  • 6
  • 22
  • What are the properties of the characteristic you are trying to write? Does it support write with notify? Or only write – Paulw11 Mar 08 '15 at 22:28
  • Here's what I got from a simple print statement: Characteristic(0): Characteristic(1): Characteristic(2): – RYS Mar 08 '15 at 23:05
  • I've switched the writeType to 'without response'. By the way, I'm declaring 'sendCharacteristic' to be an optional CBCharacteristic. In the send function I find that the UUID is 2223 with a properties values of 0xC. – RYS Mar 08 '15 at 23:11
  • 0xc indicates both write and write with response are supported. What error do you get in `didDisconnectPeripheral`? – Paulw11 Mar 08 '15 at 23:16
  • I get CBErrorDomain - code 7, "The specified device has disconnected from us" – RYS Mar 08 '15 at 23:34
  • And what error do you get in `peripheral:didWriteValueForCharacteristic:error:` (`CBPeripheralDelegate` method) when you write with response? – Michał Ciuba Mar 09 '15 at 10:13
  • 1
    I resolved this. can't use my method for detecting 'write' characteristics simply by looking at which characteristics have write. Turns out both the 'write' and 'disconnect' characteristics of the rfduino are writable, and disconnect comes last so I was writing to disconnect! I found which characteristic belongs to the write char '2222' and implemented a function that checks the uuid against that down value. From there the rest of my code was ok. – RYS Mar 12 '15 at 10:39

0 Answers0