自定义蓝牙消息 mBluetoothAdapter

Custom Bluetooth message mBluetoothAdapter

使用以下代码,您可以弹出 window 蓝牙访问请求:

if (!mBluetoothAdapter.isEnabled()) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}

代码输出如下图:

是否可以更改消息(不是“一个应用想要打开蓝牙”,而是“嘿,你能打开蓝牙吗?”之类的消息)

不,这是内置 Android 功能,因此您必须制作一个自定义对话框,以您想要的方式询问问题。

然后您将根据用户的选择以编程方式启用或禁用蓝牙。

您应该使用自己的消息创建自己的对话,然后调用

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.enable();

并且不要忘记在清单中声明 BLUETOOTH_ADMIN 权限。

无论如何,this operation is discouraged...enable() 方法仅适用于包含用于更改系统设置的用户界面的应用程序,例如 "power manager" 应用程序。