I want to use the BluetoothHeadsetClient. in order to use this hidden SDK code I use reflection.
I want to call
mBluetoothAdapter.getProfileProxy(this, mHeadsetProfileListener, BluetoothProfile.HEADSET_CLIENT);
but BluetoothProfile.HEADSET_CLIENT is hidden in BluetoothProfile.java. in order to solve it I run the following code
protected BluetoothProfile.ServiceListener mHeadsetProfileListener = new BluetoothProfile.ServiceListener()
{
@Override
public void onServiceDisconnected(int profile) {
}
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy)
{
mlisten = mHeadsetProfileListener;
mHeadSetCleintObj = proxy;
// reflection for getting the HEADSET_CLIENT type
try {
Class classRef = Class.forName("android.bluetooth.BluetoothProfile");
Field field = classRef.getField("HEADSET_CLIENT");
if(profile == BluetoothProfile.HEADSET ) {
mBluetoothAdapter.getProfileProxy(getApplicationContext(), mHeadsetProfileListener, field.getInt(proxy));
}
}
catch (Exception e) {
e.printStackTrace();
}
mBluetoothHeadset.getConnectedDevices();
}
};
@Override
public void onDestroy() {
unregisterReceiver(mPairReceiver);
super.onDestroy();
}
field.getInt(proxy) value == 16 --> BluetoothProfile.HEADSET_CLIENT as I wanted. but when I run the
mBluetoothAdapter.getProfileProxy(getApplicationContext(), mHeadsetProfileListener, field.getInt(proxy));
I see the following error
E/BluetoothHeadsetClient﹕ Could not bind to Bluetooth Headset Client Service with Intent { act=android.bluetooth.IBluetoothHeadsetClient }
do you know how to solve this type of error?
Thanks!
Aucun commentaire:
Enregistrer un commentaire