1
Надеюсь, вы могли бы помочь мне с этим.Android, сервис: получение BluetoothManager возвращает null
Я перемещаю свой код Bluetooth из Activity в службу, но он не работает, чтобы получить BluetoothManager внутри конструктора в службе. Он возвращает значение 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>
Любая помощь приветствуется!
Теперь я просто чувствую себя глупо! Спасибо за помощь. –