2

OK I've been trying all the solutions I have found on stack overflow, and still I cannot make it work. I want to launch an activity when the car auto radio is connected to my phone. What I tried so far:

void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
     //..... some code
    //check if the autoradio is connected 
    IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
    this.registerReceiver(mReceiver, filter1);
}
//The BroadcastReceiver that listens for bluetooth broadcasts
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(BluetoothAdapter.STATE_CONNECTED)) {
            //Device is now connected
            Toast.makeText(getApplicationContext(), "AutoradioConnected", 3).show();
            Log.e("Bluetooth", "connected");

            int state =intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,  BluetoothAdapter.ERROR);
            switch (state)
            {
            case BluetoothAdapter.STATE_CONNECTED:
            {
                new Intent(getApplicationContext(), EventDetector.class);
                myTTS=new TextToSpeech(AutoRadio.this, new TextToSpeech.OnInitListener() {

                    @Override
                    public void onInit(int status) {
                        // TODO Auto-generated method stub
                        if(status == TextToSpeech.SUCCESS){
                            int result=myTTS.setLanguage(Locale.US);
                            if(result==TextToSpeech.LANG_MISSING_DATA ||
                                    result==TextToSpeech.LANG_NOT_SUPPORTED){
                                Log.e("error", "This Language is not supported");
                            }
                            else{
                                speakWords("Auto radio connected");
                            }
                        }
                        else
                            Log.e("error", "Initilization Failed!");
                    }
                });
                break;
            }
            default:
                break;
            }
        }
    }
};

Can anybody help?

Hana
  • 239
  • 1
  • 3
  • 15
  • Please look at this link, may it help http://stackoverflow.com/questions/4715865/how-to-programmatically-tell-if-a-bluetooth-device-is-connected-android-2-2 – Lakhwinder Singh Oct 15 '15 at 12:10

0 Answers0