4

I'm new to iOS and BLE. I've been following a tutorial to connect and send data to a BLE receiver board on an Arduino. The tutorial was designed for a different board, and I'm using the (the BLE board is the Adafruit nrf8001 if that might help).

The following code sets up the UUIDs...

let BLEServiceUUID = CBUUID(string: "6E400001-B5A3-F393-E0A9-E50E24DCCA9E") let PositionCharUUID = CBUUID(string: "6E400002-B5A3-F393-E0A9-E50E24DCCA9E") let BLEServiceChangedStatusNotification = "kBLEServiceChangedStatusNotification"

And this code connects my iOS device to the BLE board.

central.connectPeripheral(peripheral, options: nil)

I then have a slider set up in my ViewController, and I want to send the value of that slider to the BLE board. Here's my writePosition function:

func writePosition(position: UInt8)->NSData { let currentValue = NSUserDefaults.standardUserDefaults().floatForKey("sliderValue") var currentValueString=NSString(format: "%.5f", currentValue) var writeType:CBCharacteristicWriteType let newString:NSString = currentValueString let data = NSData(bytes: newString.UTF8String, length: newString.length) peripheral?.writeValue(data, forCharacteristic: self.positionCharacteristic, type: writeType) return data }

Note that both my arduino AND my app confirm that they are indeed connected. However, if I change the slider value in the app, it does not send its value to the BLE board. What am I doing wrong??

Thanks in advance!

2
  • Any ideas guys? I've been stuck on this for a while... Commented Mar 31, 2015 at 2:54
  • Did you use the function peripheral.discoverServices(nil) ? Commented Dec 17, 2015 at 15:01

1 Answer 1

0

Please note that writing data to a BLE can not be done (typically), faster than 35 to 50 ms. It's a good idea to use a timer (which in iOS has a minimum tick between 50 to 100 ms) to write to the charactestistic, instead of writing directly from your slider. So the timer polls the slider's value and writes it. More advanced apps should use a tx queue.

Also, you may want to take a look to this answer: SWIFT - BLE communications

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.