deviceplug / btleplug

Rust Cross-Platform Host-Side Bluetooth LE Access Library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

(dev branch - async) macOS needs to discern between WriteWithResponse and WriteWithoutResponse

qdot opened this issue · comments

commented

Describe the bug

  1. Write a WriteWithoutResponse packet to a WriteWithoutResponse only endpoint on macOS

Expected behavior
Should return Ok(())

Actual behavior
Stalls indefinitely. If we write a WriteWithoutResponse packet to a WriteWithResponse only endpoint, things work fine.

Additional context

According to the docs (https://developer.apple.com/documentation/corebluetooth/cbperipheral/1518747-writevalue?language=objc):

When you call this method to write the value of a characteristic, the peripheral calls the peripheral:didWriteValueForCharacteristic:error: method of its delegate object only if you specified the write type as CBCharacteristicWriteWithResponse. The response you receive through the peripheral:didWriteValueForCharacteristic:error: delegate method indicates whether the write was successful; if the write failed, it details the cause of the failure in an error.
On the other hand, if you specify the write type as CBCharacteristicWriteWithoutResponse, Core Bluetooth attempts to write the value but doesn’t guarantee success. If the write doesn’t succeed in this case, you aren’t notified and you don’t receive an error indicating the cause of the failure.

So, for WriteWithoutResponse endpoints, we should resolve the corresponding future ourselves, as we'll never get back a response via the callback. However, for endpoints that ONLY handle WriteWithResponse, we should file the future to wait for the callback, otherwise we'll panic when there's nothing to resolve in the callback.

commented

There's an argument to be made that we should probably error out if WriteWithoutResponse is sent to a WriteWithResponse only characteristic, but all of the OS APIs seem to handle this fine, and this mostly has to do with our async resolution strategy on macOS, so it's probably on us. :|