【发布时间】:2022-01-03 15:30:32
【问题描述】:
我使用 a2dp 配置文件连接到蓝牙设备。该部分一切正常,但是当需要断开所有预期代码时,可以访问但设备没有断开连接,我不确定我做错了什么。这是我的断开连接代码:
public void disconnectFromOther(BluetoothDevice device) {
BluetoothProfile.ServiceListener serviceListener =
new BluetoothProfile.ServiceListener() {
@Override
public void onServiceDisconnected(int profile) {
}
public void onServiceConnected(int profile, BluetoothProfile proxy) {
Method disconnect;
try {
disconnect = a2dp.getClass()
.getMethod("disconnect", BluetoothDevice.class);
disconnect.setAccessible(true);
disconnect.invoke(proxy, device);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
BluetoothAdapter.getDefaultAdapter().closeProfileProxy(profile, proxy);
}
};
BluetoothAdapter.getDefaultAdapter().getProfileProxy(context, serviceListener, BluetoothProfile.A2DP);
}
编辑:我现在也尝试了 BluetoothGatt,但仍然无法断开我的设备。
【问题讨论】:
标签: java android bluetooth android-bluetooth a2dp