Android,服务:获取BluetoothManager返回null

时间:2014-07-04 18:35:05

标签: android android-service android-bluetooth

希望你能帮助我解决这个问题。

我正在将蓝牙代码从“活动”移动到“服务”,但它无法将蓝牙管理器置于服务中的构造函数中。它一直返回null。你知道为什么吗?

public class BluetoothService extends Service {

private Handler handler;
private static int secondsBetweenScans;
private BluetoothAdapter mBluetoothAdapter;

private Handler mHandler;

private boolean mScanning;

private Map<String, Integer> foundBluetoothDevicesAndTheirSignalStrengths;
private ArrayList<String> foundBluetoothDevices;

// Stops scanning after 10 seconds.
private static final long SCAN_DURATION = 2000;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

public BluetoothService() {
    Log.i("KontaktBluetoothService", "BluetoothService initialized");

    // Initializes Bluetooth adapter.
    final BluetoothManager mbluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); <-- Returns null
    mBluetoothAdapter = mbluetoothManager.getAdapter();

    .
    .
    .

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    .
    .
    .

}

private void scanLeDevice(final boolean enable) {
    .
    .
    .
}

// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {

    @Override
    public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
        .
        .
        .
    };

}

这是我收到的错误

07-04 20:29:02.134: D/AndroidRuntime(27710): Shutting down VM
07-04 20:29:02.134: W/dalvikvm(27710): threadid=1: thread exiting with uncaught exception (group=0x41ce1700)
07-04 20:29:02.159: E/AndroidRuntime(27710): FATAL EXCEPTION: main
07-04 20:29:02.159: E/AndroidRuntime(27710): java.lang.RuntimeException: Unable to instantiate service fo.vera.kontakt.BluetoothService: java.lang.NullPointerException
07-04 20:29:02.159: E/AndroidRuntime(27710):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2671)
07-04 20:29:02.159: E/AndroidRuntime(27710):    at android.app.ActivityThread.access$1700(ActivityThread.java:159)
07-04 20:29:02.159: E/AndroidRuntime(27710):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1404)
07-04 20:29:02.159: E/AndroidRuntime(27710):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-04 20:29:02.159: E/AndroidRuntime(27710):    at android.os.Looper.loop(Looper.java:176)
07-04 20:29:02.159: E/AndroidRuntime(27710):    at android.app.ActivityThread.main(ActivityThread.java:5419)
07-04 20:29:02.159: E/AndroidRuntime(27710):    at java.lang.reflect.Method.invokeNative(Native Method)
07-04 20:29:02.159: E/AndroidRuntime(27710):    at java.lang.reflect.Method.invoke(Method.java:525)
07-04 20:29:02.159: E/AndroidRuntime(27710):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
07-04 20:29:02.159: E/AndroidRuntime(27710):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
07-04 20:29:02.159: E/AndroidRuntime(27710):    at dalvik.system.NativeStart.main(Native Method)
07-04 20:29:02.159: E/AndroidRuntime(27710): Caused by: java.lang.NullPointerException
07-04 20:29:02.159: E/AndroidRuntime(27710):    at android.content.ContextWrapper.getSystemService(ContextWrapper.java:526)
07-04 20:29:02.159: E/AndroidRuntime(27710):    at fo.vera.kontakt.BluetoothService.<init>(BluetoothService.java:38)
07-04 20:29:02.159: E/AndroidRuntime(27710):    at java.lang.Class.newInstanceImpl(Native Method)
07-04 20:29:02.159: E/AndroidRuntime(27710):    at java.lang.Class.newInstance(Class.java:1130)
07-04 20:29:02.159: E/AndroidRuntime(27710):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2668)
07-04 20:29:02.159: E/AndroidRuntime(27710):    ... 10 more

这是清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="fo.vera.kontakt"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

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

    <uses-feature android:name="android.hardware.bluetooth_le" android:required="false"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
         <activity
            android:name="fo.vera.kontakt.MainActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

         <service
            android:name="fo.vera.kontakt.BluetoothService"
            android:enabled="true"
            android:exported="false" >
        </service>
    </application>

</manifest>

感谢任何帮助!

1 个答案:

答案 0 :(得分:5)

不要在构造函数中执行此操作。将其移至onCreate()

此时服务的上下文尚未完全设置。