0

I want to communicate with my HM10 via iPhone app, I have already created an app to establish the connection with HM10, I am able to send data to Bluetooth but I am not receiving data, Following is the code:-

It doesn't print anything.

 func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
    // notify the delegate in different ways
    // if you don't use one of these, just comment it (for optimum efficiency :])
    let data = characteristic.value
    guard data != nil else { return }

    // first the data
    delegate.serialDidReceiveData(data!)
  //  print(data)

    // then the string
    if let str = String(data: data!, encoding: String.Encoding.utf8) {
        delegate.serialDidReceiveString(str)
        print("str")
    } else {
        //print("Received an invalid string!") uncomment for debugging
    }

    // now the bytes array
    var bytes = [UInt8](repeating: 0, count: data!.count / MemoryLayout<UInt8>.size)
    (data! as NSData).getBytes(&bytes, length: data!.count)
    delegate.serialDidReceiveBytes(bytes)
    print(bytes)
//    print(delegate.serialDidReceiveData(bytes))
}
func serialDidReceiveString(message: String) {
   // Label.text! = message
    print(message)
Soma Sharma
  • 73
  • 15
  • Do you attempt a read at all? Is `peripheral(_:didUpdateValueFor:error:)` even called? Is it because the string is invalid? Is `data` nil? – Larme Aug 01 '18 at 13:11
  • Can you exactly tell me with code , as m new to core Bluetooth – Soma Sharma Aug 02 '18 at 03:31
  • You need to register to the characteristics if possible (depends on its properties): `setNotifyValue(_:for:)` or just read it `readValue(for:)`, and then only then the delegate method `peripheral(_:didUpdateValueFor:error:)` should be called. – Larme Aug 02 '18 at 08:03
  • Thank you so much , I will try this and update. – Soma Sharma Aug 02 '18 at 08:25
  • hey, I got it worked thanks. – Soma Sharma Aug 07 '18 at 07:04

0 Answers0