bluetooth - Roboletric 返回 null 蓝牙管理器

标签 bluetooth bluetooth-lowenergy robolectric

我尝试使用 Robolectric 测试使用蓝牙的应用程序。但是当我尝试获取 BluetoothManager 时,Roboletric 中总是返回 null。

以下是我如何尝试在 Robolectric 的类(class)中获取蓝牙管理器。

final BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);

感觉我在 Robolectric 设置中遗漏了一些简单的东西?以下是用于设置类的注释。

@Config(
 sdk = Build.VERSION_CODES.KITKAT
 , manifest = "src\\main\\AndroidManifest.xml"
)
@RunWith(RobolectricTestRunner.class)

最佳答案

更新

如果您使用Robolectric v3.1+,问题和答案就会过时

==========================

Robolectric 提供 lot of services from the box 。但是,BluetoothManager 不是其中的一部分。我认为这是因为在 API18 之前我们使用了 BluetoothAdapter.getDefaultAdapter()

看起来您正在瞄准最新的 API,而“旧”方式不是您的选择。作为测试的解决方法,您可以使用描述的方法 here .

我还创建了 issue在 Robolectric GitHub 页面上

关于bluetooth - Roboletric 返回 null 蓝牙管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31715709/

相关文章:

ios - 适用于 iOS/Swift 的支持密码的 BLE 配对

android - Robolectric 说 "AndroidManifest.xml not found"

ios - 如何在 iOS 中使用 Swift 和 Core 蓝牙从 Polar H10 获取心率测量值?

安卓蓝牙 : Software Caused Connection Abort IOException?

android - 打开蓝牙并等待 Intent ?

java - 按下按钮后应用程序崩溃 - 蓝牙 arduino

android - 如何从自定义 BLE 服务(例如智能 watch )读取数据

xcode - CoreBluetooth 框架和 IOBluetooth 框架的区别

android - 使用 Firebase 进行 Robolectric 应用程序测试

java - Robolectric 创建不同应用程序的虚拟共享首选项