1.0.5 • Published 9 months ago

dirox-react-native-ble-peripheral v1.0.5

Weekly downloads
-
License
MIT
Repository
-
Last release
9 months ago

dirox-react-native-ble-peripheral

This is a simulator for a BLE peripheral, to help with testing BLE apps without an actual peripheral BLE device this project is not yet complete, for all ready parts see documentation below. (docs are not complete, feel free to improve them)

Installation

npm install dirox-react-native-ble-peripheral

Usage

0. Import package to project

import DiroxReactNativeBlePeripheral from 'dirox-react-native-ble-peripheral';

// ...

Methods

1. checkPermissions

let permissionStatus = await DiroxReactNativeBlePeripheral.checkPermissions(["android.permission.BLUETOOTH", "android.permission.ACCESS_FINE_LOCATION"])
permissionStatus['android.permission.BLUETOOTH']              //  UNAVAILABLE | BLOCKED | DENIED | GRANTED | LIMITED
permissionStatus['android.permission.ACCESS_FINE_LOCATION']   //  UNAVAILABLE | BLOCKED | DENIED | GRANTED | LIMITED

2. requestPermissions

await DiroxReactNativeBlePeripheral.requestPermissions(["android.permission.BLUETOOTH", "android.permission.ACCESS_FINE_LOCATION"])

3. createService

await DiroxReactNativeBlePeripheral.createService(uuid: String, serviceType: int<SERVICE_TYPE_ENUM>)
SERVICE_TYPE_ENUM:
 - PRIMARY = 0
 - SECONDARY = 1 
Example:
 let dataService = await DiroxReactNativeBlePeripheral.createService('9b1deb4d-1bdd-1bdd-1bdd-2b0d7b3dcb6d', 0)

ADD_LISTENER
import { DeviceEventEmitter} from 'react-native';

DeviceEventEmitter.addListener('onCharacteristicReadRequest', event => {
    console.log("onCharacteristicReadRequest call: ", event.uuid)
});
DeviceEventEmitter.addListener('onCharacteristicWriteRequest', event => {
    console.log(`onCharacteristicWriteRequest: ${event.uuid} , ${event.value}, ${event.deviceAddress}, ${event.requestId}`)
});
DeviceEventEmitter.addListener('bluetoothDeviceConnected', event => {
    console.log(`bluetoothDeviceConnected: ${event.deviceAddress}`)
});
DeviceEventEmitter.addListener('bluetoothDeviceDisconnected', event => {
    console.log(`bluetoothDeviceDisconnected: ${event.deviceAddress}`)
});

STOP_LISTENER
DeviceEventEmitter.removeAllListeners()

UUID (String) to Bytes (hex String)
let bytes = await DiroxReactNativeBlePeripheral.getBytesFromUUID('your uuid')

Bytes (hex String) to UUID (String)
let uuid = await DiroxReactNativeBlePeripheral.getUUIDFromBytes(bytes)

4. addCharacteristicService

await DiroxReactNativeBlePeripheral.addCharacteristicService(serviceUuid: String, uuid: String, property: int <ENUM>, permission: int<ENUM>)
PROERTY_ENUM:
- INDICATE = 32
- NOTIFY = 16
- READ = 2
- WRITE = 8
PERMISSION_ENUM:
- READ = 1
- WRITE = 16
Example:
let result = await DiroxReactNativeBlePeripheral.addCharacteristicService(uuidService1, '9b1deb4d-9bdd-9bdd-9bdd-2b0d7b3dcb6d', 2, 16)

5. start

 let result = await DiroxReactNativeBlePeripheral.start("BLE NAME") 
 - result = 0 : user disabled bluetooth
 - result = 1 : success

6. sendNotificationToDevices / sendNotificationToDevice

let result = await DiroxReactNativeBlePeripheral.sendNotificationToDevices(serviceUuid: String, charUuid: String, message: number[])
or
let result = await DiroxReactNativeBlePeripheral.sendNotificationToDevice(device: String, serviceUuid: String, charUuid: String, message: number[])

Example:

let result = await DiroxReactNativeBlePeripheral.sendNotificationToDevices(uuidService1, "9b1deb4d-9bdd-9bdd-9bdd-2b0d7b3dcb6d", [207,159,136,23,88,11,74,78,170,181,159,182,222,160,147,66,35])

7. stop

 let result = await DiroxReactNativeBlePeripheral.stop()

8. setName

let result = await DiroxReactNativeBlePeripheral.setName("NewName")

9. sendResponse

let requestId = 123
let result = await DiroxReactNativeBlePeripheral.sendResponse(requestId, "Value", "deviceAddress")

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with create-react-native-library

1.0.2

9 months ago

1.0.1

9 months ago

1.0.5

9 months ago

1.0.4

9 months ago

1.0.3

9 months ago

0.2.4

12 months ago

0.1.1

1 year ago

0.1.0

1 year ago