Niedawno miałem podobny problem, próbując podłączyć urządzenie Bluetooth do telefonu z systemem Android. Chociaż twój profil urządzenia jest inny, myślę, że rozwiązanie jest takie samo.
Najpierw trzeba utworzyć pakiet w projekcie o nazwie android.bluetooth
i umieścić następujące IBluetoothA2dp.aidl tam:
package android.bluetooth;
import android.bluetooth.BluetoothDevice;
/**
* System private API for Bluetooth A2DP service
*
* {@hide}
*/
interface IBluetoothA2dp {
boolean connectSink(in BluetoothDevice device);
boolean disconnectSink(in BluetoothDevice device);
boolean suspendSink(in BluetoothDevice device);
boolean resumeSink(in BluetoothDevice device);
BluetoothDevice[] getConnectedSinks();
BluetoothDevice[] getNonDisconnectedSinks();
int getSinkState(in BluetoothDevice device);
boolean setSinkPriority(in BluetoothDevice device, int priority);
int getSinkPriority(in BluetoothDevice device);
boolean connectSinkInternal(in BluetoothDevice device);
boolean disconnectSinkInternal(in BluetoothDevice device);
}
Następnie, aby uzyskać dostęp do tych funkcji, należy umieścić następujące klasy w projekcie:
public class BluetoothA2dpConnection {
private IBluetoothA2dp mService = null;
public BluetoothA2dpConnection() {
try {
Class<?> classServiceManager = Class.forName("android.os.ServiceManager");
Method methodGetService = classServiceManager.getMethod("getService", String.class);
IBinder binder = (IBinder) methodGetService.invoke(null, "bluetooth_a2dp");
mService = IBluetoothA2dp.Stub.asInterface(binder);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public boolean connect(BluetoothDevice device) {
if (mService == null || device == null) {
return false;
}
try {
mService.connectSink(device);
} catch (RemoteException e) {
e.printStackTrace();
return false;
}
return true;
}
public boolean disconnect(BluetoothDevice device) {
if (mService == null || device == null) {
return false;
}
try {
mService.disconnectSink(device);
} catch (RemoteException e) {
e.printStackTrace();
return false;
}
return true;
}
}
Wreszcie, aby podłączyć urządzenie A2DP, wybrać jedną Blu etoothDevice z listy sparowanych urządzeń i wyślij je jako parametr metody connect
. Pamiętaj, aby wybrać urządzenie z prawidłowym profilem, w przeciwnym razie wystąpi wyjątek.
Testowałem to rozwiązanie w telefonie z systemem Android w wersji 2.3 i działało dobrze.
Przepraszamy za błąd angielski. Mam nadzieję, że to może ci pomóc.
jaki to telefon, czy ustawienie jest obecne na linii urządzeń Nexus? – nandeesh
Telefon, którego używam to Galaxy S3 w/AT & T. Nie mam pewności, czy tam będzie, ale wyobrażam sobie, że tak. – JamesB41