Android 检查蓝牙设备是否已连接
好吧,我一直在尝试我在堆栈溢出上找到的所有解决方案,但我仍然无法让它工作。 我想在车载收音机连接到手机时启动一项活动。 到目前为止,我尝试的是:Android 检查蓝牙设备是否已连接,android,bluetooth,Android,Bluetooth,好吧,我一直在尝试我在堆栈溢出上找到的所有解决方案,但我仍然无法让它工作。 我想在车载收音机连接到手机时启动一项活动。 到目前为止,我尝试的是: void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); //..... some code //check if the autoradi
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;
}
}
}
};
有人能帮忙吗 请查看此链接,是否有帮助