How Can I set timeout interval when use bluetooth

Published

When I use bluetooth to write data, I hope get response. But when the peripheral goes wrong, it doesn’t send notification. I need set a timeout interval to handle this bad interaction. Like we use urlrequest:

    /// Creates and initializes a URLRequest with the given URL and cache policy.
    /// - parameter: url The URL for the request. 
    /// - parameter: cachePolicy The cache policy for the request. Defaults to `.useProtocolCachePolicy`
    /// - parameter: timeoutInterval The timeout interval for the request. See the commentary for the `timeoutInterval` for more information on timeout intervals. Defaults to 60.0
    public init(url: URL, cachePolicy: CachePolicy = .useProtocolCachePolicy, timeoutInterval: TimeInterval = 60.0) {
        _handle = _MutableHandle(adoptingReference: NSMutableURLRequest(url: url, cachePolicy: cachePolicy, timeoutInterval: timeoutInterval))
    }

How could I make it.

Source: Ios

Published
Categorised as bluetooth, ios

Answers

By “when the peripheral goes wrong” – if you mean that the peripheral crashes or stops working then you should get a BLE disconnection event to indicate the crash:

(centralManager:didDisconnectPeripheral:) 

If this is not the case and you just stop receiving notifications after some time and the BLE connection is still alive, then there is no way to tell why the peripheral stopped sending notifications. The reason for this is that there is no specific “time” associated with notifications. Some peripherals send notifications every 1 second and some send notifications every 1 week. Some peripherals send notifications on a value change (e.g. if the temperature increases by 1 degree) and some send notifications on a user action (e.g. the user pressed a button).

The only workaround for this is if you add a timer in your central device, then every time you receive a notification using:

peripheral(_:didUpdateValueFor:error:)

you can reset that timer (if it is the exact notifications you’re expecting to timeout). Then if the timer expires, you know that you did not receive your notification on time as expected and therefore you can flag an error or force a disconnection. This is just one example and there are a few variations of this that you can create (e.g. set a flag on peripheral(_:didUpdateValueFor:error:) that you check and reset every 30 seconds). You can find more information about timers in the links below:-

I hope this helps.


Maryse Sporer

Leave a Reply

Your email address will not be published. Required fields are marked *

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq