Giter Site home page Giter Site logo

nickolans / rn-nordic-dfu Goto Github PK

View Code? Open in Web Editor NEW

This project forked from salt-pepperengineering/react-native-nordic-dfu

1.0 0.0 0.0 21.43 MB

Nordic Device Firmware Update for React Native

License: MIT License

JavaScript 21.73% Ruby 5.84% Objective-C 35.33% Java 32.55% Starlark 4.56%

rn-nordic-dfu's Introduction

rn-nordic-dfu

This library allows you to do a Device Firmware Update (DFU) of your nrf51 or nrf52 chip from Nordic Semiconductor. It works for both iOS and Android.

For more info about the DFU process, see: Resources

Installation

Install and link the NPM package per usual with

npm install --save https://github.com/Nickolans/rn-nordic-dfu

or

yarn add rn-nordic-dfu

iOS Installation

On your project directory;

cd ios && pod install

Update Podfile (Optional)

If your React Native version below 0.60 or any problem occures on pod command, you can try these steps;

Add the following to your Podfile

target "YourApp" do

  ...
  pod "rn-nordic-dfu", path: "../node_modules/rn-nordic-dfu"
  ...

end

and in the same folder as the Podfile run

pod install

Since there's native Swift dependencies you need to set which Swift version your project complies with. If you haven't already done this, open up your project with XCode and add a User-Defined setting under Build Settings: SWIFT_VERSION = <your-swift-version>.

If your React Native version is higher than 0.60, probably it's already there.

Bluetooth integration

iOS

This library needs access to an instance of CBCentralManager, which you most likely will have instantiated already if you're using Bluetooth for other purposes than DFU in your project.

To integrate with your existing Bluetooth setup, call [RNNordicDfu setCentralManagerGetter:<...>] with a block argument that returns your CBCentralManager instance.

If you want control over the CBCentralManager instance after the DFU process is done you might need to provide the onDFUComplete and onDFUError callbacks to transfer back delegate control.

Example code;

...
...
#import "RNNordicDfu.h"
#import "BleManager.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
  ...
  ...

    [RNNordicDfu setCentralManagerGetter:^() {
    return [BleManager getCentralManager];
  }];

  // Reset manager delegate since the Nordic DFU lib "steals" control over it
  [RNNordicDfu setOnDFUComplete:^() {
    NSLog(@"onDFUComplete");
    CBCentralManager * manager = [BleManager getCentralManager];
    manager.delegate = [BleManager getInstance];
  }];

  [RNNordicDfu setOnDFUError:^() {
    NSLog(@"onDFUError");
    CBCentralManager * manager = [BleManager getCentralManager];
    manager.delegate = [BleManager getInstance];
  }];

  return YES;
}

You can find them aslo in example project.

On iOS side this library requires to BleManager module which that react-native-ble-manager provides.

It required because;

  • You need BleManager.h module on AppDelegate file for integration.
  • You should call BleManager.start() (for once) before the trigger a DFU process on iOS or you will get error like this issue.

Android

Android requires that you have FOREGROUND_SERVICE permissions. You will need the following in your AndroidManifest.xml

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

API

startDFU

Starts the DFU process

Observe: The peripheral must have been discovered by the native BLE side so that the bluetooth stack knows about it. This library will not do a scan but only the actual connect and then the transfer. See the example project to see how it can be done in React Native.

Parameters

  • obj Object
    • obj.deviceAddress string The identifier* of the device that should be updated
    • obj.deviceName string The name of the device in the update notification (optional, default null)
    • obj.filePath string The file system path to the zip-file used for updating
    • obj.alternativeAdvertisingNameEnabled boolean Send unique name to device before it is switched into bootloader mode (iOS only) - defaults to true

* identifier โ€” MAC address (Android) / UUID (iOS)

Examples

import { NordicDFU, DFUEmitter } from "react-native-nordic-dfu";

NordicDFU.startDFU({
  deviceAddress: "C3:53:C0:39:2F:99",
  deviceName: "Device Name",
  filePath: "/data/user/0/com.nordicdfuexample/files/RNFetchBlobTmp4of.zip",
})
  .then((res) => console.log("Transfer done:", res))
  .catch(console.log);

Returns Promise A promise that resolves or rejects with the deviceAddress in the return value

DFUEmitter

Event emitter for DFU state and progress events

Examples

import { NordicDFU, DFUEmitter } from "react-native-nordic-dfu";

DFUEmitter.addListener(
  "DFUProgress",
  ({ percent, currentPart, partsTotal, avgSpeed, speed }) => {
    console.log("DFU progress: " + percent + "%");
  }
);

DFUEmitter.addListener("DFUStateChanged", ({ state }) => {
  console.log("DFU State:", state);
});

Selecting firmware file from local storage

If your user will select the firmware file from local storage you should keep on mind some issues;

You can use react-native-document-picker library for file selecting process.

On iOS

You should select file type as public.archive or you will get null type error as like this issue

DocumentPicker.pick({ type: "public.archive" });

If your device getting disconnect after enable DFU, you should set false value to alternativeAdvertisingNameEnabled prop while starting DFU.

NordicDFU.startDFU({
  deviceAddress: "XXXXXXXX-XXXX-XXXX-XXXX-XX",
  filePath: firmwareFile.uri,
  alternativeAdvertisingNameEnabled: false,
});

On Android

Some Android versions directly selecting file may can cause errors. If you get any file error you should copy it to your local storage. Like cache directory.

You can use react-native-fs for copying file.

const firmwareFile = await DocumentPicker.pick({ type: DocumentPicker.types.zip })
const destination = RNFS.CachesDirectoryPath + "/firmwareFile.zip");

await RNFS.copyFile(formatFile.uri, destination);

NordicDFU.startDFU({ deviceAddress: "XX:XX:XX:XX:XX:XX", filePath: destination })

If you getting disconnect error sometimes while starting DFU process, you should connect the device before start it.

Example project

Navigate to example/ and run

npm install

Run the iOS project with

react-native run-ios

and the Android project with

react-native run-android

Development

PR's are always welcome!

Resources

rn-nordic-dfu's People

Contributors

niclas-jetset avatar vikeri avatar nickolans avatar manualexsp avatar looveh avatar dariuszseweryn avatar yannvanhalewyn avatar domir avatar robwalkerco avatar chrischares avatar sofiaschn avatar ezranbayantemur avatar filipengberg avatar kevinresol avatar samulitam avatar anees17861 avatar moonbird-yordi avatar

Stargazers

Andres 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.