0

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?

User210282
  • 83
  • 4
  • 13

1 Answers1

0

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);
    }
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
  • The Bluetooth still doesn't turn on after implementing onStartCommand. I don't see the service in task manager. – User210282 Apr 02 '13 at 13:25
  • that means onStartCommand not called or service not started.. so check your StartReceiver is working or not?? – Dhaval Parmar Apr 02 '13 at 13:47
  • StartReceiver doesn't seem to be started, but is there anything wrong with my receiver class? Also a Logcat entry says "ActivityManager(187): Force stopping package PACKAGE_NAME uid=10062". Why should such a thing happen? Googling the error says to set the permissions correctly. Aren't the permissions right in my manifest? – User210282 Apr 02 '13 at 14:02
  • i have tryed this BOOT_COMPLETED it not worked well.. insted of that use another reciver link media scenner, in comming call.... etc.. – Dhaval Parmar Apr 03 '13 at 04:51
  • Initially I developed the app using the same link – User210282 Apr 03 '13 at 13:44
  • @User210282: If there service is not getting invoked,can you try stating the full name of your service in the manifest.For eg : '' and then try. – Basher51 Jul 25 '14 at 12:29