微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 BluetoothManager 启用蓝牙不起作用

如何解决使用 BluetoothManager 启用蓝牙不起作用

我正在尝试在 Android 手机上激活 BLE (Bluetooth Low Energy)(目前在 Android Studio 上使用模拟器)。我知道我不能在模拟器上使用蓝牙,但即使没有它我仍然有一些麻烦,这里是代码

我的主要问题是 bluetoothAdapter.isEnabled() 的位置。我已经为它添加manifest.permission.BLUetoOTH,但即使这样做了,我似乎也无法理解我可能在哪里犯了错误

    // Initializes Bluetooth adapter.
       @SuppressLint("NewApi")

          
   public final BluetoothManager bluetoothManager = getSystemService(BluetoothManager.class);

BluetoothAdapter bluetoothAdapter = null;
if  (bluetoothManager != null)
{
    bluetoothAdapter = bluetoothManager.getAdapter();
}

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

解决方法

我真的不明白你的问题,正如你没有提到的,究竟是什么不起作用,但我希望蓝牙没有打开。

以下是 Google 的官方 Android 指南: https://developer.android.com/guide/topics/connectivity/bluetooth https://developer.android.com/guide/topics/connectivity/bluetooth-le#permissions

我认为您缺少 BLUETOOTH_ADMIN 以及 ACCESS_FINE_LOCATION 权限,如果您不使用 Device Companion Manager,则 BLE 需要此权限。

请按照链接中的步骤操作,它应该可以工作!

PS:请查看正确的拼写、语法以及帖子的格式,如果您需要帮助,请提供清晰简洁的信息。根据所提供的信息确定您需要什么并不容易。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。