0

I am trying to send data from the iPhone to HM-10

I have establish a connection with the bluetooth device, however I don't know how to send the data, is this a peripheral delegate function?

When a button is pressed a fixed integer(1,2,3,4...) is to be sent to the BLE device once. Furthermore, I have slider, and I would like the value of the slider to be sent across to

How do I do this?

Thanks

Tejkaran Samra
  • 96
  • 1
  • 14

2 Answers2

0

You're looking for this method:

CBPeripheral.func writeValue(_ data: Data, for characteristic: CBCharacteristic, type: CBCharacteristicWriteType)

See: https://developer.apple.com/reference/corebluetooth/cbperipheral/1518747-writevalue

Lars Blumberg
  • 19,326
  • 11
  • 90
  • 127
  • Hi Lars, I saw the link you referenced, however when i try to do the function writeValue in the button IBAction func bit there is no option for writeValue. e.g. I will do mainPeripheral. and there is not the choice to choose 'writeValue' – Tejkaran Samra Jan 04 '17 at 16:40
  • Do you already have a `CBPeripheral` instance from a peripheral discovery? – Lars Blumberg Jan 04 '17 at 17:26
  • No, I have two view controllers. On one viewcontroller I have a tableview which I use to connect to the bluetooth. On the second viewcontroller I have the buttons that I want to press to send the information to the bluetooth. On the second view controller I am currently using CBPeripheralDelegate only. Is this correct? – Tejkaran Samra Jan 05 '17 at 12:46
  • Lars, I have managed to get the writeValue to appear. However what information do I put into the Data/CBCharacteristic? i am not sure there. This example has some but not the full answer(http://stackoverflow.com/questions/35794107/swift-ble-communications) – Tejkaran Samra Jan 05 '17 at 17:15
  • Do you know how many bytes the characteristic accepts as its input? – Lars Blumberg Jan 05 '17 at 20:47
  • You can install the free app "nRF connect" on your phone to try out writing to the characteristic. It also allows to read values from characteristics. – Lars Blumberg Jan 05 '17 at 20:48
  • Hi Lars, sorry for the delay - I currently have: var writeType: CBCharacteristicWriteType = .withoutResponse var writeCharacteristic: CBCharacteristic? let value = [UInt8(1)] let data = NSData(bytes: value, length: value.count) mainPeripheral?.writeValue(data as Data, for: writeCharacteristic!, type: writeType) print("Button pressed") xcode has no error but the data isn't receiving on the arduino. i will try the nRF app to see what happens there too – Tejkaran Samra Feb 01 '17 at 13:37
0

Before you will write your data to device using CBPerpheral.writeValue function you have to discover that characteristics, services and keep a strong reference to them.

Everything is described in that tutorial from apple: https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/PerformingCommonCentralRoleTasks/PerformingCommonCentralRoleTasks.html#//apple_ref/doc/uid/TP40013257-CH3-SW1

It's in Obj-C but it's easy to translate that into swift

Michał Kwiecień
  • 2,734
  • 1
  • 20
  • 23
  • 1
    thanks for the answer, however I am not so good at swift, thus changing to swift from Obj-C is another layer on complications i don't need :/ – Tejkaran Samra Jan 04 '17 at 16:39