简体   繁体   English

如何在 Android 上测试 BLE 对象?

[英]How to test BLE objects on Android?

I am learning to communicate over Bluetooth Low Energy on Android.. Here is an example app我正在学习通过 Android 上的蓝牙低功耗进行通信。 这是一个示例应用程序

There in there source code are several Bluetooth related objects, which were final classes obviously:源代码中有几个与蓝牙相关的对象,它们显然是最终类:

private BluetoothManager mBluetoothManager;
private BluetoothAdapter mBluetoothAdapter;
private BluetoothGatt mBluetoothGatt;

Of course I do not want to test library stuff like BluetoothManager, BluetoothAdapter or BluetoothGatt itself.当然,我不想测试 BluetoothManager、BluetoothAdapter 或 BluetoothGatt 本身之类的库内容。 But I want to test BluetoothLeService: Service which was written in that project.但我想测试在该项目中编写的BluetoothLeService: Service

I do not know, how to mock these final BluetoothManager, BluetoothAdapter or BluetoothGatt objects.我不知道,如何模拟这些final BluetoothManager, BluetoothAdapter or BluetoothGatt对象。

  1. How can I test BluetoothLeService ?如何测试BluetoothLeService
  2. Can I write plain unit test s or do I need to write special androidTests s where the device is connected during the tests?我可以编写普通的 unit test还是需要在测试期间连接设备的地方编写特殊的androidTests
  3. How would this look when I have integration systems as a build environment?当我将集成系统作为构建环境时,这会是什么样子?

You should have some tests which run when your device is actually connected to another bluetooth device.当您的设备实际连接到另一个蓝牙设备时,您应该进行一些测试。

  • To ensure that you've implemented the boilerplate code for communicating with a real bluetooth device correctly.确保您已经实现了与真正的蓝牙设备正确通信的样板代码。
  • To ensure you're reading / writing the correct characteristics that are expected from the device.确保您正在读取/写入设备预期的正确特性。

Most of the code is boilerplate, and will be the same in any application which uses bluetoothle.大多数代码都是样板代码,并且在任何使用蓝牙的应用程序中都是相同的。 The differences will be:差异将是:

  • Services, Characteristics, and what / when you update or read information from the bluetooth device.服务、特性以及更新或从蓝牙设备读取信息的内容/时间。

So I would split your testing into two parts:所以我会把你的测试分成两部分:

  • Code that simply connects to a bluetooth device, and reads and writes from the device.简单地连接到蓝牙设备并从设备读取和写入的代码。 This module of code should be reusable in any app, and should not contain any business logic unique to your app.此代码模块应可在任何应用程序中重复使用,并且不应包含任何应用程序独有的业务逻辑。 That way you need to test with a real device only when you make changes to this module of code.这样,只有在更改此代码模块时,您才需要使用真实设备进行测试。

  • Code that is unique to your app.您的应用程序独有的代码。 This includes all the business logic that executes once you read / write information from the bluetooth device.这包括从蓝牙设备读取/写入信息后执行的所有业务逻辑。 Here you can write a mock class that pretends to be the bluetooth device from which you're reading / writing information.在这里,您可以编写一个模拟 class 假装是您从中读取/写入信息的蓝牙设备。 It can act as a layer between the business logic unique to your app, and the code which actually connects and interacts with the bluetooth device.它可以充当应用程序特有的业务逻辑与实际连接蓝牙设备并与之交互的代码之间的一层。 During your automated tests, it pretends to provide mock data its read from the bluetooth device.在您的自动化测试期间,它假装提供从蓝牙设备读取的模拟数据。 When you want to test with a real device, toggle a flag, and have it read and write to a real bluetooth device using the code I mentioned in the previous bullet point.当您想使用真实设备进行测试时,切换一个标志,并使用我在上一个要点中提到的代码让它读取和写入真实的蓝牙设备。

Most of the code in the sample application is boiler plate.示例应用程序中的大部分代码都是样板代码。 You should cut out anything extraneous from it that your app does not need and use it as a layer in your app that your business logic communicates with.您应该从中删除任何您的应用程序不需要的无关内容,并将其用作应用程序中与您的业务逻辑通信的层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM