Reputation: 677
My app can connect with a blutooth device and read data, but when the bluetooth is offline, my app will freeze the screen until it finds the device or timeout. Here are the main functions:
Manifest.xml
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyGatewayService"
android:enabled="true"
android:exported="true"></service>
</application>
</manifest>
BluetoothManager.java
public class BluetoothManager {
private static final String TAG = BluetoothManager.class.getName();
private static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
public static BluetoothSocket connect(BluetoothDevice dev) throws IOException{
BluetoothSocket sock = null;
BluetoothSocket socketFallback = null;
Log.d(TAG,"Start Bluetooth Connection...");
try
{
sock = dev.createRfcommSocketToServiceRecord(myUUID);
Log.d(TAG, "Probably gonna wait here...");
sock.connect();
}catch (Exception e1){
Log.e(TAG, "There was an error while establishing Bluetooth connection, Failing back...", e1);
}
return sock;
}
}
MyGateWayService.java
public class MyGatewayService extends AbstractGatewayService{
private static final String TAG = MyGatewayService.class.getName();
private BluetoothDevice dev = null;
private BluetoothSocket sock = null;
@Override
protected void onHandleIntent(Intent intent) {
//what should I put in here?
}
@Override
public void startService() throws IOException {
final String remoteDevice = getSharedPreferences(Constants.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE).getString(Constants.SHARED_PREFERENCES_BLUETOOTH_SELECTION_ADDRESS_KEY, "");
if (remoteDevice == null||"".equals(remoteDevice)){
Toast.makeText(getApplicationContext(),"No Bluetooth device selected...",Toast.LENGTH_SHORT).show();
Log.e(TAG,"No Bluetooth device selected...");
stopService();
throw new IOException();
}else{
final BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
dev = btAdapter.getRemoteDevice(remoteDevice);
Log.d(TAG,"Stop bluetooth discovery...");
btAdapter.cancelDiscovery();
Log.d(TAG,"Start Service..");
try{
startServiceConnection();
}catch (Exception e){
Log.e(TAG, "There was an error while establishing connection..." + e.getMessage());
stopService();
throw new IOException();
}
}
}
private void startServiceConnection() throws IOException, InterruptedException {
Log.d(TAG, "Start the connection");
isRunning = true;
try{
sock = com.ibm.us.wuxiaosh.androidbluetoothdemo.BluetoothManager.connect(dev);
}catch (Exception e2){
Log.e(TAG, "There was an error while connecting... stop...");
stopService();
throw new IOException();
}
}
@Override
protected void executeQueue(){
Log.d(TAG,"Executing...");
while(!Thread.currentThread().isInterrupted()){
//Log.d(TAG,"Executing ....................");
}
}
@Override
public void stopService() {
isRunning = false;
if (sock!=null){
try{
sock.close();
}catch (IOException e){
Log.e(TAG, e.getMessage());
}
stopSelf();
}
}
}
Mainfunction:
@Override
public void onServiceConnected(ComponentName className, IBinder binder) {
Log.d(TAG, className.toString() + " service is bound");
isServiceBound = true;
service = ((AbstractGatewayService.AbstractGatewayServiceBinder) binder).getService();
service.setContext(MainActivity.this);
Log.d(TAG, "Starting live data");
try{
service.startService();
Log.d(TAG , "Connected");
} catch (IOException ioe){
Log.e(TAG, "Failure Starting Live Data");
doUnbindService();
}
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public void onServiceDisconnected(ComponentName className) {
Log.d(TAG, " service is unbound");
isServiceBound = false;
}
};
private void doBindService(){
if(!isServiceBound){
Log.d(TAG, "Binding OBD Service..");
Log.e(TAG,"start intent 1");
Intent serviceIntent = new Intent(this, MyGatewayService.class);
Log.e(TAG,"intent finished");
bindService(serviceIntent,serviceConn, Context.BIND_AUTO_CREATE);
Log.e(TAG,"bindService");
}
}
private void doUnbindService(){
if(isServiceBound){
if (service.isRunning()){
service.stopService();
Log.d(TAG,"Ready");
}
Log.e(TAG, "Unbinding OBD Service...");
unbindService(serviceConn);
isServiceBound = false;
Log.e(TAG, "Disconnected");
}
}
UPDATE: another file: AbstractGateWayService & changed it to extends intentService
public abstract class AbstractGatewayService extends IntentService{
private static final String TAG = AbstractGatewayService.class.getName();
private final IBinder binder = new AbstractGatewayServiceBinder();
protected Context ctx;
protected boolean isRunning = false;
Thread t = new Thread(new Runnable() {
@Override
public void run() {
Log.e(TAG, "Thread Run...");
long futureTime = System.currentTimeMillis()+10000;
while(System.currentTimeMillis()<futureTime) {
executeQueue();
}
}
});
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "Creating Service...");
t.start();
Log.d(TAG, "Service Creating...");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG,"Destroying service");
t.interrupt();
Log.d(TAG,"Service Destroyed");
}
class AbstractGatewayServiceBinder extends Binder {
public AbstractGatewayService getService(){
return AbstractGatewayService.this;
}
}
public boolean isRunning() {
return isRunning;
}
public void setContext(Context c) {
ctx = c;
}
abstract protected void executeQueue();
abstract public void startService() throws IOException;
abstract public void stopService();
}
When I press the connect button, the app will initialize the service but it will also freeze the animation on the main thread, even if I explicitly put it in a separate thread. Based on the logcat I think the sock.connect()
funtion in the BluetoothManager.java
file is waiting for the callback. How should I implement to make sure everything runs in the background?
I also put all the code in here
Any help are welcome! Thanks
Upvotes: 1
Views: 483
Reputation: 7082
Your problem is that you invoke the Socket connection logic (which, as you correctly deducted, is a blocking call) in the startService()
method. Services
in Android, while logically separate from the main flow of the app, run on the same Thread
by default. By calling a blocking call in the service, you block the UI Thread.
You have several solutions to choose from:
IntentService
instead of Service
. IntentService
has its own Handler and operates on a separate Thread.AsyncTask
. IMPORTANT: you need to wrap the logic itself, not the Service
-starting logic.Service
part altogether and just put the logic in an AsyncTask
It is up to you what you choose, the IntentService
solution seems best for a more complex app, while creating an AsyncTask
would be the quickest and most straight-forward approach :-)
Upvotes: 2