2013-02-05 65 views
3


我需要通過簡單按下按鈕連接定義的BT設備。
要求是用戶不應該收到任何通知對話框,因爲在使用標準套接字方法的情況下。
在我的項目中我使用了this solution。 代碼爲下一:如何在Android 4.2上使用反射連接配對的藍牙A2dp設備?

/** 
* 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; 
} 

它運作良好,直到我開始了我的應用程序在Android 4.2。現在我無法獲得IBluetoothA2dp接口,因爲getService()方法不會返回帶有「bluetooth_a2dp」鍵的IBinder。

有人可以幫我嗎?

在此先感謝!

+0

你不需要爲此反思,是嗎?這是一種黑客攻擊,並且可能不可靠。 –

+0

其實我需要反思。我有要求避免任何對話和通知,這就是爲什麼我使用這種黑客。是的,這絕對是不可靠的,但直到谷歌改變英國電信協議棧之前,它在2.3到4.1之間沒有任何問題。 – dmitriyzaitsev

+0

你在說什麼對話框? –

回答

1

終於搞定了這個4.2。在這裏看到的細節:http://code.google.com/p/a2dp-connect2/

這是從4.1和以前的完全不同。

首先呼叫連接到該接口是這樣的:

public static void getIBluetoothA2dp(Context context) { 

    Intent i = new Intent(IBluetoothA2dp.class.getName()); 

    if (context.bindService(i, mConnection, Context.BIND_AUTO_CREATE)) { 

    } else { 
     // Log.e(TAG, "Could not bind to Bluetooth A2DP Service"); 
    } 

} 

當界面返回它會調用回本:以上

public static ServiceConnection mConnection = new ServiceConnection() { 

    @Override 
    public void onServiceConnected(ComponentName name, IBinder service) { 

     ibta2 = IBluetoothA2dp.Stub.asInterface(service); 
    } 

    @Override 
    public void onServiceDisconnected(ComponentName name) { 
     // TODO Auto-generated method stub 

    } 

}; 

ibta2是IBluetoothA2dp接口。

在相關說明中,IBluetooth接口也發生了變化。我用它來獲取設備別名(用戶可以編輯的名稱)。這個getRemoteAlias()函數需要之前的mac地址。現在需要一個BluetoothDevice。

請記住,使用這些隱藏的界面是有風險的,因爲他們可以並經常使用新的Android版本進行更改。我現在已經好幾次了。你真的需要留在它之上。

相關問題