私たちのアプリでは、以前にペアリングされた A2DP Bluetooth スピーカーに接続し、Android v4.2 以降を使用して直接オーディオを再生したいと考えています。
このコードを使用して 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;
}
}
};
今何をすべきか、デバイスを接続し、オーディオ出力をデバイスに向けるかについて、少し迷っています。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();
コードは何らかの方法でデバイスに接続しているように見えますが、実行を停止すると、Bluetooth スピーカーはペアリングの準備ができたことを通知します (オーディオ ソースから切断するときは常にそうです)。
. BluetoothA2dp
_ connect(BluetoothDevice device)
_ どちらにアプローチするかについての助けは、ありがたく受け取られます。
これにアプローチする方法についてのアドバイスは大歓迎です。