我正在尝试使用uiautomator检查Android上的蓝牙配对设备。
public void verByApi() throws UiObjectNotFoundException{
boolean paired = false;
Bundle params = getParams();
String deviceName = params.getString("deviceName");//get device name for verification
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter != null) {
BluetoothDevice[] mAllBondedDevices = (BluetoothDevice[]) mBluetoothAdapter.getBondedDevices().toArray(new BluetoothDevice[0]);
for (BluetoothDevice device : mAllBondedDevices) {
if (device.getName().equals(deviceName)) {
System.out.println(deviceName + " is PAIRED");
paired = true;
}
}
if (!paired) {
System.out.println(deviceName + " is UNPAIRED");
}
} else {System.out.println("getDefaultAdapter() return null");}
}
但是在BluetoothAdapter.getDefaultAdapter()上得到错误NullPointerException。
INSTRUMENTATION_STATUS: numtests=1
INSTRUMENTATION_STATUS: stream=
bluetooth.Verification:
INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
INSTRUMENTATION_STATUS: test=verByApi
INSTRUMENTATION_STATUS: class=bluetooth.Verification
INSTRUMENTATION_STATUS: current=1
INSTRUMENTATION_STATUS_CODE: 1
INSTRUMENTATION_STATUS: numtests=1
INSTRUMENTATION_STATUS: stream=
Error in verByApi:
java.lang.NullPointerException
at android.os.Handler.<init>(Handler.java:229)
at android.os.Handler.<init>(Handler.java:137)
at android.bluetooth.BluetoothAdapter.<init>(BluetoothAdapter.java:442)
at android.bluetooth.BluetoothAdapter.getDefaultAdapter(BluetoothAdapter.java:421)
at bluetooth.Verification.verByApi(Verification.java:66)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.uiautomator.testrunner.UiAutomatorTestRunner.start(UiAutomatorTestRunner.java:160)
at com.android.uiautomator.testrunner.UiAutomatorTestRunner.run(UiAutomatorTestRunner.java:96)
at com.android.commands.uiautomator.RunTestCommand.run(RunTestCommand.java:91)
at com.android.commands.uiautomator.Launcher.main(Launcher.java:83)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:243)
at dalvik.system.NativeStart.main(Native Method)
INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
INSTRUMENTATION_STATUS: test=verByApi
INSTRUMENTATION_STATUS: class=bluetooth.Verification
INSTRUMENTATION_STATUS: stack=java.lang.NullPointerException
at android.os.Handler.<init>(Handler.java:229)
at android.os.Handler.<init>(Handler.java:137)
at android.bluetooth.BluetoothAdapter.<init>(BluetoothAdapter.java:442)
at android.bluetooth.BluetoothAdapter.getDefaultAdapter(BluetoothAdapter.java:421)
at bluetooth.Verification.verByApi(Verification.java:66)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.uiautomator.testrunner.UiAutomatorTestRunner.start(UiAutomatorTestRunner.java:160)
at com.android.uiautomator.testrunner.UiAutomatorTestRunner.run(UiAutomatorTestRunner.java:96)
at com.android.commands.uiautomator.RunTestCommand.run(RunTestCommand.java:91)
at com.android.commands.uiautomator.Launcher.main(Launcher.java:83)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:243)
at dalvik.system.NativeStart.main(Native Method)
INSTRUMENTATION_STATUS: current=1
INSTRUMENTATION_STATUS_CODE: -1
INSTRUMENTATION_STATUS: stream=
Test results for WatcherResultPrinter=.E
Time: 0.115
FAILURES!!!
Tests run: 1, Failures: 0, Errors: 1
INSTRUMENTATION_STATUS_CODE: -1
我使用Android 4.4.4和5.0.1在Nexus 4和Nexus 7上测试此代码。 如果我使用5.0.1它工作正常,但在4.4.4我得到错误。 也许有人有同样的问题?
答案 0 :(得分:0)
来自BlueToothAdapter文档:
要获得代表本地蓝牙适配器的BluetoothAdapter, 在JELLY_BEAN_MR1及以下运行时,请调用静态 getDefaultAdapter()方法;在JELLY_BEAN_MR2及更高版本上运行时, 使用BLUETOOTH_SERVICE通过getSystemService(String)检索它。
希望这有帮助