背景服务崩溃

huangapple go评论79阅读模式
英文:

Background service crash

问题

所以出现某种原因导致我的后台服务崩溃,而我不知道原因所在。
我的服务包含BLE,我从MainActivity中使用以下代码绑定到服务:

public void startBluetoothService() {
    // 启动BLE服务
    Log.d(TAG, "Starting BLE Service");
    Intent gattServiceIntent = new Intent(this, BLE_Service.class);
    bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
    Log.d(TAG, "Bluetooth is Enabled");
}

这段代码运行了,但是之后我得到了以下错误:

10-09 12:51:31.104 14007-14081/com.example.wrd I/ProviderInstaller: Installed default security provider GmsCore_OpenSSL
10-09 12:51:31.114 14007-14007/com.example.wrd D/AndroidRuntime: Shutting down VM
10-09 12:51:31.114 14007-14007/com.example.wrd E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.wrd, PID: 14007
    java.lang.RuntimeException: Unable to instantiate service com.example.wrd.ble.BLE_Service: java.lang.InstantiationException: java.lang.Class<com.example.wrd.ble.BLE_Service> has no zero argument constructor
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3841)
        at android.app.ActivityThread.access$2100(ActivityThread.java:229)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1909)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7325)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
     Caused by: java.lang.InstantiationException: java.lang.Class<com.example.wrd.ble.BLE_Service> has no zero argument constructor
        at java.lang.Class.newInstance(Native Method)
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3838)
        at android.app.ActivityThread.access$2100(ActivityThread.java:229)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1909)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7325)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

我的服务看起来是这样的:

public class BLE_Service extends Service {
    // ... 其他部分的代码 ...

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public BLE_Service(Home home, long scanPeriod, int signalStrength) {
        // 构造函数的实现
        // ...
    }

    // ... 其他部分的代码 ...
}

你提到了在你的onStart()中启动了服务,然后在onCreate()中运行了 BLE_Service(this, 7500, -75),但是问题在于BLE_Service的构造函数需要一个参数列表。可能的解决方法之一是,将这部分逻辑放到onServiceConnected()回调中,以确保服务已经被正确绑定。这样做可以确保在构造函数中调用服务实例之前,onStart()已经成功地绑定了服务。

private ServiceConnection mServiceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        // 这里是服务绑定成功的回调
        BLE_Service.LocalBinder binder = (BLE_Service.LocalBinder) iBinder;
        mBoundService = binder.getService();
        mServiceBound = true;

        // 这里可以执行启动服务的逻辑,确保服务已绑定后再运行
        mBoundService.initialize(); // 例如这里初始化服务
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        mServiceBound = false;
    }
};

当你在onStart()中绑定服务时,onServiceConnected()将会在服务绑定成功时被调用。在这个回调中,你可以执行启动服务以及其他必要的逻辑,确保服务在使用之前已经准备好。

另外,确保在不再需要服务时解除绑定,可以在onStop()中实现:

@Override
protected void onStop() {
    super.onStop();
    if (mServiceBound) {
        unbindService(mServiceConnection);
        mServiceBound = false;
    }
}

这样可以避免内存泄漏和其他可能的问题。

英文:

So for some reason my background service crashes, and I don't know why.
So my service contains BLE, and I bind to the service from the MainActivity with:

public void startBluetoothService() {
    // Start the BLE Service
    Log.d(TAG, &quot;Starting BLE Service&quot;);
    Intent gattServiceIntent = new Intent(this, BLE_Service.class);
    bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
    Log.d(TAG, &quot;Bluetooth is Enabled&quot;);
}

This runs, but then I get the following error

10-09 12:51:31.104 14007-14081/com.example.wrd I/ProviderInstaller: Installed default security provider GmsCore_OpenSSL
10-09 12:51:31.114 14007-14007/com.example.wrd D/AndroidRuntime: Shutting down VM
10-09 12:51:31.114 14007-14007/com.example.wrd E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.wrd, PID: 14007
    java.lang.RuntimeException: Unable to instantiate service com.example.wrd.ble.BLE_Service: java.lang.InstantiationException: java.lang.Class&lt;com.example.wrd.ble.BLE_Service&gt; has no zero argument constructor
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3841)
        at android.app.ActivityThread.access$2100(ActivityThread.java:229)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1909)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7325)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
     Caused by: java.lang.InstantiationException: java.lang.Class&lt;com.example.wrd.ble.BLE_Service&gt; has no zero argument constructor
        at java.lang.Class.newInstance(Native Method)
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3838)
        at android.app.ActivityThread.access$2100(ActivityThread.java:229)&#160;
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1909)&#160;
        at android.os.Handler.dispatchMessage(Handler.java:102)&#160;
        at android.os.Looper.loop(Looper.java:148)&#160;
        at android.app.ActivityThread.main(ActivityThread.java:7325)&#160;
        at java.lang.reflect.Method.invoke(Native Method)&#160;
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)&#160;
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)&#160;

My service looks like this:


public class BLE_Service extends Service {

    private Home ma;

    private boolean mscanning;
    public boolean Read = false;
    private Handler mHandler;

    private long scanPeriod;
    private int signalStrength;

    //Intent string
    private static String DeviceAddress;

    private static final String TAG = &quot;BLE_Service&quot;;
    // Bluetooth objects that we need to interact with
    private static BluetoothManager mBluetoothManager;
    private static BluetoothAdapter mBluetoothAdapter;
    private static BluetoothGatt mBluetoothGatt;
    private static BluetoothDevice device;

    private static BluetoothGattCharacteristic BLECharacterisitc;
    private final static String BaseUuid = &quot;00000000-0000-1000-8000-00805f9b34f&quot;;
    private final static String CSUuid = &quot;00002902-0000-1000-8000-00805f9b34fb&quot;;
    private final static String UuidWrite = BaseUuid + &quot;0&quot;;
    private final static String UuidRead = BaseUuid + &quot;1&quot;;
    private final static String UuidRead2 = BaseUuid + &quot;2&quot;;
    private final static String state_RW = BaseUuid + &quot;0&quot;;
    private final static String treatmentSettings_RW = BaseUuid + &quot;1&quot;;
    private final static String adjustment_RW = BaseUuid + &quot;2&quot;;
    private final static String validation_R = BaseUuid + &quot;3&quot;;

    //Broadcasts
    public final static String BROADCAST_ADDED = &quot;added&quot;;
    public final static String BROADCAST_ADDEDFrag = &quot;added&quot;;

    // Bluetooth characteristics that we need to read/write
    private static BluetoothGattCharacteristic mCharacterisitc, mCharacterisitc2, mCharacterisitcWrite, mCharacterisitc_state_RW,
            mCharacterisitc_treatmentSettings_RW, mCharacterisitc_adjustment_RW, mCharacterisitc_validation_R;


    private static StringBuilder sb = new StringBuilder();


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

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public boolean connecting(String DeviceAddress) {
        Log.d(TAG, &quot;Connection State: connect&quot;);
        device = mBluetoothAdapter.getRemoteDevice(DeviceAddress);
        if (device == null) {
            Log.d(TAG, &quot;Connection State Device Not Available &quot;);
            return false;
        } else {
            Log.d(TAG, &quot;Connection State Connecting...&quot;);
            device.connectGatt(this, false, mGattCallback);
            return true;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public boolean disconnect() {
        mBluetoothGatt.disconnect();
        mBluetoothGatt.close();
        return true;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public void checkConnectedDevices() {
        if (mBluetoothGatt != null){
            String DeviceString = mBluetoothGatt.getDevice().getAddress();
            String NameString = mBluetoothGatt.getDevice().getName();
            Log.d(TAG, &quot;checkConnectedDevices: &quot; + NameString);
            //broadCastHEX(BROADCAST_ADDEDFrag, NameString+&quot;;&quot;+DeviceString, &quot;get_conn_BLE_return&quot;);
        }else{
            //broadCastHEX(BROADCAST_ADDEDFrag, null, &quot;Connected_BLE_Devices_return&quot;);
        }
    }

    public class LocalBinder extends Binder {
        public BLE_Service getService() {
            return BLE_Service.this;
        }
    }

    private final IBinder mBinder = new LocalBinder();

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * Initializes a reference to the local Bluetooth adapter.
     *
     * @return Return true if the initialization is successful.
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public boolean initialize() {

        Log.d(TAG, &quot;mBluetoothManager: service&quot;);
        // For API level 18 and above, get a reference to BluetoothAdapter through
        // BluetoothManager.
        if (mBluetoothManager == null) {
            mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
            if (mBluetoothManager == null) {
                Log.e(TAG, &quot;Unable to initialize BluetoothManager.&quot;);
                return false;
            }
        }
        Log.d(TAG, &quot;mBluetoothManager: initialize&quot;);
        BluetoothManager bluetoothManager = (BluetoothManager) getBaseContext().getSystemService(Context.BLUETOOTH_SERVICE);
        List&lt;BluetoothDevice&gt; devices = bluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
        Log.d(TAG, &quot;mBluetoothManager: initialize device&quot; + devices.toString());
        for(BluetoothDevice device : devices) {
            if(device.getType() == BluetoothDevice.DEVICE_TYPE_LE) {
                Log.d(TAG, &quot;mBluetoothManager: running loop&quot; + device.getName() + &quot;\n&quot;);
            }else{
                Log.d(TAG, &quot;mBluetoothManager: nothing&quot; + device.getName() + &quot;\n&quot;);
            }
        }
        mBluetoothAdapter = mBluetoothManager.getAdapter();
        if (mBluetoothAdapter == null) {
            Log.e(TAG, &quot;Unable to obtain a BluetoothAdapter.&quot;);
            return false;
        }

        return true;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public BLE_Service(Home home, long scanPeriod, int signalStrength) {

        ma = home;

        mHandler = new Handler();

        this.scanPeriod = scanPeriod;
        this.signalStrength = signalStrength;

        final BluetoothManager bluetoothManager =
                (BluetoothManager) ma.getSystemService(Context.BLUETOOTH_SERVICE);

        mBluetoothAdapter = bluetoothManager.getAdapter();
    }

    public boolean isScanning() {
        return mscanning;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public void start() {
        if (!Utils.checkBluetooth(mBluetoothAdapter)) {
            Utils.requestUserBluetooth(ma);
            ma.stopScan();
        } else {
            scanLeDevice(true);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public void stop() {
        scanLeDevice(false);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    private void scanLeDevice(final boolean enable) {
        if (enable &amp;&amp; !mscanning) {
            Utils.toast(ma.getApplication(), &quot;Starting BLE Scan...&quot;);
            mHandler.postDelayed(new Runnable() {
                @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
                @Override
                public void run() {
                    Utils.toast(ma.getApplicationContext(), &quot;Stopping BLE scan...&quot;);

                    mscanning = false;
                    mBluetoothAdapter.stopLeScan(mLeScanCallback);
                    ma.stopScan();
                }
            }, scanPeriod);

            mscanning = true;
            mBluetoothAdapter.startLeScan(mLeScanCallback);
        }
    }


    private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
        @Override
        public void onConnectionStateChange(final BluetoothGatt gatt, int status, int newState) {
            Log.d(TAG, &quot;Connection State Change: &quot; + status + &quot; -&gt; &quot; + connectionState(newState));
            gatt.discoverServices();
            if (status == BluetoothGatt.GATT_SUCCESS &amp;&amp; newState == BluetoothProfile.STATE_CONNECTED) {
                /*
                 * Once successfully connected, we must next discover all the services on the
                 * device before we can read and write their characteristics.
                 */
                mBluetoothGatt = gatt;
                BluetoothDevice device = gatt.getDevice();
                Log.d(TAG, &quot;Connection State onConnectionStateChange: &quot; + device);
            } else if (status == BluetoothGatt.GATT_SUCCESS &amp;&amp; newState == BluetoothProfile.STATE_DISCONNECTED) {
                /*
                 * If at any point we disconnect, send a message to clear the weather values
                 * out of the UI
                 */
                Log.d(TAG, &quot;Connection State: 2&quot;);
            } else if (status != BluetoothGatt.GATT_SUCCESS) {
                /*
                 * If there is a failure at any stage, simply disconnect
                 */
            }
        }

        /**
         * This is called when a service discovery has completed.
         *
         * It gets the characteristics we are interested in and then
         * broadcasts an update to the main activity.
         *
         * @param gatt The GATT database object
         * @param status Status of whether the write was successful.
         */
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
        @Override
        public void onServicesDiscovered(final BluetoothGatt gatt, int status) {//pr&#248;v med ekstra status p&#229; denne
            if (status == BluetoothGatt.GATT_SUCCESS) {
                BluetoothGattService mBluetoothGattService = gatt.getService(UUID.fromString(CSUuid));
                if (mBluetoothGattService != null) {
                    Log.i(TAG, &quot;Connection State: Service characteristic UUID found: &quot; + mBluetoothGattService.getUuid().toString());
                    mCharacterisitc = mBluetoothGattService.getCharacteristic(UUID.fromString(UuidRead));
                    mCharacterisitc2 = mBluetoothGattService.getCharacteristic(UUID.fromString(UuidRead2));
                    mCharacterisitcWrite = mBluetoothGattService.getCharacteristic(UUID.fromString(UuidWrite));
                    //First activate buttons here....
                    Log.w(TAG, &quot;Connection State 1: mCharacterisitc &quot; + mCharacterisitc + &quot; &quot; + mCharacterisitc2);
                    Intent broadcastIntent = new Intent().putExtra(&quot;ConnState&quot;, true);
                    broadcastIntent.setAction(BLE_Service.BROADCAST_ADDED);
                    LocalBroadcastManager.getInstance(BLE_Service.this).sendBroadcast(broadcastIntent);
                    if(Read){
                        readCharacteristic(gatt, mCharacterisitc);
                    }
                } else {
                    Log.i(TAG, &quot;Connection State: Service characteristic not found for UUID: &quot; + UuidRead);
                }
            }
        }

        private String connectionState(int status) {
            switch (status) {
                case BluetoothProfile.STATE_CONNECTED:
                    return &quot;Connection State: Connected&quot;;
                case BluetoothProfile.STATE_DISCONNECTED:
                    return &quot;Connection State: Disconnected&quot;;
                case BluetoothProfile.STATE_CONNECTING:
                    return &quot;Connection State: Connecting&quot;;
                case BluetoothProfile.STATE_DISCONNECTING:
                    return &quot;Connection State: Disconnecting&quot;;
                default:
                    return String.valueOf(status);
            }
        }

        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicRead(gatt, characteristic, status);
            if (status == BluetoothGatt.GATT_SUCCESS) {
                Log.d(TAG, &quot;Connection State: onCharacteristicRead&quot;);
                Log.w(TAG, &quot;Connection State: Read 1&quot;);
                // Verify that the read was the LED state
                String uuid = characteristic.getUuid().toString();
                // In this case, the only read the app does is the LED state.
                // If the application had additional characteristics to read we could
                // use a switch statement here to operate on each one separately.
                if (uuid.equalsIgnoreCase(UuidRead)) {
                    Log.w(TAG, &quot;Connection State: Read 2&quot;);
                    final int Val = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
                    broadCast(BROADCAST_ADDEDFrag, Val, &quot;read_Service&quot;);
                    Log.i(TAG, &quot;Connection State: onCharacteristicRead: &quot; + Val);
                    Log.d(TAG, &quot;Connection State Read state: &quot;+Read);
                    readCharacteristic(gatt, mCharacterisitc2);
                }
                if (uuid.equalsIgnoreCase(UuidRead2)) {
                    Log.w(TAG, &quot;Connection State: Read 3&quot;);
                    final byte[] data = characteristic.getValue();
                    if (data != null &amp;&amp; data.length &gt; 0) {
                        final StringBuilder stringBuilder = new StringBuilder(data.length);
                        for (byte byteChar : data)
                            stringBuilder.append(String.format(&quot;%02X &quot;, byteChar));
                        String msg = &quot;letter: &quot; + new String(data) + &quot;\nHEX: &quot; + stringBuilder.toString();
                        Log.d(TAG, &quot;Connection State: onCharacteristicRead2: 4 &quot; + msg);
                        broadCastHEX(BROADCAST_ADDEDFrag, msg, &quot;read_Service2&quot;);
                    }
                    Read = false; //Has to be set to false in the last read message..
                    Log.d(TAG, &quot;Connection State Read state: &quot;+Read);
                }
            }
        }
    };

    /**
     * Runs service discovery on the connected device.
     *
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public void discoverServices() {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.d(TAG, &quot;Connection State BluetoothAdapter not initialized&quot;);
            return;
        }
        mBluetoothGatt.discoverServices();
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public void Write(Integer integer) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.d(TAG, &quot;Connection State BluetoothAdapter not initialized&quot;);
            return;
        }
        Integer Val = integer;
        byte[] byteVal;
        byteVal = intToByteArray(Val);
        mCharacterisitcWrite.setValue(byteVal);
        Log.d(TAG, &quot;Connection State write 1: &quot; + byteVal);
        mBluetoothGatt.writeCharacteristic(mCharacterisitcWrite);
    }

    public static byte[] intToByteArray(int a) {
        byte[] ret = new byte[4];
        ret[3] = (byte) (a &amp; 0xFF);
        ret[2] = (byte) ((a &gt;&gt; 8) &amp; 0xFF);
        ret[1] = (byte) ((a &gt;&gt; 16) &amp; 0xFF);
        ret[0] = (byte) ((a &gt;&gt; 24) &amp; 0xFF);
        return ret;
    }

    /**
     * This method is used to read the state of the LED from the device
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public void readCharacteristic(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        if (mBluetoothAdapter == null || gatt == null) {
            Log.w(TAG, &quot;Connection State: BluetoothAdapter not initialized 2&quot;);
            return;
        }
        Log.w(TAG, &quot;Connection State: BluetoothAdapter initialized&quot;);
        gatt.readCharacteristic(characteristic);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    private BluetoothAdapter.LeScanCallback mLeScanCallback =
            new BluetoothAdapter.LeScanCallback() {
                @Override
                public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
                    final int new_rssi = rssi;
                    if (rssi &gt; signalStrength &amp;&amp; device.getName() != null) {
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    FragmentBLE.addDevice(device, new_rssi);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                    }
                }
            };

    public void broadCast(String broadcastTXT, Integer Val, String key) {
        Log.d(TAG, &quot;Connection State: -&gt; broadCast: Service Broadcast&quot;);
        Intent broadcastIntent = new Intent().putExtra(key, Val);
        broadcastIntent.setAction(broadcastTXT);
        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcastIntent);
    }

    public void broadCastHEX(String broadcastTXT, String Val, String key) {
        Log.d(TAG, &quot;Connection State: -&gt; broadCast: Service Broadcast&quot;);
        Intent broadcastIntent = new Intent().putExtra(key, Val);
        broadcastIntent.setAction(broadcastTXT);
        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcastIntent);
    }
}

So what causes this error to occure?
looks like it's somthing to do with java.lang.RuntimeException: Unable to instantiate service com.example.wrd.ble.BLE_Service, and I've been serching on it, but can't seem to find anything that helps.

EDIT

So my filled contructer is done before my empty contructer is set. I bind in my onStart()

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
protected void onStart() {
    super.onStart();
    // Start the BLE Service
    Log.d(TAG, &quot;Starting BLE Service&quot;);
    Intent gattServiceIntent = new Intent(this, BLE_Service.class);
    bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
    Log.d(TAG, &quot;Bluetooth is Enabled&quot;);
    registerReceiver(mBTStateUpdateReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
}

And then in my oncreate I run the following

mBTLeScanner = new BLE_Service(this, 7500, -75);

but somehow BLE_Service(this, 7500, -75) is finished before onStart()

答案1

得分: 2

创建一个空的构造函数。

BLE_Service(){
}
英文:

Create an empty constructor.

BLE_Service(){
}

huangapple
  • 本文由 发表于 2020年10月9日 18:57:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64278674.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定