检查蓝牙设备是否已连接

时间:2015-10-15 12:01:38

标签: android bluetooth

好的,我一直在尝试我在堆栈溢出时找到的所有解决方案,但仍然无法使其正常工作。 我想在汽车自动收音机连接到手机时启动活动。 到目前为止我尝试了什么:

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;
            }
        }
    }
};

有人可以帮忙吗?

0 个答案:

没有答案