0

UPDATE I have an Activity where I am starting a service which is running successfully. I am trying to keep bluetooth connectivity in the service. Bluetooth connectivity is running successfully through service when the application is active. But when I close the application I service is also getting killed and the bluetooth connectivity is lost. Below is my Activity code.

http://www.vogella.com/tutorials/AndroidServices/article.html

@Override
protected void onResume() {
    super.onResume();
    if (bluetoothService == null) {
        kolamDeviceDialog();
    } else {
        Log.e("MenuBluetoothService",bluetoothService.getStatus()+"");
        if (!bluetoothService.getStatus()) {
            kolamDeviceDialog();
        }
    }

}

private void kolamDeviceDialog() {
    MaterialDialog.Builder builder = new MaterialDialog.Builder(MenuActivity.this)
            .title("Select Bot")
            .items(scanTypes)
            .itemsCallback(new MaterialDialog.ListCallback() {
                @Override
                public void onSelection(MaterialDialog dialog, View itemView, int position, CharSequence text) {
                    if (position == 0) {
                        KolamDevice kolamDevice = new KolamDevice();
                        Intent intent = new Intent(MenuActivity.this, BluetoothService.class);
                        intent.putExtra("Address", kolamDevice.getBigBotAddress());
                        intent.putExtra("Name", kolamDevice.getBigBotName());
                        startService(intent);
                        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
                    } else {
                        KolamDevice kolamDevice = new KolamDevice();
                        Intent intent = new Intent(MenuActivity.this, BluetoothService.class);
                        intent.putExtra("Address", kolamDevice.getSmallBotAddress());
                        intent.putExtra("Name", kolamDevice.getSmallBotName());
                        startService(intent);
                        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
                    }
                }
            });
    builder.show();
}

@Override
protected void onPause() {
    super.onPause();
   // unbindService(mConnection);
}

private ServiceConnection mConnection = new ServiceConnection() {

    public void onServiceConnected(ComponentName className,
                                   IBinder binder) {
        BluetoothService.MyBinder b = (BluetoothService.MyBinder) binder;
        bluetoothService = b.getService();
        Toast.makeText(MenuActivity.this, "Connected", Toast.LENGTH_SHORT)
                .show();
    }

    public void onServiceDisconnected(ComponentName className) {
        bluetoothService = null;
    }
};

This is my Manifest file.

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
    android:name=".AppController"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity" />
    <activity android:name=".ARCameraActivity" />
    <activity android:name=".RegistrationActivity" />
    <activity android:name=".LoginActivity" />
    <activity android:name=".SplashActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MenuActivity" />
    <activity android:name=".ScanAndDrawActivity" />
    <activity android:name=".GalleryActivity" />
    <activity android:name=".PdfKolamActivity" />

    <service
        android:enabled="true"
        android:name=".BluetoothService">
        <intent-filter>
            <action android:name="com.ignite.a01hw909350.kolamdemo.BluetoothService"/>
        </intent-filter>
    </service>

    <receiver
        android:name=".MyScheduleReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <receiver
        android:name=".MyStartServiceReceiver"/>
</application>

At last this is my Service class.

public class BluetoothService extends Service {

private final IBinder mBinder = new MyBinder();
private SmoothBluetooth smoothBluetooth;
private static Device device;
private boolean isConnected = false;
String address, name;
private List<Integer> mBuffer = new ArrayList<>();
private List<String> mResponseBuffer = new ArrayList<>();

public BluetoothService() {
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.e("BluetoothService", "Started");
    Bundle extras = intent.getExtras();
    if (extras != null) {
        address = extras.getString("Address");
        name = extras.getString("Name");
    }
    smoothBluetooth = new SmoothBluetooth(this);
    smoothBluetooth.setListener(mListener);

    if (smoothBluetooth.isBluetoothEnabled()) {
        if (!smoothBluetooth.isConnected()) {
            device = new Device(name, address, true);
            smoothBluetooth.tryConnection();
        }
    }
    return Service.START_NOT_STICKY;
}


@Override
public IBinder onBind(Intent arg0) {
    return mBinder;
}

public class MyBinder extends Binder {
    BluetoothService getService() {
        return BluetoothService.this;
    }
}

public boolean getStatus() {
    return isConnected;
}

This is the Receiver where I explicitly call the startService().

public class MyStartServiceReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Intent service = new Intent(context, BluetoothService.class);
    context.startService(service);
}

}

Please tell me whats wrong in my code.

  • what does `bindService` return? – pskink Mar 23 '17 at 14:15
  • In the onServiceConnected() method its saying "connected" but the bind is not happening –  Mar 23 '17 at 14:18
  • so if `onServiceConnected` is called your service is bound to your activity... what do you mean by `"but the bind is not happening"`? – pskink Mar 23 '17 at 14:19
  • I have kept Log inside onStartCommand in the Service. But its not showing the Log. What to do? –  Mar 23 '17 at 14:24
  • `onStartCommand` is used by "started" Services (when you call `startService()` method) - if you want "bound" Services then read https://developer.android.com/guide/components/bound-services.html – pskink Mar 23 '17 at 14:27
  • Please see my updated question. I am able to create the service but the onStartCommand method is not getting called. Whats wrong ? –  Mar 23 '17 at 14:39
  • because you didn't call `startService`? i already said that `"onStartCommand is used by "started" Services (when you call startService() method)"` – pskink Mar 23 '17 at 14:40
  • How can i start the "onStartCommand" after onCreate(). Any solutions ? –  Mar 23 '17 at 14:46
  • did you read the official documentation on `Service#onStartCommand`? if so, what is that method used for? – pskink Mar 23 '17 at 14:48
  • See i created a receiver where i explicitly called the startService(). Below is the link which I used to create the service. http://www.vogella.com/tutorials/AndroidServices/article.html I am not able to figure out where to call startService(). –  Mar 23 '17 at 14:50
  • i dont see any `startService()` method call – pskink Mar 23 '17 at 14:50
  • please see my updated question. –  Mar 23 '17 at 14:53
  • what does `context.startService()` return? – pskink Mar 23 '17 at 14:55
  • I am able to successfully run the service and the bluetooth connectivity is happening through service. But when I am closing my application the service is also getting stopped coz I am losing bluetooth connectivity as I am closing my application. –  Mar 24 '17 at 03:09

1 Answers1

0

Don'n bind your service to your activity,instead use started service. https://developer.android.com/guide/components/services.html

ak0692
  • 233
  • 2
  • 13
  • I need to bind so that I can communication back and forth from activity to service and vice versa. –  Mar 25 '17 at 04:40
  • Then create a service which is started and bound at the same time. – ak0692 Mar 27 '17 at 06:22
  • Yeah did that. I got it working by adding `startForeground()` anyway thanks for the help. –  Mar 27 '17 at 06:26