我有一个服务有一个实例BluetoothGattCallback
public class MyService extends Service {
private BluetoothGattCallback callback;
@Override
public void onCreate() {
super.onCreate();
callback = new BluetoothGattCallback() {
@Override
public synchronized void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
Log.i("onConnectionStateChanged", "Status " + status);
Log.i("onConnectionStateChanged", "New State " + newState);
}
};
}
// registration of bluetooth adapter and blah blah blah
}
当我启动应用程序时,它工作正常,回调只被调用一次,但经过几次尝试,它被调用了两次。
示例日志
10-22 13:29:18.731 26944-26961/redacted.lollipop I/onConnectionStateChange: Status 0
10-22 13:29:18.731 26944-26961/redacted.lollipop I/onConnectionStateChange: New State 2
10-22 13:29:18.731 26944-26961/redacted.lollipop I/onConnectionStateChange: Status 0
10-22 13:29:18.731 26944-26961/redacted.lollipop I/onConnectionStateChange: New State 2
更多示例日志
10-22 13:29:48.836 26944-26961/redacted.lollipop I/onConnectionStateChange: Status 8
10-22 13:29:48.836 26944-26961/redacted.lollipop I/onConnectionStateChange: New State 0
10-22 13:29:48.850 26944-30763/redacted.lollipop I/onConnectionStateChange: Status 8
10-22 13:29:48.850 26944-30763/redacted.lollipop I/onConnectionStateChange: New State 0
应用程序保持活跃的时间越长,它被调用的次数就越多。我该如何防止这种情况?