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!

nbloqs
  • 3,152
  • 1
  • 28
  • 49
Ross M.
  • 313
  • 2
  • 13

1 Answers1

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

Community
  • 1
  • 1
nbloqs
  • 3,152
  • 1
  • 28
  • 49