hasSystemFeature不返回任何东西

时间:2019-02-02 15:59:13

标签: android service bluetooth

我遇到一个奇怪的问题。我执行Android应用中蓝牙连接。在清单中我有权限:

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

我在活动中也有这些意图:

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
    <action android:name="android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED" />
    <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
    <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />

    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

由于我在后台使用服务和接收器进行蓝牙处理,因此我也对该服务具有以下权限:

        <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
        <action android:name="android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED" />
        <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
        <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />

现在,在服务内部,我正在调用以下代码:

if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
   //it is true. bla bla bla
}else{
   //it is false. bla bla bla
}

好了,我不明白为什么,如果我在调试模式下输入时,getPackageManager不返回任何东西。如果我在,如果把一个布尔变量与这条线,我可以清楚地看到返回,没有什么。实际上,就好像代码在那一刻被杀死是因为访问了代码中的任何其他行,但我仍然可以操作该应用程序。难道是因为什么被由于错误而被杀的是服务,而不是应用程序本身?我该如何调试呢?因为我看不到任何错误行。任何想法,错误是来自何处?

编辑:我运行它在Android 8.0

1 个答案:

答案 0 :(得分:0)

其他方面:该函数在我从服务调用的对象的构造函数中调用。您需要将上下文传递给对象:

your_object = new your_object( getApplicationContext() );

并在对象构造函数内部:

public class your_object{

 public your_object(Context context){
     context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
}
}
相关问题