如何以编程方式在Android中启用或禁用蓝牙配置文件?

5
我有一个需求,需要列出通过蓝牙与我的手机连接的设备对列表。在某些时候,我需要禁用/启用共享媒体或联系人详细信息到我的配对设备。 我的要求是在android中以编程方式实现此功能。我已经搜索过了,但是没有找到任何解决方案。 请告诉我这是否可能?

2
你找到解决方案了吗? - peshkira
2个回答

1

只需尝试启用或禁用蓝牙即可。

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    

            if (mBluetoothAdapter.isEnabled()) {
                mBluetoothAdapter.disable(); 
            }else {
                mBluetoothAdapter.enable(); 
            }

但我无法完全禁用蓝牙。 - Vishnu Cyruz
在我的案例中,mBluetoothAdapter.disable(); 可以完美地禁用蓝牙。你能展示一下你的代码吗? - Sanju
你添加了权限吗?<uses-permission android:name="android.permission.BLUETOOTH" android:required="false" /> - Sanju
根据您提供的代码,蓝牙可以被禁用。但这不是我的要求。我必须在不禁用蓝牙的情况下禁用蓝牙配置文件。希望现在您理解我的意思了。 - Vishnu Cyruz
@VishnuCyruz 你有什么解决方案吗? - Android Developer

1
尝试获取默认的蓝牙适配器。
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    

启用

 bluetoothAdapter.enable(); 

Disable

bluetoothAdapter.disable(); 

检查状态
bluetoothAdapter.isEnabled();

感谢您提供的代码。但那不是我需要的。我需要禁用蓝牙配置文件,如媒体、联系人详细信息,而不禁用蓝牙。 - Vishnu Cyruz
不,我只想禁用蓝牙配置文件。蓝牙连接和配对设备都需要保留。 - Vishnu Cyruz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接