User210282
User210282

Reputation: 83

Start bluetooth in android service

I am developing an application which should start bluetooth scan in an android service (without requesting the user to enable bluetooth coz the app has no activity at all) and my app has no UI or more precisely my device has no LCD/LED display. After exhaustive search & referencing the links in google and stackoverflow, I could partially find the solution and wrote the below code.

Here I start a service from a broadcast receiver, and in the service I start the bluetooth, but the bluetooth doesn't seem to turn on, I have tried enabling the bluetooth directly using the code

BluetoothAdapter.getDefaultAdapter().enable()

and also tried

Intent btIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
btIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
serviceContext.startActivity(btIntent);

But in either of the cases, bluetooth doesn't turn on.

Here is the manifest file:

android:versionCode="1"
android:versionName="1.0" android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
<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:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <receiver android:name=".StartReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>

    <service android:name=".StartService"></service>
</application>

Here is the broadcast receiver class:

public class StartReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    Intent receiverIntent = new Intent(context, StartService.class);
    context.startService(receiverIntent);
    Log.i("Autostart", "started");
}
}

And the service class:

public class StartService extends Service{

private static final String TAG = "BluetoothService";
BluetoothAdapter btAdapter;
BluetoothDevice device;

Context serviceContext;

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

@Override
public void onStart(Intent intent, int startid){
    Toast.makeText(this, "Bluetooth Service Started", Toast.LENGTH_LONG).show();
    Log.d(TAG, "OnStart");

    BluetoothAdapter.getDefaultAdapter().enable();
    //Intent btIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    //btIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    //serviceContext.startActivity(btIntent);

    registerReceiver(bcReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));

        btAdapter.startDiscovery();

 }

@Override
public void onDestroy(){
    BluetoothAdapter.getDefaultAdapter().disable();
    unregisterReceiver(bcReceiver);
    btAdapter.cancelDiscovery();
}

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

        String action = intent.getAction();
        if(BluetoothDevice.ACTION_FOUND.equals(action)){

            //Do Something
        }
    }
};
}

Also the Toast message used in the service class doesn't show up.

What could be wrong in my code for not enabling & scanning bluetooth?

Upvotes: 0

Views: 9079

Answers (1)

Dhaval Parmar
Dhaval Parmar

Reputation: 18978

you have add code in wrong method thats why it not called just use onStartCommand method. for that please check service life cycle

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "Bluetooth Service Started", Toast.LENGTH_LONG).show();

        Log.e("in onStartCommand", "onStartCommand");
        BluetoothAdapter.getDefaultAdapter().enable();
       //Intent btIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
       //btIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       //serviceContext.startActivity(btIntent);

       registerReceiver(bcReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));

       btAdapter.startDiscovery();

       return super.onStartCommand(intent, flags, startId);
    }

Upvotes: 0

Related Questions