Members
(constant) EventContext
The React Context
that is used to pass data from the App component to the children
components. The EventContext
is used to provide the Scooter
,
IoTInformation
, and VehicleInfo
objects to the event component. The event
component uses these objects to display the current status of the scooter.
- Source:
Example
import React, { useContext } from 'react';
import { View, Text } from 'react-native';
import { EventContext } from './ContextProvider';
function ScooterInfo() {
const { scooter, ioTInformation, vehicleInformation } = useContext(EventContext);
return (
<View>
<Text>Scooter Info</Text>
<Text>ScooterNumber {scooter.number}</Text>
<Text>IoT Device is Locked {ioTInformation.isLocked}</Text>
<Text>Vehicle Battery Level {vehicleInformation.powerPercent}</Text>
</View>
);
}
(constant) SegwayBleManagerModule
This is very important part. If new architecture is enabled, we should use the turbo module. Otherwise, we should get the module from NativeModules.
- Source:
- See:
(constant) Spec
This is a proxy that throws an error when SegwayBleManagerModule is accessed. This is used to prevent SegwayBleManagerModule from being accessed in a context where it is not defined, such as during static analysis.
- Source:
(constant) eventReceiver
The event emitter of the module.
- Source:
(constant) logger
React native applications do not support secondary logging such as assert, timestamp, and so on, rather than just logging levels such as error, warn, info, etc. On the console. So I've only implemented some extensions for easy debugging.
Methods
BLEProvider(props)
Function that connects to a scooter and stores the connection information in the scooter state. This returns a provider that will be contains context when the connection is established It also listens for specific events and updates the context when those events are triggered.
Parameters:
Name | Type | Description | ||||||
---|---|---|---|---|---|---|---|---|
props |
ProviderExoticComponent.<ProviderProps.<T>> |
The props are used to pass the children to the provider. Properties
|
Properties:
Name | Type | Description |
---|---|---|
emitter |
The event emitter is used to communicate with the native code. |
|
mEvents |
The list of events is used to update the context when those events are triggered. |
|
BLEContext |
The context is used to store information that is shared between components. |
- Source:
Example
import React from 'react';
import { View, Text } from 'react-native';
import { BLEProvider } from './ContextProvider';
export default function App() {
return (
<BLEProvider>
<View>
<Text>Hello, world!</Text>
<ScooterInfo />
</View>
</BLEProvider>
);
}
initialize(secretKey, operatorCode, isDebug)
Function that calls the initializing method named SegwayBleManagerModule.init
and then calls
the registerListener
function with the EventType.INITIALIZE
parameter.
Parameters:
Name | Type | Description |
---|---|---|
secretKey |
string | The secret key you received from the Spec.ai team. |
operatorCode |
string | The operator code you received from the Spec team. |
isDebug |
boolean | If true, the SDK will log all the events to the console. |
- Source:
Example
init(e0382c1944874be7a1ed7f4546e0f412, B40006, true);
ioTConnect(deviceMac, deviceKey, iotImei)
Function that calls the SegwayBleManagerModule.connect
function and then calls the
registerListener
function with the EventType.CONNECT
parameter.
Parameters:
Name | Type | Description |
---|---|---|
deviceMac |
string | The MAC address of the scooter. |
deviceKey |
string |
The device key is a unique identifier for the scooter. It is a 16-character string. |
iotImei |
string | The IMEI number of the scooter. |
- Source:
ioTDisconnect()
Function that calls the SegwayBleManagerModule.disconnect
function and then calls the
registerListener
function with the EventType.DISCONNECT
parameter.
- Source:
Example
registerListener();
lockScooter()
Function that calls the SegwayBleManagerModule.lock
function and then calls the
registerListener
function with the EventType.LOCK
parameter.
- Source:
Example
lockScooter();
openBatteryCover()
Function that calls the SegwayBleManagerModule.openBatteryCover
function and then calls the
registerListener
function with the EventType.OPEN_COVER
parameter.
- Source:
Example
onBatteryCover();
openSaddle()
Function that calls the SegwayBleManagerModule.openSaddle
function and then calls the
registerListener
function with the EventType.OPEN_SADDLE
parameter.
- Source:
Example
openSaddle();
openTailBox()
Function that calls the SegwayBleManagerModule.openTailBox
function and then calls the
registerListener
function with the EventType.OPEN_TAIL_BOX
parameter.
- Source:
Example
openTailBox();
queryIoTInfo(listener)
Function that calls the SegwayBleManagerModule.queryIoTInformation
function and then calls
the registerListener
function with the EventType.IOT_INFO
parameter.
Parameters:
Name | Type | Description |
---|---|---|
listener |
EventListener.<EventType.IOT_INFO> | The function to be called when the event is emitted. |
- Source:
Example
queryIoTInfo((data) => console.debug(data));
queryVehicleInfo(listener)
Function that calls the SegwayBleManagerModule.queryVehicleInformation
function and then
calls the registerListener
function with the EventType.VEHICLE_INFO
parameter.
Parameters:
Name | Type | Description |
---|---|---|
listener |
EventListener.<EventType.VEHICLE_INFO> | The function to be called when the event is emitted. |
- Source:
Example
queryVehicleInfo((data) => console.debug(data));
registerListener(eventType, listener) → {EmitterSubscription}
Function that takes an event type and a listener, check the listener count to have only single subscription, and returns a event emitter subscription.
Parameters:
Name | Type | Description |
---|---|---|
eventType |
EventType | The event type to listen for. |
listener |
EventListener.<EventType> | The function to be called when the event is emitted. |
- Source:
- See:
-
- EmitterSubscription https://reactnative.dev/docs/emittersubscription
Returns:
A function that takes a single argument of type T and returns void.
- Type
- EmitterSubscription
Example
registerListener(EventType.INITIALIZE, (data) => {
console.debug(data)
})
unLockScooter()
Function that calls the SegwayBleManagerModule.unLock
function and then calls the
registerListener
function with the EventType.UNLOCK
parameter.
- Source:
Example
registerListener();
validateKeyCode(operatorCode, secretKey, isDebug) → {boolean}
Validate operator code and secret key.
Parameters:
Name | Type | Description |
---|---|---|
operatorCode |
string | Operator Code. |
secretKey |
string | Secret Key. |
isDebug |
boolean | Debug mode. |
Returns:
- True if all strings are valid, otherwise false.
- Type
- boolean
Example
const operatorCode = '830201';
const secretKey = 'f5e5c6cf34214936b2b309c4077a949d'; // trunk-ignore(gitleaks/generic-api-key)
const isDebug = true;
validateKeyCode(operatorCode, secretKey, isDebug); // will return true;
validateRegex(target, type) → {boolean}
Validate a string with a regex.
Parameters:
Name | Type | Description |
---|---|---|
target |
string | String to validate. |
type |
ValidationType | Type of validation. |
Returns:
- True if string is valid, otherwise false.
- Type
- boolean
Example
validateRegex('00:11:22:33:44:55', 'mac-address');
validateScooter(MAC_ADDRESS, DEV_BLE_KEY, IOT_IMEI) → {boolean}
Validate scooter's mac address, device ble key and IoT's IMEI code.
Parameters:
Name | Type | Description |
---|---|---|
MAC_ADDRESS |
string | Mac address of scooter. |
DEV_BLE_KEY |
string | Device's BLE Key. |
IOT_IMEI |
string | IoT's IMEI Code. |
Returns:
- True if all strings are valid, otherwise false.
- Type
- boolean
Example
const MAC_ADDRESS = '00:00:00:00:00:00';
const DEV_BLE_KEY = '00000000000000000000000000000000';
const IOT_IMEI = '000000000000000';
validateScooter(MAC_ADDRESS, DEV_BLE_KEY, IOT_IMEI); // will return true.
Type Definitions
EventType
The enumeration values of supported event names. NOTE: It has to be same with native module's event names.
Type:
- Record.<string, string>
Properties:
Name | Type | Description |
---|---|---|
name |
string | |
CONNECT |
string | The event that returns whether the scooter is connected. |
DISCONNECT |
string | The event that returns whether the scooter is disconnected. |
INITIALIZE |
string | The event that returns whether the scooter is initialized. |
IOT_INFO |
string |
The event that contains condition about the scooter's IoT information. |
VEHICLE_INFO |
string |
The event that contains condition about the scooter's vehicle information. |
LOCK |
string | The event that returns whether the scooter is locked. |
UNLOCK |
string | The event that returns whether the scooter is unlocked. |
OPEN_COVER |
string | The event that returns whether the scooter's cover is opened. |
OPEN_SADDLE |
string | The event that returns whether the scooter's saddle is opened. |
OPEN_TAIL_BOX |
string |
The event that returns whether the scooter's tail box is opened. |
- Source:
- See:
SupportedEvents
The enumeration values of supported event names.
Type:
- Array.<EventType>
- Source: