This reposatory shows an example Android application for a bluetooth low energy scanner written is react native, using expo. Step by step I will guide you how to develop and deploy into production this application. The goal is to scan the nearby smartphones with enabled bluetooth (with different IDs) and estimate it's relative distance for each source. Nowadays the electric rollers (scooters) are great risk to car traffic. The aim is to alarm the smartphone users (cars), to prepare the nearby scooter traffic. Therefore I implemented a separate mode for cars (scan) and another mode for scooters (beacon) in the same application.
https://play.google.com/store/apps/details?id=host.exp.exponent
https://developer.android.com/studio/debug/dev-options
Set the necessary environmental variables.
Make you sure to choose that option during the installation which command line interface supports git commands.
https://developer.android.com/studio/install
Set the necessary environmental variables.
https://www.oracle.com/java/technologies/javase/jdk20-archive-downloads.html
Set the necessary environmental variables.
npx create-expo-app@latest
cd <project_name>
npx expo install react-native-ble-manager
"plugins": [
"expo-router",
[
"react-native-ble-manager",
{
"isBleRequired": true,
"neverForLocation": true,
"companionDeviceEnabled": false,
"bluetoothAlwaysPermission": "Allow BLE DEMO APP to connect to bluetooth devices"
}
]
]{
"cli": {
"version": ">= 3.9.0"
},
"build": {
"development": {
"developmentClient": true,
"distribution": "internal",
"ios": {
"resourceClass": "m-medium"
},
"channel": "development"
}
}
}npx expo prebuild
adb kill-server
Remove adbkey file from c:/Users/<Username>/.android
adb start-server
17. Revoke USB debugging authorization, then disable USB debugging, and finally enable USB debugging on your smartphone
npx expo run:android
This process will take sime time for the first run.
npx expo install react-native-ble-plx
{
"build": {
"development": {
"developmentClient": true,
"distribution": "internal"
},
"preview": {
"distribution": "internal"
},
"production": {}
}
}npx expo install expo-device
"plugins": [
[
"react-native-ble-plx",
{
"isBackgroundEnabled": true,
"modes": ["peripheral", "central"],
"bluetoothAlwaysPermission": "Allow $(PRODUCT_NAME) to connect to bluetooth devices"
}
]
]npx npm install eas-cli
npx expo install expo-dev-client
npm install react-native-background-timer
npm install react-native-ble-advertiser
npx expo prebuild
Locate and update your AndroidManifest.xml from <project_name>\android\app\src\main\AndroidManifest.xml
<!-- Required for BLE on Android 12+ -->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<!-- Required for older Android versions -->
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>Locate and update your build.gradle from <project_name>\node_modules\react-native-ble-advertiser\android\build.gradle
apply plugin: 'com.android.library'
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
}
}
dependencies {
implementation 'com.facebook.react:react-native:+'
}
npx expo run:android
At this point we finished doing the development and this reposatory contains every necessary files, except the node_modules and android folders because it's size. Now let's turn this into production. In production you will have unique credentials, therefore I do not want to include them.
eas login
eas build:configure
{
"cli": {
"version": ">= 3.9.0"
},
"build": {
"preview": {
"android": {
"buildType": "apk"
}
},
"production": {},
"development": {
"developmentClient": true,
"distribution": "internal",
"ios": {
"resourceClass": "m-medium"
},
"channel": "development"
}
}
}eas build -p android --profile preview