我想模拟通过“设置”->“无线”->“蓝牙”并以编程方式连接配对的蓝牙耳机的操作。我在 Stackoverflow 和 Google 上做了一些搜索,都表明在 API 级别 11 之前没有可用的解决方案。但是,我有兴趣通过查看 Android 蓝牙实现的源代码来解决这个问题。问题是我不知道应该查看哪个特定源代码。有什么建议么?非常感谢。
最佳答案
经过几天的努力,我现在成功了,干杯:)
- 将 android.bluetooth.IBluetoothA2dp.aidl 添加到您应用的/src 目录中;
在您的代码中添加此私有(private)方法:
private IBluetoothA2dp getIBluetoothA2dp() { IBluetoothA2dp ibta = null; try { Class c2 = Class.forName("android.os.ServiceManager"); Method m2 = c2.getDeclaredMethod("getService", String.class); IBinder b = (IBinder) m2.invoke(null, "bluetooth_a2dp"); Log.d("Felix", "Test2: " + b.getInterfaceDescriptor()); Class c3 = Class.forName("android.bluetooth.IBluetoothA2dp"); Class[] s2 = c3.getDeclaredClasses(); Class c = s2[0]; // printMethods(c); Method m = c.getDeclaredMethod("asInterface", IBinder.class); m.setAccessible(true); ibta = (IBluetoothA2dp) m.invoke(null, b); } catch (Exception e) { Log.e("flowlab", "Erroraco!!! " + e.getMessage()); }
用这个测试它:
private void testBluetoothA2dp(BluetoothDevice device) { // TODO Auto-generated method stub // TODO Auto-generated method stub IBluetoothA2dp ibta = getIBluetoothA2dp(); try { Log.d("Felix", "Here: " + ibta.getSinkPriority(device)); ibta.connectSink(device); } catch (RemoteException e) { // * TODO Auto-generated catch block e.printStackTrace(); }
我无法提供这些代码的引用,因为我花了很多时间谷歌搜索、检查 stackoverflow 和审查 Android 源代码,但未能跟踪源代码。非常感谢 Stackoverflow 的你们 :)
关于安卓连接配对的蓝牙耳机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6161870/