GithubHelp home page GithubHelp logo

nguyenphuquang / react-native-tuya Goto Github PK

View Code? Open in Web Editor NEW

This project forked from owowagency/react-native-tuya

1.0 0.0 0.0 54.47 MB

License: MIT License

JavaScript 0.01% Java 6.40% Kotlin 45.36% Ruby 0.18% Objective-C 43.02% TypeScript 5.02%

react-native-tuya's Introduction

@volst/react-native-tuya

This is a fork of TuyaInc/tuyasmart-home-sdk-react-native, fixing a lot of issues we came across and a better install guide. It also uses TypeScript. We use it currently in multiple projects for clients and it is stable.

Some features are not implemented, feel free to send a PR for those missing features. Unfortunately there is no demo yet

Feature Overview

Tuya Smart APP SDK provides the interface package for the communication with hardware and Tuya Cloud to accelerate the application development process, including the following features:

Hardware functions (network configuration, control, status reporting, regular tasks, groups, firmware upgrades, sharing) Account system (phone number, email registration, login, password reset and other general account functions) Tuya Cloud HTTP API interface package

Getting started

npm install @volst/react-native-tuya

This library contains native code which is automatically linked in React Native >= 0.59. For iOS, run cd ios && pod install.

Installation

In the Tuya development environment create a new app and make sure you have an "App key", "App secret" and "Secure image". Read how to do this.

iOS

Put the secure image into the root path of your project as explained here.

In ios/AppDelegate.m, add the following import;

#import <TuyaSmartHomeKit/TuyaSmartKit.h>

Then, under the roootView.backgroundColor line in the same file, add this:

  #ifdef DEBUG
    [[TuyaSmartSDK sharedInstance] setDebugMode:YES];
  #endif

  [[TuyaSmartSDK sharedInstance] startWithAppKey:@"xxx" secretKey:@"xxx"];

Now replace the xxx with your app key and secret key.

Android

Assuming you already have created an app in the Tuya development environment (otherwise follow the iOS steps before this), follow these steps. You should now have an app key, app secret and security image for Android. Make sure the security image is put in android/src/main/assets/t_s.bmp.

Open your AndroidManifest.xml and put the following in the <application> tag:

<meta-data
  android:name="TUYA_SMART_APPKEY"
  android:value="xxx" />
<meta-data
  android:name="TUYA_SMART_SECRET"
  android:value="xxx" />

Replace the xxx with your app key and secret key.

Now open MainApplication.java and add the following import to the top:

import com.tuya.smart.rnsdk.core.TuyaCoreModule;

Change the onCreate function to look like this:

@Override
public void onCreate() {
  super.onCreate();
  SoLoader.init(this, /* native exopackage */ false);
  initializeFlipper(this); // Remove this line if you don't want Flipper enabled
  TuyaCoreModule.Companion.initTuyaSDKWithoutOptions(this);
}

Now you can try to build, but you'll probably run into an error saying that it can't choose between libc++_shared or something. One fix for this (don't know if it's the best fix) is to open android/app/build.gradle and add this;

android {
    ...
    packagingOptions {
        pickFirst '**/armeabi-v7a/libc++_shared.so'
        pickFirst '**/x86/libc++_shared.so'
        pickFirst '**/arm64-v8a/libc++_shared.so'
        pickFirst '**/x86_64/libc++_shared.so'
        pickFirst '**/x86/libjsc.so'
        pickFirst '**/armeabi-v7a/libjsc.so'
    }
}

Usage

Now you can actually use the methods in this package. Unfortunately I don't have time to document them all, so it is advised to read the source code, but here's a start.

To login with an existing account:

import { loginWithEmail } from '@volst/react-native-tuya';

await loginWithEmail({
  countryCode: '+1',
  email: '[email protected]',
  password: 'testtest'
});

To register a new account you first need to validate the email address. And then actually register using the code in the email.

import { getRegisterEmailValidateCode, registerAccountWithEmail } from '@volst/react-native-tuya';

await getRegisterEmailValidateCode({
  countryCode: '+1',
  email: '[email protected]'
});

...

await registerAccountWithEmail({
  countryCode: '+1',
  email: '[email protected]',
  password: 'testtest',
  validateCode: 'xxxxxx'
})

To get the currently logged in user:

import { getCurrentUser } from '@volst/react-native-tuya';

const user = await getCurrentUser();

Local Development

yarn start

Runs the project in development/watch mode. Your project will be rebuilt upon changes. TSDX has a special logger for you convenience. Error messages are pretty printed and formatted for compatibility VS Code's Problems tab.

Your library will be rebuilt if you make edits.

yarn build

Bundles the package to the dist folder. The package is optimized and bundled with Rollup into multiple formats (CommonJS, UMD, and ES Module).

react-native-tuya's People

Contributors

hughcoder avatar nguyenphuquang avatar spacek33z avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.