mjm mjm - 9 days ago 6
Android Question

Service not binding in Android

I have a

service
in
Android
. The following is the
service


public class BTService extends Service {

private static final String TAG = "BTService";

// Constants that indicate the current connection state
public static final String ACTION_STATE_NONE = "ACTION_STATE_NONE"; // we're doing nothing
public static final String ACTION_STATE_LISTEN = "ACTION_STATE_LISTEN"; // now listening for incoming connections
public static final String ACTION_STATE_CONNECTING = "ACTION_STATE_CONNECTING"; // now initiating an outgoing connection
public static final String ACTION_STATE_CONNECTED = "ACTION_STATE_CONNECTED"; // now connected to a remote device
public final static String ACTION_STATE_DISCONNECTED = "ACTION_STATE_DISCONNECTED"; // now disconnected to a remote device

// Member fields
private BluetoothManager mBluetoothManager;
private BluetoothAdapter mBluetoothAdapter;

private String mState;

// Binder given to clients
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
public BTService getService() {
return BTService.this;
}
}

@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind");
return mBinder;
}

@Override
public boolean onUnbind(Intent intent) {
// stop();
return super.onUnbind(intent);
}

@Override
public void onCreate() {
Log.d(TAG, "onCreate");
super.onCreate();
}

/**
* Initializes a reference to the local Bluetooth adapter.
*
* @return Return true if the initialization is successful.
*/
public boolean initialize() {
Log.d(TAG, "initialize");
// For API level 18 and above, get a reference to BluetoothAdapter
// through
// BluetoothManager.
if (mBluetoothManager == null) {
mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
if (mBluetoothManager == null) {
Log.e(TAG, "Unable to initialize BluetoothManager.");
return false;
}
}

mBluetoothAdapter = mBluetoothManager.getAdapter();
if (mBluetoothAdapter == null) {
Log.e(TAG, "Unable to obtain a BluetoothAdapter.");
return false;
}

mState = ACTION_STATE_NONE;

Log.i(TAG, "Initialized BluetoothAdapter.");
return true;
}

/**
* Start the ConnectThread to initiate a connection to a remote device.
*
* @param device The BluetoothDevice to connect
* @param secure Socket Security type - Secure (true) , Insecure (false)
*/
public synchronized void connect(BluetoothDevice device, boolean secure) {
Log.d(TAG, "connect to : " + device);
}
}


I am calling to
bind
the
service
from an another class

public class BTTransreceiver extends Transreceiver {
private static final String TAG = "Transreceiver";

private boolean devicePaired;
private Context context;

private BluetoothDevice device;
private Intent btServiceIntent;
private BTService btService;

private final BroadcastReceiver serviceUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {

final String action = intent.getAction();

if (BTService.ACTION_STATE_NONE.equals(action)) {
Log.d(TAG, "ACTION_STATE_NONE");
// setServiceStatus(false);
} else if (BTService.ACTION_STATE_LISTEN.equals(action)) {
Log.d(TAG, "ACTION_STATE_LISTEN");
// setServiceStatus(true);
} else if (BTService.ACTION_STATE_CONNECTING.equals(action)) {
Log.d(TAG, "ACTION_STATE_CONNECTING");
// setServiceStatus(false);
} else if (BTService.ACTION_STATE_CONNECTED.equals(action)) {
Log.d(TAG, "ACTION_STATE_CONNECTED ....");
// setServiceStatus(true);
} else if (BTService.ACTION_STATE_DISCONNECTED.equals(action)) {
Log.d(TAG, "ACTION_STATE_DISCONNECTED ....");
//setServiceStatus(false);
}
}
};

private static IntentFilter makeServiceUpdateIntentFilter() {
final IntentFilter intentFilter = new IntentFilter();

intentFilter.addAction(BTService.ACTION_STATE_NONE);
intentFilter.addAction(BTService.ACTION_STATE_LISTEN);
intentFilter.addAction(BTService.ACTION_STATE_CONNECTING);
intentFilter.addAction(BTService.ACTION_STATE_CONNECTED);
intentFilter.addAction(BTService.ACTION_STATE_DISCONNECTED);

return intentFilter;
}

private final ServiceConnection serviceConnection = new ServiceConnection() {

@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
Log.d(TAG, "onServiceConnected");
btService = ((BTService.LocalBinder) service).getService();
if (btService.initialize()) {
Log.i(TAG, "Service Initialized");
btService.connect(device, true);
} else {
Log.e(TAG, "Unable to initialize the Service");
}
}

@Override
public void onServiceDisconnected(ComponentName componentName) {
btService = null;
}
};

@Override
public void init(Parameters parameters) {
Log.d(TAG, "init");
this.context = (Context) parameters.get("context");
this.device = (BluetoothDevice) parameters.get("device");
this.btServiceIntent = new Intent(context, BTService.class);

if (device != null) {
this.devicePaired = context.getApplicationContext().bindService(btServiceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}
}

@Override
public boolean connect() {
Log.d(TAG, "connect");

if (!devicePaired) {
//throw new IOException("No BLE device paired.");
return false;
} else {
try {
this.context.startService(btServiceIntent);
this.context.registerReceiver(serviceUpdateReceiver, makeServiceUpdateIntentFilter());

return true;
} catch (Exception e) {
return false;
}
}

}
}


I get the return of
bindservice
as true (
devicePaired
). But the
log
message in
onBind
function doesn't print anything.

I have also declared the
service
in the
Android Manifest


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="sit.css.testapplication">

<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<uses-feature android:name="android.hardware.nfc" android:required="true" />
<uses-feature android:name="android.hardware.bluetooth" android:required="true" />

<application
android:allowBackup="true"
android:description="@string/app_name"
android:icon="@drawable/launcher_icon"
android:label="@string/app_name"
android:logo="@drawable/launcher_icon"
android:theme="@android:style/Theme.DeviceDefault.Light"
tools:replace="android:allowBackup">

<activity
android:name="sit.css.testapplication.MainActivity"
android:label="@string/app_name"
android:exported="true"
android:screenOrientation="portrait">

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

</activity>

<service android:name=".bluetooth.BTService" android:enabled="true" android:exported="true"/>

</application>

</manifest>


I first call the init method in the transreceiver and then call the connect method from the
MainActivity
. But the binding never happens. What am I doing wrong ?

BTTransreceiver transreceiver = new BTTransreceiver();

Parameters paramTranmission = new Parameters();
paramTranmission.put("context", this);
paramTranmission.put("device", device);
paramTranmission.put("transreceiver", transreceiver);

transreceiver.init(paramTranmission);

jgm jgm
Answer

I once had this issue working with Service in android. I was then starting the Service from the Main Thread.

Then when I started the Service from an AsyncTask in android it worked. Maybe you should give it a try.