gpt4 book ai didi

java - 以编程方式连接到配对的蓝牙扬声器并播放音频

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:33:14 28 4
gpt4 key购买 nike

在我们的应用中,我想使用 Android v4.2 或更高版本连接到之前配对的 A2DP 蓝牙扬声器并直接向其播放音频。

我可以使用此代码成功创建 A2DP 配置文件对象以启动该过程:


/* Manifest permissions */
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

// Get the default adapter
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

// Establish connection to the proxy.
mBluetoothAdapter.getProfileProxy(this, mProfileListener, BluetoothProfile.A2DP)

和以下监听器响应连接:

private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {

if (profile == BluetoothProfile.A2DP) {

mBluetoothSpeaker = (BluetoothA2dp) proxy;

// no devices are connected
List<BluetoothDevice> connectedDevices = mBluetoothSpeaker.getConnectedDevices();

//the one paired (and disconnected) speaker is returned here
int[] statesToCheck = {BluetoothA2dp.STATE_DISCONNECTED};
List<BluetoothDevice> disconnectedDevices = mBluetoothSpeaker.getDevicesMatchingConnectionStates(statesToCheck);



BluetoothDevice btSpeaker = disconnectedDevices.get(0);

//WHAT NOW?

}
}
public void onServiceDisconnected(int profile) {
if (profile == BluetoothProfile.A2DP) {
mBluetoothSpeaker = null;
}
}
};

我只是有点不知道现在该做什么,连接设备,并将音频输出定向到它。我试过连接到设备,as detailed in the Android docs , 使用以下代码,但最后的 BluetoothSpeaker.getConnectedDevices()调用未返回连接的设备。

    BluetoothSocket tmp = null;
UUID MY_UUID = UUID.fromString("00001108-0000-1000-8000-00805f9b34fb");
try {
tmp = btSpeaker.createInsecureRfcommSocketToServiceRecord(MY_UUID);

} catch (IOException e1) {
// TODO Auto-generated catch block
Log.d("createRfcommSocketToServiceRecord ERROR", e1.getMessage());
}
mmSocket = tmp;

try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
Log.d("connectException", connectException.getMessage());
mmSocket.close();
} catch (IOException closeException) { }
return;
}

connectedDevices = mBluetoothSpeaker.getConnectedDevices();

不过,代码似乎确实以某种方式连接到设备,因为当我停止执行时,蓝牙扬声器会通知它已准备好配对(当它与音频源断开连接时总是如此)。

旧版本的 BluetoothA2dp似乎有一个 connect(BluetoothDevice device)方法,但现在已被删除(从 4.2 开始),我正在努力寻找任何清晰的示例,说明如何以编程方式连接到 A2DP 设备,并将音频输出定向到它。我们将不胜感激地收到有关如何处理其中任何一个的任何帮助。

任何有关如何处理此问题的建议都将不胜感激。

最佳答案

这个对我有用。接收到BluetoothA2dp.STATE_CONNECTED后,即可正常播放音乐。

public class A2DPActivity extends Activity {

protected static final String TAG = "ZS-A2dp";

Button mBtPlay;

BluetoothAdapter mBtAdapter;
BluetoothA2dp mA2dpService;

AudioManager mAudioManager;
MediaPlayer mPlayer;

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

};

boolean mIsA2dpReady = false;
void setIsA2dpReady(boolean ready) {
mIsA2dpReady = ready;
Toast.makeText(this, "A2DP ready ? " + (ready ? "true" : "false"), Toast.LENGTH_SHORT).show();
}

private ServiceListener mA2dpListener = new ServiceListener() {

@Override
public void onServiceConnected(int profile, BluetoothProfile a2dp) {
Log.d(TAG, "a2dp service connected. profile = " + profile);
if (profile == BluetoothProfile.A2DP) {
mA2dpService = (BluetoothA2dp) a2dp;
if (mAudioManager.isBluetoothA2dpOn()) {
setIsA2dpReady(true);
playMusic();
} else {
Log.d(TAG, "bluetooth a2dp is not on while service connected");
}
}
}

@Override
public void onServiceDisconnected(int profile) {
setIsA2dpReady(false);
}

};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
setContentView(ll);

mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
registerReceiver(mReceiver, new IntentFilter(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED));
registerReceiver(mReceiver, new IntentFilter(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED));

mBtAdapter = BluetoothAdapter.getDefaultAdapter();
mBtAdapter.getProfileProxy(this, mA2dpListener , BluetoothProfile.A2DP);

}

@Override
protected void onDestroy() {
mBtAdapter.closeProfileProxy(BluetoothProfile.A2DP, mA2dpService);
releaseMediaPlayer();
unregisterReceiver(mReceiver);
super.onDestroy();
}

@Override
protected void onPause() {
releaseMediaPlayer();
super.onPause();
}

private void releaseMediaPlayer() {
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}

private void playMusic() {
mPlayer = new MediaPlayer();
AssetManager assetManager = this.getAssets();
AssetFileDescriptor fd;
try {
fd = assetManager.openFd("Radioactive.mp3");
Log.d(TAG, "fd = " + fd);
mPlayer.setDataSource(fd.getFileDescriptor());
mPlayer.prepare();
Log.d(TAG, "start play music");
mPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}

关于java - 以编程方式连接到配对的蓝牙扬声器并播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22226552/

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