1

Я хочу использовать BluetoothHeadsetClient. для использования этого скрытого кода SDK я использую отражение.BluetoothHeadsetClient: не удалось связать с Bluetooth-гарнитурой Клиентское обслуживание с намерением

Я хочу назвать

mBluetoothAdapter.getProfileProxy(this, mHeadsetProfileListener, BluetoothProfile.HEADSET_CLIENT); 

но BluetoothProfile.HEADSET_CLIENT скрыт в BluetoothProfile.java. для того, чтобы решить эту проблему, я выполнить следующий код

значение
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 (прокси) == 16 -> BluetoothProfile.HEADSET_CLIENT как я хотел. но когда я бегу

mBluetoothAdapter.getProfileProxy(getApplicationContext(), mHeadsetProfileListener, field.getInt(proxy)); 

я вижу следующее сообщение об ошибке

E/BluetoothHeadsetClient﹕ Could not bind to Bluetooth Headset Client Service with Intent { act=android.bluetooth.IBluetoothHeadsetClient } 

вы знаете, как решить этот тип ошибки?

Спасибо!

ответ

0

Я нашел API от robolectric, что делает скрытые части API видимыми. Вы можете использовать его с областью действия, указанной в файле build.gradle.

provided 'org.robolectric:android-all:5.0.0_r2-robolectric-1' 

К сожалению, я тоже не в состоянии получить рабочий прокси-сервер, но отладка в BluetoothHeadsetClient показывает, что doBind IST успешно называется. В моем случае onServiceConnected из BluetoothProfile.ServiceListener никогда не вызывается, поэтому «рабочий» прокси никогда не возвращается в мою деятельность.

0

Возможно, проблема вызывает mBluetoothAdapter.getProfileProxy(getApplicationContext(), mHeadsetProfileListener, field.getInt(proxy)); изнутри BluetoothProfile.ServiceListener.onServiceConnected.

следующие работы:

try { 
    final int Bluetooth_Profile_HEADSET_CLIENT = 
     (int) BluetoothProfile.class.getField("HEADSET_CLIENT").get(null); 

    BluetoothProfile.ServiceListener mProfileListener = 
    new BluetoothProfile.ServiceListener() { 
     public void onServiceConnected(int profile, BluetoothProfile proxy) { 
      if (profile == Bluetooth_Profile_HEADSET_CLIENT) { 
       //proxy is BluetoothHeadsetClient 
      } 
     } 

     public void onServiceDisconnected(int profile) { 

     } 
}; 

    BluetoothAdapter 
      .getDefaultAdapter() 
      .getProfileProxy(this, mProfileListener, Bluetooth_Profile_HEADSET_CLIENT); 
}catch(Exception ex) {} 
Смежные вопросы