Reputation: 462
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: ¶meter, 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?
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'?
}
Upvotes: 2
Views: 1191