Android,服务:获取BluetoothManager返回空值
希望你能帮我做这件事 我正在将我的蓝牙代码从一个活动移动到一个服务,但是将BluetoothManager放在服务的构造函数中是行不通的。它一直返回null。你知道为什么吗Android,服务:获取BluetoothManager返回空值,android,android-service,android-bluetooth,Android,Android Service,Android Bluetooth,希望你能帮我做这件事 我正在将我的蓝牙代码从一个活动移动到一个服务,但是将BluetoothManager放在服务的构造函数中是行不通的。它一直返回null。你知道为什么吗 public class BluetoothService extends Service { private Handler handler; private static int secondsBetweenScans; private BluetoothAdapter mBluetoothAdapter; priv
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) {
.
.
.
};
}
公共类蓝牙服务扩展服务{
私人经办人;
扫描之间的专用静态int seconds;
私人蓝牙适配器mBluetoothAdapter;
私人经理人;
私有布尔扫描;
私有地图发现了蓝牙设备及其信号强度;
私有ArrayList foundBluetoothDevices;
//10秒后停止扫描。
专用静态最终长扫描时间=2000;
@凌驾
公共IBinder onBind(意向){
返回null;
}
公共蓝牙服务{
Log.i(“KontaktBluetoothService”、“BluetoothService已初始化”);
//初始化蓝牙适配器。
final BluetoothManager MBluetothostManager=(BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);不要在构造函数中执行此操作。将其移动到onCreate()
中
此时服务的上下文尚未完全设置
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>