我需要通过简单地按下按钮来连接定义的BT设备
要求是用户不应该像使用标准套接字方法那样接收任何通知对话框
在我的项目中,我使用了this solution。
代码是下一个:
/**
* 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 :(得分: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地址。现在它需要一个蓝牙设备。
请记住,使用这些隐藏的界面是有风险的,因为它们可以并且经常使用新的Android版本进行更改。我现在已经好几次了。你真的需要保持领先。
答案 1 :(得分:0)
如果某人需要与自动停机相关的答案,可以在这里查看我的答案。 https://stackoverflow.com/a/30362554/3920157