gpt4 book ai didi

安卓,服务: getting the BluetoothManager returns null

转载 作者:行者123 更新时间:2023-12-02 18:06:51 25 4
gpt4 key购买 nike

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

我正在将蓝牙代码从 Activity 移动到服务,但在服务的构造函数内获取蓝牙管理器不起作用。它不断返回 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

这是 list 文件:

<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>

感谢任何帮助!

最佳答案

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

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

关于安卓,服务: getting the BluetoothManager returns null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24579422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com