java - Waiting for a callback method to be called in Android -
i writing android application interacts sensor using bluetooth , obtains temperature values. doing calling connectgatt() asynchronous , calls callback once connection established. problem facing code has wait until connection gets established.
this implementation of method being used in code below.
public boolean connect(final string address) { log.v(log_tag,"in connect method"+thread.currentthread().getname()); if(btadapter == null || address == null) { log.v(log_tag,"unable bluetooth adapter or address not valid"); return false; } if(address != null && address.equals(btaddress) && btgatt != null) { log.v(log_tag,"trying connect bt gatt profile directly"); boolean result = btgatt.connect(); if(result) return true; return false; } btdevice = btadapter.getremotedevice(address); if(btdevice == null) { log.v(log_tag,"could not find device."); return false; } btgatt = btdevice.connectgatt(this,false,btgattcallback); log.v(log_tag,btgatt.tostring()); btaddress = address; log.v(log_tag,"connecting device"); btconnectionstate = state_connecting; return true; }
currently solve problem writing following code in handler thread don't want block ui thread while waiting callback. not convinced approach.
if(mlocation != null) { connect(btaddress); // method encapsulates calls connectgatt method. while (btconnectionstate != state_connected) { continue; } while (!services_discovered) { continue; } //other code
i feel there better ways of solving couldn't find on web. saw couple of answers using countdownlatch , semaphores didn't understand them clearly.
can me understanding how handle situations these? thank you.
wiki
Comments
Post a Comment