diff --git a/src/app/server/java/AppMain.cpp b/src/app/server/java/AppMain.cpp index 18f1c2c019761b..3a34c10cede618 100644 --- a/src/app/server/java/AppMain.cpp +++ b/src/app/server/java/AppMain.cpp @@ -45,9 +45,6 @@ CHIP_ERROR ChipAndroidAppInit(void) err = chip::Platform::MemoryInit(); SuccessOrExit(err); - err = chip::DeviceLayer::PlatformMgr().InitChipStack(); - SuccessOrExit(err); - ConfigurationMgr().LogDeviceConfig(); // Init ZCL Data Model and CHIP App Server diff --git a/src/controller/java/CHIPDeviceController-JNI.cpp b/src/controller/java/CHIPDeviceController-JNI.cpp index 339e2561ea6857..2c5a8d7af593c4 100644 --- a/src/controller/java/CHIPDeviceController-JNI.cpp +++ b/src/controller/java/CHIPDeviceController-JNI.cpp @@ -146,10 +146,6 @@ JNI_METHOD(jlong, newDeviceController)(JNIEnv * env, jobject self) ChipLogProgress(Controller, "newDeviceController() called"); - // sSystemLayer and sInetLayer are in platform/android to share with app server - err = DeviceLayer::PlatformMgr().InitChipStack(); - SuccessOrExit(err); - wrapper = AndroidDeviceControllerWrapper::AllocateNew(sJVM, self, kLocalDeviceId, &DeviceLayer::SystemLayer(), &DeviceLayer::InetLayer(), &err); SuccessOrExit(err); diff --git a/src/platform/android/AndroidChipPlatform-JNI.cpp b/src/platform/android/AndroidChipPlatform-JNI.cpp index fd2cbd20dd7f8b..dc10f5d1c5c932 100644 --- a/src/platform/android/AndroidChipPlatform-JNI.cpp +++ b/src/platform/android/AndroidChipPlatform-JNI.cpp @@ -96,6 +96,12 @@ void AndroidChipPlatformJNI_OnUnload(JavaVM * jvm, void * reserved) chip::Platform::MemoryShutdown(); } +JNI_METHOD(void, initChipStack)(JNIEnv * env, jobject self) +{ + CHIP_ERROR err = chip::DeviceLayer::PlatformMgr().InitChipStack(); + VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "Error initializing CHIP stack: %s", ErrorStr(err))); +} + // for BLEManager JNI_METHOD(void, nativeSetBLEManager)(JNIEnv *, jobject, jobject manager) { diff --git a/src/platform/android/java/chip/platform/AndroidChipPlatform.java b/src/platform/android/java/chip/platform/AndroidChipPlatform.java index dbd294043e6537..88754ee5d9e1b0 100644 --- a/src/platform/android/java/chip/platform/AndroidChipPlatform.java +++ b/src/platform/android/java/chip/platform/AndroidChipPlatform.java @@ -26,7 +26,10 @@ public AndroidChipPlatform( ConfigurationManager cfg, ServiceResolver resolver, ChipMdnsCallback chipMdnsCallback) { + // Order is important here: initChipStack() initializes the BLEManagerImpl, which depends on the + // BLEManager being set. setConfigurationManager() depends on the CHIP stack being initialized. setBLEManager(ble); + initChipStack(); setKeyValueStoreManager(kvm); setConfigurationManager(cfg); setServiceResolver(resolver, chipMdnsCallback); @@ -72,6 +75,9 @@ public native void handleUnsubscribeComplete( // for ConfigurationManager private native void setConfigurationManager(ConfigurationManager manager); + /** Initialize the CHIP stack. */ + private native void initChipStack(); + // for ServiceResolver private void setServiceResolver(ServiceResolver resolver, ChipMdnsCallback chipMdnsCallback) { if (resolver != null) {