Skip to content

[Android] App crashes on Gradle 8 (deprecated compile) & onWrite payload is never emitted to JS natively #32

@DhruvaSingh12

Description

@DhruvaSingh12

GitHub Issue Report: react-native-ble-peripheral

1. Deprecated compile dependency mapping blocks Android builds (Gradle 8+)

2. Missing DeviceEventManagerModule.RCTDeviceEventEmitter in RNBLEModule.java prevents onWrite events from ever reaching Javascript.

Description

Hello! I have been building a peer-to-peer data tool using this library to host a BLE peripheral dynamically, and I encountered two severe blocking issues natively on Android that break both the execution build step and the actual data pipeline.

Issue 1: Gradle Build Failure
In android/build.gradle, the dependencies block uses the deprecated compile command. In modern Gradle implementations (Gradle 8+, React Native 0.70+), compile has been completely removed in favor of implementation. This causes modern React Native applications traversing Expo or standard EAS pipelines to completely fail during module initialization.

Issue 2: The onWrite payload is never emitted to Javascript
In the Android native module (RNBLEModule.java), onCharacteristicWriteRequest successfully catches the incoming data payloads and packages them into a WritableMap. However, the module completely forgets to actually emit the event back to the Javascript context via DeviceEventManagerModule. The map is constructed and completely dropped into the void, making it impossible to establish bidirectional communication for the Host natively without mocking the layer. Furthermore, the required addListener and removeListeners methods are missing, violating React Native's NativeEventEmitter validation bounds.

Environment

  • OS: Android (API 34)
  • Library Version: react-native-ble-peripheral@2.0.1
  • React Native: 0.76.x (Expo SDK 52)

Proposed Fix (My local patch)

To get my local application compiling and receiving Host data successfully, I implemented the following patch mapping:

1. android/build.gradle (Replacing compile with implementation)

 dependencies {
-    compile fileTree(dir: "libs", include: ["*.jar"])
-    compile "com.android.support:appcompat-v7:23.0.1"
-    compile "com.facebook.react:react-native:+"  // From node_modules
+    implementation fileTree(dir: "libs", include: ["*.jar"])
+    implementation "com.facebook.react:react-native:+"  // From node_modules
 }

2. RNBLEModule.java (Adding standard RN Device Event Emissions)

+import com.facebook.react.modules.core.DeviceEventManagerModule;

         @Override
         public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId,
                                                  BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded,
                                                  int offset, byte[] value) {
             super.onCharacteristicWriteRequest(device, requestId, characteristic, preparedWrite,
                     responseNeeded, offset, value);
             characteristic.setValue(value);
             WritableMap map = Arguments.createMap();
             WritableArray data = Arguments.createArray();
             for (byte b : value) {
                 data.pushInt((int) b);
             }
             map.putArray("data", data);
             map.putString("device", device.toString());
+            
+            if (reactContext != null) {
+                reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
+                        .emit("onWrite", map);
+            }
+
             if (responseNeeded) {
                 mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
             }
         }
     };

+    @ReactMethod
+    public void addListener(String eventName) { }
+
+    @ReactMethod
+    public void removeListeners(Integer count) { }

Adding these directly natively maps the Event Emitter pipeline fully back over to JS, un-blocking the library for production duplex configurations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions