I need to connect defined BT device by simple pressing the button.
The requirement is user shouldn't receive any notification dialogs as in case of using standard socket methods.
In my project I used this solution.
Code was next:
/**
* Return system service to work with A2DP
*
* @return bluetooth interface
*/
private static IBluetoothA2dp getIBluetoothA2dp() {
IBluetoothA2dp ibta = null;
try {
final Class serviceManager = Class.forName("android.os.ServiceManager");
final Method getService = serviceManager.getDeclaredMethod("getService", String.class);
final IBinder iBinder = (IBinder) getService.invoke(null, "bluetooth_a2dp");
final Class iBluetoothA2dp = Class.forName("android.bluetooth.IBluetoothA2dp");
final Class[] declaredClasses = iBluetoothA2dp.getDeclaredClasses();
final Class c = declaredClasses[0];
final Method asInterface = c.getDeclaredMethod("asInterface", IBinder.class);
asInterface.setAccessible(true);
ibta = (IBluetoothA2dp) asInterface.invoke(null, iBinder);
} catch (final Exception e) {
Log.e("Error " + e.getMessage());
}
return ibta;
}
It worked well until I've launched my app on Android 4.2. Now I'm unable to get IBluetoothA2dp interface because getService() method doesn't return me an IBinder with "bluetooth_a2dp" key.
Can someone help me?
Thanks in advance!