What is the correct way to retrieve the name of the currently selected media audio output on Android?
CodePudding user response:
Spotify has its own API. The endpoint you need to use to get the device name of the current device is Get Playback State (demo here).
To get the names of all available devices, you can use the Get Available Devices endpoint (demo here).
CodePudding user response:
I didn't try this with wired devices but I did this for Bluetooth devices like this-
BluetoothManager btManager = (BluetoothManager)
getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapter.cancelDiscovery();
bluetoothAdapter.getProfileProxy(this, listener, BluetoothProfile.HEADSET);
public final BluetoothProfile.ServiceListener listener = new BluetoothProfile.ServiceListener() {
@Override
public void onServiceConnected(int i, final BluetoothProfile bluetoothProfile) {
final TextView device = (TextView) findViewById(R.id.device);
List<BluetoothDevice> b = bluetoothProfile.getConnectedDevices();
StringBuilder stringBuilder = new StringBuilder();
for(BluetoothDevice getConnectedDevice : b){
stringBuilder.append(getConnectedDevice.getName());
}
device.setText(stringBuilder);
}
@Override
public void onServiceDisconnected(int i) {
final TextView device = (TextView) findViewById(R.id.device);
device.setText(String.valueOf(i));
}
};
You can try this for Bluetooth devices, I hope it helps.