Swift - Ble Communications

Swift BLE communication between iPhone and ESP32

it is important that you advertise the service to be found, not only create/facilitate. Apps like nRF connect can help you determine if the service is advertised, otherwise the scan for services (that particular service) will be unsuccessful.

And for the second question: watch his work https://www.raywenderlich.com/231-core-bluetooth-tutorial-for-ios-heart-rate-monitor

You can read out characteristics intended to be read and vice versa to write. Notification is not always needed.

Core Bluetooth - Communicate with LED Light

One thing you could try would be to get rid of the dispatch queue all together and write the value directly. Of course, some values will not be written since the phone will still be handling the last value transmission. You could simply ignore this error. Something like this could do the trick:

if sender == nil || sliderVal % 1 == 0 {
print(sliderVal)

if sender != nil, previousValue == sliderVal {
return
}
previousValue = sliderVal
writeLEDValueToChar( withCharacteristic: char, withValue: data)
}

Another way could be to not use every possible value from the slider by setting the step width of the slider:

Slider(
value: $speed,
in: 0...100,
step: 5
)

This way you would get only the values 0, 5, 10, 15, ... instead of 0.0000, 0.0001, 0.0002, ... which would decrease the amount of entries in the DispatchQueue while moving the slider

IOS BLE communication write value;

Your problem is that you are accessing the characteristic.value immediately after the call to readValueForCharacteristic, but it takes some time for Core-Bluetooth to contact the peripheral, request a value for the characteristic and for the peripheral to reply.

If you refer to the documentation for this method you will see -

Discussion

When you call this method to read the value of a
characteristic, the peripheral calls the
peripheral:didUpdateValueForCharacteristic:error: method of its
delegate object. If the value of the characteristic is successfully
retrieved, you can access it through the characteristic’s value
property.

So, you need to access characteristic.value in your didUpdateValueForCharacteristic: CBPeripheral delegate method

I'm trying to receive indicate via BLE connection, but I can't receive any data

I have example iOS projects (Central & Peripheral) which send/receive indications: https://github.com/alexanderlavrushko/BLEProof-collection

setNotifyValue here is called similarly as you do, it should be fine.

I suggest to check the way how the characteristic is created and updated on the Peripheral side, iOS example link.

Also there is a great iOS application LightBlue which can simulate a BLE device, see this guide:

  • Central - the topic "Subscribing to Characteristics" might be useful
  • Peripheral - "Adding a New Virtual Peripheral", but use Blank device and configure services/characteristics you need

iOS secure communication with paired BLE device

In that case you need to implement some secure protocol on top of GATT / BLE, since as you say iOS gives all apps access, regardless if it's paired or not. You may also want to make sure no one can reverse engineer this. But at this point we are no longer really discussing BLE but just general security. So you might be better off asking at https://security.stackexchange.com.

But note that the other app will not see the communication that the first app does to / from the peripheral, with the exception of notifications / indications. However what you need to solve is how the peripheral knows if a request comes from the correct app.



Related Topics



Leave a reply



Submit