React-Native, BleManager "Native Module cannot be null" - react-native

I wanted to try out the react-native-ble-plx library from Polidea
https://github.com/Polidea/react-native-ble-plx
So I created a new app in terminal and installed the library as explained by Polidea
create-react-native-app playground
npm install --save react-native-ble-plx
react-native link react-native-ble-plx
I then ran npm start and then i (for opening in iOS simulator)
and then I got the following error (in the terminal and in the simulator)
Native module cannot be null
node_modules/react-native/node_modules/fbjs/lib/invariant.js:44:24 in invariant
node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:31:16 in NativeEventEmitter
node_modules/react-native-ble-plx/src/BleManager.js:52:25 in BleManager
App.js:8:19 in App
node_modules/react-native/Libraries/Renderer/ReactNativeStack-dev.js:1679:33 in
node_modules/react-native/Libraries/Renderer/ReactNativeStack-dev.js:1610:15 in measureLifeCyclePerf
... 52 more stack frames from framework internals
Here is the code in App.js:
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import {BleManager} from 'react-native-ble-plx';
//importing BleManager without calling new BleManager() works
export default class App extends React.Component {
constructor(){
super();
this.manager = new BleManager();
}
//calling new BleManager() leads to error
render() {
return (
<View style={styles.container}>
<Text>nothing, just sucking errors</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
Here is the file package.json
{
"name": "playground",
"version": "0.1.0",
"private": true,
"devDependencies": {
"react-native-scripts": "1.5.0",
"jest-expo": "^21.0.2",
"react-test-renderer": "16.0.0-alpha.12"
},
"main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
"scripts": {
"start": "react-native-scripts start",
"eject": "react-native-scripts eject",
"android": "react-native-scripts android",
"ios": "react-native-scripts ios",
"test": "node node_modules/jest/bin/jest.js --watch"
},
"jest": {
"preset": "jest-expo"
},
"dependencies": {
"expo": "^21.0.0",
"react": "16.0.0-alpha.12",
"react-native": "^0.48.4",
"react-native-ble-plx": "^0.6.3"
}
}
I already looked for help and tried
npm -rm rf node_modules
npm install
but it didn't help.
Here is my system information:
macOS High Sierra
iMac (27-inch, Mid 2010)
Processor 2.8 GHz Intel Core i5
Memory 16 GB 1333 MHz DDR3
I also got the same error when opening the app in my iPhone(6s, 10.3.1) (after scanning QR code in terminal from npm start)
Other apps without the react-native-ble-plx library work normally.
Looking forward receiving some help.
Thanks in advance.
Frank

You have used create-react-native-app to create your React Native project. So you can not use react-native-ble-plx library with this project. Because to access Bluetooth hardware, react-native-ble-plx package has some native code written in android and ios seperately.
If you want to build your app, you'll need to eject from create-react-native-app and use Xcode and Android Studio.
Use the below command:--
npm install -g react-native-cli
npm run eject
react-native link react-native-ble-plx
1st command is to install react-native-cli globally
2nd command is to convert your project from Expo project to regular React Native project
3rd command is to link react-native-ble-plx. Because most probably your previous link was failed.

What I did was (after completely removing ble-plx by discarding git changes before I started)
navigating to /node_modules/react-native-ble-plx/ios/BleClient.xcodeproj and
I pulled that into the Libraries folder in the project navigator
Then I went to my target -> build phases -> link binary with libraries and added libBleClient.a

Related

npm run dev mix doesn't work in laravel 9

I have a fresh laravel 9 project
I want to create an auth system with laravel command by using
composer require laravel/ui
and also
php artisan ui bootstrap --auth
after that I did
npm install
npm run dev
but rather that opening a mix development to generate css styles of auth pages it gave me this output in terminal
npm run dev image
and the page style is being like this
Page styles
I found the solution for this problem
the problem was in the package.json file
package.json was like this:
{
"private": true,
"scripts": {
"dev": "vite",
"build": "vite build"
},
"devDependencies": {
"#popperjs/core": "^2.10.2",
"axios": "^0.25",
"bootstrap": "^5.1.3",
"laravel-mix": "^6.0.49",
"laravel-vite-plugin": "^0.4.0",
"lodash": "^4.17.19",
"postcss": "^8.1.14",
"sass": "^1.32.11",
"sass-loader": "^11.0.1",
"vite": "^2.9.11"
}
}
As you can see the dev in script was applied to vite,
"scripts": {
"dev": "vite",
"build": "vite build"
}
I just modified vite to mix and run
npm install again
and the problem was resolve and tyles of my login page generated successfully
Styles result
Update - ## Better Solution:
Installing Laravel/ui
composer require laravel/ui
Install Bootstrap Auth Scaffold
php artisan ui bootstrap --auth
In Vite.config file
import path by adding this at top:
import path from 'path';
after plugins add this:
resole: {
alias: {
'~bootstrap': path.resolve(__dirname, 'node_module/bootstrap'),
}
}
final result of vite config file will be like this:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import path from 'path';
export default defineConfig({
plugins: [
laravel({
input: [
'resources/sass/app.scss',
'resources/js/app.js',
],
refresh: true,
}),
],
resole: {
alias: {
'~bootstrap': path.resolve(__dirname, 'node_module/bootstrap'),
}
}
});
From resources/js/app.js - add these lines
import '../sass/app.scss';
import * as boostrap from 'bootstrap'
Final Result of resources/js/app.js will be like this
import './bootstrap';
import '../sass/app.scss';
import * as boostrap from 'bootstrap'
Final Step is to change styles and script to #vite directive in resources/view/layouts/app.blade.php
change any css or js files from this type
<link href="{{asset('css/app.css')}}" rel="stylesheet" />
to #vite directive type
#vite(['resources/css/app.css', 'resources/css/app.css'])
Make sure you already have those files before trying to include it, otherwise vite will cause an error.

How to avoid having createStackNavigator crash react-native app?

I'm trying to implement a basic stack navigation using createStackNavigator:
App.js
import { createStackNavigator } from '#react-navigation/stack';
const Stack = createStackNavigator();
function MyStack() {
return (
<Stack.Navigator>
<Stack.Screen name="Home" component={Home} />
<Stack.Screen name="Notifications" component={Notifications} />
<Stack.Screen name="Profile" component={Profile} />
<Stack.Screen name="Settings" component={Settings} />
</Stack.Navigator>
);
}
When I run this on my Mac (Big Sur), I get this error in the Metro console:
[Mon Jan 25 2021 19:21:42.268] BUNDLE ./index.js
[Mon Jan 25 2021 19:21:44.595] ERROR TypeError: null is not an object (evaluating '_RNGestureHandlerModule.default.Direction')
[Mon Jan 25 2021 19:21:44.596] ERROR Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication)
[Mon Jan 25 2021 19:21:45.600] ERROR Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication)
Here is my package.json:
{
"name": "stack_navigation",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"test": "jest",
"lint": "eslint ."
},
"dependencies": {
"#react-navigation/stack": "^5.14.2",
"react": "16.13.1",
"react-native": "0.63.4"
},
"devDependencies": {
"#babel/core": "7.12.10",
"#babel/runtime": "7.12.5",
"#react-native-community/eslint-config": "1.1.0",
"babel-jest": "25.5.1",
"eslint": "6.8.0",
"jest": "25.5.4",
"metro-react-native-babel-preset": "0.59.0",
"react-test-renderer": "16.13.1"
},
"jest": {
"preset": "react-native"
}
}
Is there anything I can do to be able using createStackNavigator without crashing?
In react-navigation installation guide they mentioned that, with react navigation you have to install some other dependencies as well.
So first install these library as well with react-natvigation :
npm install react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context #react-native-community/masked-view
If you are on android auto linking will done automatically, if you are on ios you have to run:
pod install
Now, import react-native-gesture-handle on your app top of the file like in app.js or index.js :
import 'react-native-gesture-handler';
As documentation mentioned you have to do this, you can not skip this installation step if you are building app for android or ios:
Note: If you are building for Android or iOS, do not skip this step, or your app may crash in production even if it works fine in development. This is not applicable to other platforms.
And last you need to wrap the whole app in NavigationContainer. Usually you'd do this in your entry file, such as index.js or App.js:
import 'react-native-gesture-handler';
import * as React from 'react';
import { NavigationContainer } from '#react-navigation/native';
export default function App() {
return (
<NavigationContainer>{/* Rest of your app code */}</NavigationContainer>
);
}
in the app.js you have to import the components if they are not present in that file. also you will need a navigationContainer wrapping the app.js code, indicating that these areas can be navigated. and you will need an event to navigate of course, a button perhaps, igniting the event and you will need a function. So the navigation.navigate in the docs isnt working but when you change that to push it works. However, it just pushes another screen to your stack. You are not navigating among the existing screens you've created. I'd say just go for the react-router. maybe use an older version of react-nav. v5 seems to be broken.

How to fix Expo Print.printToFileAsync(options) undefined is not an object error

I am trying to use Expo's Print.printToFileAsync(options but I keep on getting [Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_expoPrint.Print.printToFileAsync')].
I looked for a lot of solutions online but couldn't find a solution to this. I started using React Native libraries but as I searched, turns out I can only use Expo's library so I switched to Print.printToFileAsync().
App.js
import React, {Component} from 'react';
import { StyleSheet, Text, View, TouchableHighlight } from 'react-native';
import { Print } from 'expo-print';
export default class App extends Component {
async createPDF() {
let filePath = await Print.printToFileAsync({
html: "<h1>PDF TEST</h1>",
width : 612,
height : 792,
base64 : false
});
alert('PDF Generated', filePath.uri);
}
render() {
return(
<View>
<TouchableHighlight onPress={this.createPDF} style={styles.Main}>
<Text>Create PDF</Text>
</TouchableHighlight>
</View>
)
}
}
const styles = StyleSheet.create({
Main : { marginTop : 100 }
});
Package.JSON
{
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo start --web",
"eject": "expo eject"
},
"dependencies": {
"expo": "^33.0.0",
"expo-print": "^5.0.1",
"react": "16.8.3",
"react-dom": "^16.8.6",
"react-native": "https://github.com/expo/react-native/archive/sdk-33.0.0.tar.gz",
"react-native-html-to-pdf": "^0.7.0",
"react-native-share": "^1.2.1",
"react-native-web": "^0.11.4"
},
"devDependencies": {
"babel-preset-expo": "^5.1.1"
},
"private": true
}
My end goal is to make a PDF file using HTML in my Expo project.
Please use -
import * as Print from 'expo-print';
instead of -
import { Print } from 'expo-print';
The Expo sdk33 requires the module to be installed and run directly.
expo install expo-print
Modular Imports
With SDK 33, we’re deprecating imports of most modules from the expo package. This means that in a future release, you won’t be able to write, for example, import { FileSystem } from 'expo';. Rather, you’ll need to install the individual packages for each module you use and import from them instead.
You can use the new expo install command to install modules; this command is a wrapper around npm install/yarn add that automatically installs a module version compatible with your SDK version. For example, for the FileSystem module, you would run expo install expo-file-system and then use import * as FileSystem from 'expo-file-system';. This change paves the way for tree-shaking and smaller JavaScript bundles. It also makes moving between the managed and bare workflows simpler.
Imports from the expo package will continue to work in SDK 33 but will generate a warning in the console, and you’ll need to import from the individual packages to make the warning disappear. To make this change easier, we’re providing a codemod that’ll automatically update all of your imports for you.
Home page with description of SDK33
follow this link https://stackoverflow.com/a/71569236/14448694
import * as Print from "expo-print"; import { shareAsync } from "expo-sharing";

Unable to resolve module 'react-navigation'

Here is the error I get:
package.json
{
"name": "LoginApp2",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"react": "16.0.0-alpha.12",
"react-native": "0.48.3"
},
"devDependencies": {
"babel-jest": "21.2.0",
"babel-preset-react-native": "4.0.0",
"jest": "21.2.1",
"react-test-renderer": "16.0.0-alpha.12"
},
"jest": {
"preset": "react-native"
}
}
index.js
import React, { Component } from 'react';
import { AppRegistry,View,Text,StyleSheet } from 'react-native';
import UsersManager from './pages/app';
AppRegistry.registerComponent('LoginApp2', () => UsersManager);
pages/app.js
import React, { Component } from 'react';
import { AppRegistry,View,Text,StyleSheet,ScrollView,TouchableOpacity } from 'react-native';
import { StackNavigator } from 'react-navigation';
import HomeScreen from './home';
import Login from './login';
import Register from './register';
import Profile from './profile';
const UsersManager = StackNavigator({
Home: { screen: HomeScreen },
Login: { screen: Login },
Register: {screen: Register},
Profile: {screen: Profile}
});
export default UsersManager;
Can someone please help me solve this issue?
This error means that either you haven't installed the react-navigation module or that you have installed the module but didn't re-built your project using react-native run-android or react-native run-ios.
Following these steps should solve your issue:
Install react-navigation module.
Re-build your project.
Restart the packager by stopping the current packager and then
starting the packager again with react-native start.
We need to install the following dependencies:
npm i react-navigation #react-native-community/masked-view react-native-gesture-handler react-native-reanimated react-native-safe-area-context react-native-screens
In code import the following:
import {createAppContainer} from 'react-navigation';
import {createStackNavigator} from 'react-navigation-stack';
Date : 25-June-2020 Working :
Follow this steps:
Install React Navigation
npm install react-navigation
Install Dependencies
expo install react-native-gesture-handler react-native-reanimated react-native-screens react-native-safe-area-context #react-native-community/masked-view
Install React Navigation Stack
npm install react-navigation-stack #react-native-community/masked-view
Start the app and clear cache with
npm start -c
You have to just install the missing module ex.
npm install react-navigation
Then restart with
npm start
I just started to learn react. And got this problem, tried everything from the internet - nothing worked. Using Yarn instead of npm helped!
run "npm add #react-navigation/native" :that saved me hours
This error happened after updating to the new version, to fix it, just run the following command
npx react-native start --reset-cache
install has been replaced with add to add new dependencies. Run yarn add react-navigation instead.
You can use this way
npm install #react-navigation/native #react-navigation/native-stack
expo install react-native-screens react-native-safe-area-context // Expo
npm install react-native-screens react-native-safe-area-context // react native
import { createNativeStackNavigator } from '#react-navigation/native-stack';
import { NavigationContainer } from '#react-navigation/native';
const Stack = createNativeStackNavigator();
<NavigationContainer>
<Stack.Navigator initialRouteName={ routeName } screenOptions = {{ headerShown: false }}>
<Stack.Screen name="Home" component = { componentName } />
<Stack.Screen name="Onboarding" component = { componentName } />
</Stack.Navigator>
</NavigationContainer>
you can flow this document
I resolve this error reading the react navigation documentation, execute yarn add #react-navigation/native-stack. Then the react navigation will be work
Install React Navigation
npm install react-navigation --legacy-peer-deps
or
yarn add react-navigation
Install Dependencies
expo install react-native-gesture-handler react-native-reanimated react-native-screens react-native-safe-area-context #react-native-community/masked-view
Install React Navigation Stack
npm install react-navigation-stack #react-native-community/masked-view --legacy-peer-deps
or
yarn add react-navigation-stack #react-native-community/masked-view
Start the app and clear cache with expo r -c
You need to install stack-navigator in your project.
npm install #react-navigation/stack
and
npm install react-native-gesture-handler
2nd is also required, as per reactnavigation.org
You can also follow this link for detailed steps:
https://reactnavigation.org/docs/stack-navigator/
Refer this image as well
If you're using Expo, run:
expo start -c
If you're not using Expo, run:
npx react-native start --reset-cache
If that doesn't work, you can also try the following:
rm -rf $TMPDIR/metro-bundler-cache-*
More information enter link description here
Stop the server and run:
npm start
This will resolve the issue.

React Native with Native Base (Unexpected Token Error)

I have React Native and Native Base configured. When I deploy the app for Android it throws me an error stating that an unexpected token was found near where Container component is present in my code.
My package.json file is:
{
"name": "React Native POC",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"native-base": "^0.5.18",
"react": "15.4.1",
"react-native": "0.39.2",
"react-redux": "^4.4.6",
"redux": "^3.6.0"
},
"devDependencies": {
"babel-jest": "17.0.2",
"babel-preset-react-native": "1.9.0",
"jest": "17.0.3",
"react-test-renderer": "15.4.1"
},
"jest": {
"preset": "react-native"
}
}
Could it be that certain versions are not working well with the other?
Additionally (if it matters) I am using Node version 6.8.1, npm version 3.10.8 and react-native-cli 2.0.0. Also, I have yarn, sinopia and browserify installed globally.
Still a beginner with React Native and I cannot tell if any dependency clashes may be present (obvious or otherwise)
My js file is:
import React, {Component} from 'react';
import {Container, Content} from 'native-base';
export default class ReactNativePOC extends Component {
render() {
return {
<Container> // Error here
<Content>
</Content>
</Container>
}
}
}
There does not seem to be any problem with my setup (excluding native base) as I can run a react native app with default controls however I seem to be getting this error only for native base controls
The return statement should use parentheses instead of braces.
render() {
return (
<Container>
<Content>
</Content>
</Container>
);
}
Please check the basic syntax from docs of React Native
Check NativeBase KitchenSink - An example app with all the UI components of NativeBase.
Since you said you are beginner in React Native, you can check Native Starter Kit - A Starter Kit for React Native + NativeBase + Navigation Experimental + Redux + CodePush Apps