gpt4 book ai didi

java - 检测连接的蓝牙媒体设备

转载 作者:行者123 更新时间:2023-11-30 00:13:32 26 4
gpt4 key购买 nike

我有一个 BrodcastReciever 接收耳机插入事件和蓝牙连接。这很好用,但问题是当我连接时,例如,SmartWatch 类似于我连接蓝牙耳机或条形音箱。我该怎么做才能过滤可以播放其他音乐的媒体设备?

广播接收者

@Override
public void onReceive(final Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_MEDIA_BUTTON.equals(action)) {
if (Settings.PROCESS_PRESSING){
KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (isAvailable) {
if (mTimer != null) mTimer.cancel();
if (myTimer != null) myTimer.cancel();
mTimer = new Timer();
myTimer = new MyTimerTask();
if (KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE == event.getKeyCode()) {
listener.playButtonPressed();
} else if (KeyEvent.KEYCODE_MEDIA_PLAY == event.getKeyCode()) {
listener.playButtonPressed();
} else if (KeyEvent.KEYCODE_MEDIA_PAUSE == event.getKeyCode()) {
listener.playButtonPressed();
} else if (KeyEvent.KEYCODE_MEDIA_STOP == event.getKeyCode()) {
listener.playButtonPressed();
} else if (KeyEvent.KEYCODE_MEDIA_NEXT == event.getKeyCode()) {
listener.nextTrackButtonPressed();
} else if (KeyEvent.KEYCODE_MEDIA_PREVIOUS == event.getKeyCode()) {
listener.previousTrackButtonPressed();
}

isAvailable = false;
mTimer.schedule(myTimer, 500);
}
}
} else if (action.equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
switch (state) {
case 0:

break;
case 1:
if (Settings.START_HEADSET) listener.headsetPlugged();
break;

}
}
else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
if (Settings.START_BLUETOOTH)
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(6000);
Handler mainHandler = new Handler(context.getMainLooper());
Runnable myRunnable = new Runnable() {
@Override
public void run() {
listener.headsetPlugged();
}
};
mainHandler.post(myRunnable);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();

}
else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {}

}

主要 Activity

 headsetFilter = new IntentFilter();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
headsetFilter.addAction(AudioManager.ACTION_HEADSET_PLUG);
}
headsetFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
headsetFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
headsetFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);

getApplicationContext().registerReceiver(control_reciever, headsetFilter);

最佳答案

接收 BluetoothA2dp.STATE_CONNECTED Intent 然后处理

 BroadcastReceiver mReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context ctx, Intent intent) {
String action = intent.getAction();
Log.d(TAG, "receive intent for action : " + action);
if (action.equals(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED)) {
int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_DISCONNECTED);
if (state == BluetoothA2dp.STATE_CONNECTED) {
setIsA2dpReady(true);
playMusic();
} else if (state == BluetoothA2dp.STATE_DISCONNECTED) {
setIsA2dpReady(false);
}
} else if (action.equals(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED)) {
int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_NOT_PLAYING);
if (state == BluetoothA2dp.STATE_PLAYING) {
Log.d(TAG, "A2DP start playing");
Toast.makeText(A2DPActivity.this, "A2dp is playing", Toast.LENGTH_SHORT).show();
} else {
Log.d(TAG, "A2DP stop playing");
Toast.makeText(A2DPActivity.this, "A2dp is stopped", Toast.LENGTH_SHORT).show();
}
}
}

};

关于java - 检测连接的蓝牙媒体设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47737075/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com