[英]Check if headset is connected in Android application. Bluetooth and wired
I am developing a simple application and I need to track headset connection.我正在开发一个简单的应用程序,我需要跟踪耳机的连接。 Here is the part of the code.这是代码的一部分。
In onCreate method there is a broadcastReceiver.在onCreate方法中,有一个broadcastReceiver。 It works perfect with wired headphones.与有线耳机完美搭配。
Then I added an if-else condition for a bluetooth headset.然后,我为蓝牙耳机添加了if-else条件。 If the headset is already connected when I start the app it shows that it is connected.如果在启动应用程序时已经连接了耳机,则表明耳机已连接。 It's ok.没关系。 But when I turn off the bluetooth and plug in wired headphones, I receive a Toast from a bluetooth if-else: bl headset disconn.但是,当我关闭蓝牙并插入有线耳机时,会收到来自蓝牙if-else的Toast:bl耳机disconn。 Why it does not catch the Headset Plug In Action?为什么它没有赶上耳机插件行动?
public class MainActivity extends AppCompatActivity {
BroadcastReceiver broadcastReceiver;
boolean Microphone_Plugged_in = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
int c;
if (Intent.ACTION_HEADSET_PLUG.equals(action)) {
c = intent.getIntExtra("state", -1);
if (c == 0) {
Microphone_Plugged_in = false;
Toast.makeText(getApplicationContext(), "headset is not plugged in", Toast.LENGTH_LONG).show();
}
if (c == 1) {
Microphone_Plugged_in = true;
Toast.makeText(getApplicationContext(), "headset is plugged in", Toast.LENGTH_LONG).show();
}
}
BluetoothAdapter BA;
BA = BluetoothAdapter.getDefaultAdapter();
if (BA != null && BluetoothProfile.STATE_CONNECTED == BA.getProfileConnectionState(BluetoothProfile.HEADSET))
{
Microphone_Plugged_in = true;
Toast.makeText(getApplicationContext(), "bl headset connected", Toast.LENGTH_LONG).show();
}
else {
Microphone_Plugged_in = false;
Toast.makeText(getApplicationContext(), "bl headset disconn", Toast.LENGTH_LONG).show();
}
}
};
IntentFilter receiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
registerReceiver(broadcastReceiver, receiverFilter);
Thank you谢谢
I am developing a simple application and I need to track headset connection.我正在开发一个简单的应用程序,我需要跟踪耳机的连接。 Here is the part of the code.这是代码的一部分。
In onCreate method there is a broadcastReceiver.在onCreate方法中,有一个broadcastReceiver。 It works perfect with wired headphones.与有线耳机完美搭配。
Then I added an if-else condition for a bluetooth headset.然后,我为蓝牙耳机添加了if-else条件。 If the headset is already connected when I start the app it shows that it is connected.如果在启动应用程序时已经连接了耳机,则表明耳机已连接。 It's ok.没关系。 But when I turn off the bluetooth and plug in wired headphones, I receive a Toast from a bluetooth if-else: bl headset disconn.但是,当我关闭蓝牙并插入有线耳机时,会收到来自蓝牙if-else的Toast:bl耳机disconn。 Why it does not catch the Headset Plug In Action?为什么它没有赶上耳机插件行动?
public class MainActivity extends AppCompatActivity {
BroadcastReceiver broadcastReceiver;
boolean Microphone_Plugged_in = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
int c;
if (Intent.ACTION_HEADSET_PLUG.equals(action)) {
c = intent.getIntExtra("state", -1);
if (c == 0) {
Microphone_Plugged_in = false;
Toast.makeText(getApplicationContext(), "headset is not plugged in", Toast.LENGTH_LONG).show();
}
if (c == 1) {
Microphone_Plugged_in = true;
Toast.makeText(getApplicationContext(), "headset is plugged in", Toast.LENGTH_LONG).show();
}
}
BluetoothAdapter BA;
BA = BluetoothAdapter.getDefaultAdapter();
if (BA != null && BluetoothProfile.STATE_CONNECTED == BA.getProfileConnectionState(BluetoothProfile.HEADSET))
{
Microphone_Plugged_in = true;
Toast.makeText(getApplicationContext(), "bl headset connected", Toast.LENGTH_LONG).show();
}
else {
Microphone_Plugged_in = false;
Toast.makeText(getApplicationContext(), "bl headset disconn", Toast.LENGTH_LONG).show();
}
}
};
IntentFilter receiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
registerReceiver(broadcastReceiver, receiverFilter);
Thank you谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.