2

I'm new in React Native and I would like to use native modules in order to get a battery status of the phone. The problem is that I'm getting fallowing error: @"Bridge module %@ does not conform to RCTBridgeModule". I'm guessing that it is class related but I'm not familiar with a Objective-C syntax. I would really appreciate any help on this field even though the answer is simple. Thank you!

My BatteryStatus.h looks like this:

#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>

@interface BatteryStatus : RCTEventEmitter <RCTBridgeModule>
@end

My BatteryStatus.m looks like this:

#import "BatteryStatus.h"

@implementation BatteryStatus
RCT_EXPORT_MODULE(BatteryStatus)

- (instancetype)init
{
  if ((self = [super init])) {

    [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];

  }
  return self;
}

RCT_EXPORT_METHOD(hide) {
}

RCT_EXPORT_METHOD(updateBatteryLevel:(RCTResponseSenderBlock)callback)
{
  callback(@[[self getBatteryStatus]]);
}

//manually get battery status by calling following method

-(NSDictionary*)getBatteryStatus
{

  float batteryLevel = [UIDevice currentDevice].batteryLevel;

  NSObject* currentLevel = nil;

  currentLevel = [NSNumber numberWithFloat:(batteryLevel * 100)];

  NSMutableDictionary* batteryData = [NSMutableDictionary dictionaryWithCapacity:2];

  [batteryData setObject:currentLevel forKey:@"level"];
  return batteryData;

}

@end

And I'm trying to use it in React Native like this:

import { Text, View, NativeModules } from 'react-native';
import React, { Component } from 'react';

class App extends Component {

  constructor(props) {
    super(props);
    this.state = {
    batteryLevel: null,
    };
   }

   componentDidMount() {
     NativeModules.BatteryStatus.updateBatteryLevel((info) => {
     console.log(info.level);
     const level = Math.ceil(info.level);
     this.setState({ batteryLevel: level });
     });
   }
render() {
    return (
      <View>
        <Text>TEST</Text>
      </View>
    );
  }
}


export default App;
Murakami
  • 3,474
  • 7
  • 35
  • 89

1 Answers1

5

In your case, you're not supposed to be inheriting from RCTEventEmitter. You only need to do so if you want to send events from a native module to JS which listens to specific events of your module; in this case you'll need to implement the supportedEvents method in your native module and specify the names of the events that you support. You're getting this exception since you didn't implement this method.

Since you're not sending any events, BatteryStatus should just inherit from the basic NSObject class like so:

@interface BatteryStatus : NSObject <RCTBridgeModule>
@end

I recommend that you check out the RN guide for creating native ios modules.

Artal
  • 8,933
  • 2
  • 27
  • 30
  • You are right! Thank you for that. I will certainly check out the RN guide but first need to learn more RN itself without Objective-C. Thank you anyway, it helped a lot! – Murakami Jan 26 '18 at 11:12
  • Hello Artal, if I want class in module native has: listener and method which javascript side call, what should I do? I am searching the resolve. Thanks in advance. – vannguyen May 30 '19 at 02:22
  • @vannguyen in case you want to send event from native to JS, your class needs to inherit from `RCTEventEmitter` – Artal May 30 '19 at 08:25
  • Thank you for your response. I mean I have a module inherit from RCTEventEmitter. In javascript side, when componentDidMount and componentWillUnMount, I call add Listener and remove Listener. @interface DeeplinkManager : RCTEventEmitter - I want: in javascript side, I want call a method of DeepLinkManager. Ex: DeepLinkManager.functionA(); I tried add functionA(), but its error: functionA() is not define... Thank you so much. – vannguyen May 30 '19 at 08:41
  • @vannguyen it's hard to tell, and it's a different issue than what's mentioned in this question. If you still need help I recommend that you ask a new question and add the code which you can't get to work – Artal May 31 '19 at 14:53
  • Hi Artal, I have just post a question. Please help me this issue. Thank you in advance. https://stackoverflow.com/questions/56403549/exc-bad-access-when-get-value-of-static-nsstring-varible-in-class-inheritit-rcte – vannguyen Jun 01 '19 at 03:04