Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service is added twice #4

Closed
redssu opened this issue Sep 24, 2023 · 2 comments
Closed

Service is added twice #4

redssu opened this issue Sep 24, 2023 · 2 comments

Comments

@redssu
Copy link

redssu commented Sep 24, 2023

Hey,

I noticed that one of the services passed to the "addServices" function is being added twice. I believe there is an oversight in the "addServices" function (if it is important, I was adding a list with only one service):

override fun addServices(services: List<BleService>) {
// To make sure that we are safe from race condition
val totalServices = services.size - 1
for (i in 0..totalServices) {
servicesToAdd.add(services[i].toGattService())
}
addService(services.last().toGattService())
}

as you can see, you add all the services to the queue and then explicitly add the last one, so it looks like it is added twice. (First time with explicit add, next time with queued add)

I have never used kotlin, but I think it can be solved this way:

    override fun addServices(services: List<BleService>) {
        // To make sure that we are safe from race condition
        val totalServices = services.size - 1
        val numOfServicesToAdd = if (servicesToAdd.peek() != null) totalServices else (totalServices - 1) 
        
        for (i in 0..numOfServicesToAdd) {
            servicesToAdd.add(services[i].toGattService())
        }
        
        // Start a queue if it isn't started
        if (numOfServicesToAdd != totalServices) {
            addService(services.last().toGattService())
        }
    }

I tested this, and this code seems to solve the problem.

If my code is good enough, I can make PR with this change.

Thanks!

@rohitsangwan01
Copy link
Owner

@redssu seems like this is a bug, I was actually planning to leave the race condition handling on flutter side
And just provide a method to add single service, like native

@rohitsangwan01
Copy link
Owner

@redssu fixed in new version ( 1.0 ) now we have to add each service one by one, this will improve the race conditions, and proper errors handeling if any service failed to add

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants