• Stars
    star
    202
  • Rank 187,596 (Top 4 %)
  • Language
    Kotlin
  • License
    BSD 3-Clause "New...
  • Created 10 months ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Kotlin BLE Library for Android

The library simplifies usage of Android Bluetooth Low Energy on Android. It is a wrapper around native API and uses Kotlin Coroutines for asynchronous operations. The usage is designed to be more natural according to the BLE specification.

BLE Scanner

This module contains a scanner class which provides the list of available Bluetooth LE devices. Each device is kept in an aggregator which keeps devices in map together with their scan records. Scanning works as long as a Flow has an attached consumer. After the Flow is closed the scanning stops.

    //Create aggregator which will concat scan records with a device
    val aggregator = BleScanResultAggregator()
    BleScanner(context).scan()
        .map { aggregator.aggregateDevices(it) } //Add new device and return an aggregated list
        .onEach { _devices.value = it } //Propagated state to UI
        .launchIn(viewModelScope) //Scanning will stop after we leave the screen

Dependency

implementation 'no.nordicsemi.android.kotlin.ble:scanner:1.0.14'

BLE Client

This module is responsible for handling connection between the phone and the BLE device. It uses Kotlin Coroutines instead of JAVA callbacks to handle asynchronous requests.

Below example is based on the Blinky profile.

Connection to the Blinky DK may look like that:

viewModelScope.launch {
    //Connect a Bluetooth LE device. This is a suspend function which waits until device is in conncted state.
    val connection = blinkyDevice.connect(context) //blinkyDevice from scanner

    //Discover services on the Bluetooth LE Device. This is a suspend function which waits until device discovery is finished.
    val services = connection.discoverServices()

    //Remember needed service and characteristics which are used to communicate with the DK.
    val service = services.findService(BlinkySpecifications.UUID_SERVICE_DEVICE)!!
    ledCharacteristic = service.findCharacteristic(BlinkySpecifications.UUID_LED_CHAR)!!
    buttonCharacteristic = service.findCharacteristic(BlinkySpecifications.UUID_BUTTON_CHAR)!!

    //Observe button characteristic which detects when a button is pressed
    //getNotifications() is a suspend function which waits until notification is enabled.
    buttonCharacteristic.getNotifications().onEach {
        //_state is a MutableStateFlow which propagates data to UI.
        _state.value = _state.value.copy(isButtonPressed = BlinkyButtonParser.isButtonPressed(it))
    }.launchIn(viewModelScope)
    
    //Check the initial state of the Led. Read() is a suspend function which waits until the value is read from the DK.
    val isLedOn = BlinkyLedParser.isLedOn(ledCharacteristic.read())
    _state.value = _state.value.copy(isLedOn = isLedOn)
}

Turning on/off a LED light can looks like that:

viewModelScope.launch {
    if (state.value.isLedOn) {
        //Write is a suspend function which waits for the operation to finish.
        ledCharacteristic.write(DataByteArray.from(0x00))
        //No exception means that writing was a success. We can update the UI.
        _state.value = _state.value.copy(isLedOn = false)
    } else {
        //Write is a suspend function which waits for the operation to finish.
        ledCharacteristic.write(DataByteArray.from(0x01))
        //No exception means that writing was a success. We can update the UI.
        _state.value = _state.value.copy(isLedOn = true)
    }
}

Dependency

implementation 'no.nordicsemi.android.kotlin.ble:client:1.0.14'

BLE Advertiser

The library is used to advertise the server.

    val advertiser = BleAdvertiser.create(context)
    val advertiserConfig = BleAdvertisingConfig(
        settings = BleAdvertisingSettings(
            deviceName = "My Server" // Advertise a device name
        ),
        advertiseData = BleAdvertisingData(
            ParcelUuid(BlinkySpecifications.UUID_SERVICE_DEVICE) //Advertise main service uuid.
        )
    )

    viewModelScope.launch {
        advertiser.advertise(advertiserConfig) //Start advertising
            .cancellable()
            .catch { it.printStackTrace() }
            .collect { //Observe advertiser lifecycle events
                if (it is OnAdvertisingSetStarted) { //Handle advertising start event
                    _state.value = _state.value.copy(isAdvertising = true)
                }
                if (it is OnAdvertisingSetStopped) { //Handle advertising stop event
                    _state.value = _state.value.copy(isAdvertising = false)
                }
            }
    }

Dependency

implementation 'no.nordicsemi.android.kotlin.ble:advertiser:1.0.14'

BLE Server

The library is used to create a Bluetooth LE server.

Declaring a server definition

    viewModelScope.launch {
        //Define led characteristic
        val ledCharacteristic = BleServerGattCharacteristicConfig(
            BlinkySpecifications.UUID_LED_CHAR,
            listOf(BleGattProperty.PROPERTY_READ, BleGattProperty.PROPERTY_WRITE),
            listOf(BleGattPermission.PERMISSION_READ, BleGattPermission.PERMISSION_WRITE)
        )

        //Define button characteristic
        val buttonCharacteristic = BleServerGattCharacteristicConfig(
            BlinkySpecifications.UUID_BUTTON_CHAR,
            listOf(BleGattProperty.PROPERTY_READ, BleGattProperty.PROPERTY_NOTIFY),
            listOf(BleGattPermission.PERMISSION_READ, BleGattPermission.PERMISSION_WRITE)
        )

        //Put led and button characteristics inside a service
        val serviceConfig = BleServerGattServiceConfig(
            BlinkySpecifications.UUID_SERVICE_DEVICE,
            BleGattServerServiceType.SERVICE_TYPE_PRIMARY,
            listOf(ledCharacteristic, buttonCharacteristic)
        )

        val server = BleGattServer.create(context, viewModelScope, serviceConfig)
    }

Observing server incoming connections

    server.connectionEvents
        .mapNotNull { it as? ServerConnectionEvent.DeviceConnected }
        .map { it.connection }
        .onEach {
            it.services.findService(BlinkySpecifications.UUID_SERVICE_DEVICE)?.let {
                setUpServices(it)
            }
        }
        .launchIn(viewModelScope)

Setting up characteristic behaviour

    private fun setUpServices(services: BleGattServerService) {
        val ledCharacteristic = services.findCharacteristic(BlinkySpecifications.UUID_LED_CHAR)!!
        val buttonCharacteristic = services.findCharacteristic(BlinkySpecifications.UUID_BUTTON_CHAR)!!

        ledCharacteristic.value.onEach {
            _state.value = _state.value.copy(isLedOn = it != DataByteArray.from(0x00))
        }.launchIn(viewModelScope)

        buttonCharacteristic.value.onEach {
            _state.value = _state.value.copy(isButtonPressed = it != DataByteArray.from(0x00))
        }.launchIn(viewModelScope)

        this.ledCharacteristic = ledCharacteristic
        this.buttonCharacteristic = buttonCharacteristic
    }

    fun onButtonPressedChanged(isButtonPressed: Boolean) = viewModelScope.launch {
        val value = if (isButtonPressed) {
            DataByteArray.from(0x01)
        } else {
            DataByteArray.from(0x00)
        }
        buttonCharacteristic.setValueAndNotifyClient(value)
    }

Dependency

implementation 'no.nordicsemi.android.kotlin.ble:server:1.0.14'

More Repositories

1

Android-BLE-Library

A library that makes working with Bluetooth LE on Android a pleasure. Seriously.
Java
1,853
star
2

Android-nRF-Connect

Documentation and issue tracker for nRF Connect for Android.
1,192
star
3

Android-nRF-Toolbox

The nRF Toolbox is a container app that stores your Nordic Semiconductor apps for Bluetooth Low Energy in one location.
Kotlin
1,048
star
4

Android-Scanner-Compat-Library

A compat library for Bluetooth Low Energy scanning on Android.
Java
739
star
5

Android-DFU-Library

Device Firmware Update library and Android app
Java
728
star
6

IOS-nRF-Toolbox

The nRF Toolbox is a container app that stores your Nordic Semiconductor apps for Bluetooth Low Energy in one location.
Swift
577
star
7

IOS-DFU-Library

OTA DFU Library for Mac and iOS, compatible with nRF5x SoCs
Swift
498
star
8

Android-nRF-Mesh-Library

The Bluetooth Mesh Provisioner and Configurator library.
Java
383
star
9

pc-nrfutil

nrfutil python library and command line client
Python
377
star
10

nRF-Logger-API

The public library with nRF Logger API. Allows to log custom logs into the nRF Logger database.
Java
368
star
11

IOS-nRF-Mesh-Library

Provision, configure and control Bluetooth mesh devices with nRF Mesh library.
Swift
298
star
12

bluetooth-numbers-database

An up-to-date listing of all the various Bluetooth Specification-related elements that are defined by our industry (Company IDs, Service UUIDs, Characteristic UUIDs and Descriptor UUIDs), that you can use instead of rolling your own.
JavaScript
293
star
13

pc-ble-driver

Desktop library for Bluetooth low energy development
C
279
star
14

Android-nRF-Blinky

nRF Blinky is an application developed targeting an audience of developers who are new to Bluetooth Low Energy
Kotlin
249
star
15

ble-sdk-arduino

C++
248
star
16

nrfx

Standalone drivers for peripherals present in Nordic SoCs
C
242
star
17

IOS-CoreBluetooth-Mock

Mocking library for CoreBluetooth framework.
Swift
208
star
18

Nordic-Thingy52-FW

Nordic Thingy:52 software development kit. This kit is designed to assist users in developing their own custom firmware for Thingy. Please see http://www.nordicsemi.com/thingy for the latest news and software releases.
C
208
star
19

nRF-Sniffer-for-802.15.4

nRF-based 802.15.4 sniffer (firmware and software)
Python
206
star
20

pc-nrfconnect-launcher

nRF Connect for Desktop application and framework
TypeScript
162
star
21

nRF5-SDK-for-Mesh

This repo is a "Release" clone of the zip files available @
C
142
star
22

pc-nrfconnect-ble

Bluetooth low energy app for nRF Connect for Desktop
TypeScript
140
star
23

IOS-nRF-Connect

Info page
129
star
24

pc-ble-driver-py

Python bindings for the ble-driver library
Python
124
star
25

Android-nRF-Beacon

The nRF Beacon application lets you explore the full range of possibilities for beacons using Bluetooth Smart technology. The application has been designed to demonstrate all features of the nRF8122 Bluetooth® Smart Beacon Kit.
Java
107
star
26

Android-Nordic-Thingy

Android SDK and sample application for Nordic Thingy:52
Java
104
star
27

zcbor

Low footprint C/C++ CBOR library and Python tool providing code generation from CDDL descriptions.
C
99
star
28

pc-nrfconnect-ppk

Power Profiler app for nRF Connect for Desktop
TypeScript
92
star
29

pc-ble-driver-js

Node.js interface to the C/C++ pc-ble-driver library. API docs: https://nordicsemiconductor.github.io/pc-ble-driver-js/
JavaScript
81
star
30

IOS-nRF-Connect-Device-Manager

A mobile management library for devices supporting nRF Connect Device Manager.
Swift
81
star
31

pynrfjprog

Python wrapper around the nrfjprog dynamic link library (DLL)
C
74
star
32

Android-nRF-Connect-Device-Manager

A mobile management library for devices running Apache Mynewt and Zephyr (DFU, logs, stats, config, etc.)
Java
67
star
33

IOS-Nordic-Thingy

iOS Library and example application for Thingy:52â„¢
Swift
64
star
34

pc-nrfconnect-programmer

Programmer app for nRF Connect for Desktop
TypeScript
62
star
35

Android-BLE-Common-Library

A companion library for the Android BLE Library. Contains parsers for common Bluetooth SIG characteristics.
Java
55
star
36

IOS-BLE-Library

The Bluetooth LE library for iOS and Mac. 100% Swift.
Swift
48
star
37

Android-nRF-Beacon-for-Eddystone

An application with Eddystone GATT Configuration Service & Google Nearby API and Proximity API.
Java
47
star
38

iOS-nRF-Blinky

A starter app for new developers in the BLE world!
Swift
42
star
39

Android-nRF-Bluetooth-LE-Joiner

nRF BLE Joiner is an application that lets you add new IoT nodes to a network based on Bluetooth Smart.
Java
35
star
40

Linux-ble-6lowpan-joiner

C
35
star
41

pc-nrfconnect-rssi

RSSI Viewer app for nRF Connect for Desktop
C
35
star
42

nrf-udev

Shell
29
star
43

Flutter-nRF-Connect-Device-Manager

A Flutter plugin for McuMgr libraries for Android and iOS.
Dart
28
star
44

nrf-intel-hex

Yet another parser for "Intel HEX" files.
JavaScript
27
star
45

pc-nrfjprog-js

Node.js library that exposes the functionality of the nRF Command Line Tools
C++
26
star
46

pc-nrfconnect-toolchain-manager

Install and manage tools to develop with the nRF Connect SDK (NCS)
TypeScript
23
star
47

pc-nrfconnect-shared

Dependency management for nRF Connect for Desktop packages
TypeScript
19
star
48

pc-nrf-dfu-js

Javascript implementation of Nordic's DFU protocol over serial (or NoBLE).
JavaScript
16
star
49

asset-tracker-cloud-firmware-aws

nRF Asset Tracker for AWS firmware.
C
16
star
50

pc-nrfconnect-linkmonitor

LTE Link Monitor app for nRF Connect for Desktop
JavaScript
14
star
51

nrf-device-setup-js

JavaScript
14
star
52

nRF-IEEE-802.15.4-radio-driver

A generic, stack independent and system independent IEEE 802.15.4 radio driver, intended for nRF SoCs with IEEE 802.15.4 radio. It contains a built-in support for multiprotocol, allowing for concurrent operation of IEEE 802.15.4-based stacks (e.g. Thread, ZigBee) with Bluetooth LE
12
star
53

pc-nrfconnect-gettingstarted

nRF Connect Getting Started Assistant is a tool to guide through the process of setting up the nRF Connect SDK toolchain
SCSS
12
star
54

asset-tracker-cloud-aws-js

A reference implementation of a serverless backend for an IoT product developed using AWS CDK in TypeScript.
TypeScript
12
star
55

pc-nrfconnect-dtm

C
10
star
56

asset-tracker-cloud-docs

The nRF Asset Tracker aims to provide a concrete end-to-end example for an IoT product in the asset tracker space.
CSS
9
star
57

e118-iin-list-js

List of issuer identifier numbers for the international telecommunication charge card (ITU-T E.118)
TypeScript
9
star
58

pc-nrfconnect-boilerplate

Starting point for creating apps for nRF Connect for Desktop
TypeScript
8
star
59

IOS-Memfault-Library

nRF Memfault Library + Example App
Swift
6
star
60

Android-nRF-Wi-Fi-Provisioner

An Android library and app for provisioning Nordic Wi-Fi devices to a network.
Kotlin
6
star
61

IOS-nRF-Edge-Impulse

Connect to a Thing:53, collect sensor data over Bluetooth Low Energy and create your machine learning applications.
Swift
6
star
62

Android-Memfault-Library

An Android library for sending heap dumps to the Memfault cloud.
Kotlin
6
star
63

asset-tracker-cloud-app-js

The nRF Asset Tracker Web Application is a reference single-page application (SPA) developed using create-react-app in TypeScript.
TypeScript
5
star
64

pc-nrfconnect-tracecollector

Trace collector tool for nRF Connect for Desktop
JavaScript
5
star
65

nRF52-Bluetooth-Course

5
star
66

pc-nrfconnect-cellularmonitor

TypeScript
5
star
67

modemtalk

[end-of-life] library to interface with the nRF9160 modem
JavaScript
5
star
68

piccata

Python CoAp Toolkit
Python
4
star
69

cell-geolocation-helpers-js

Helper functions for the cell geolocation feature
TypeScript
4
star
70

microbit-v2-pager

C
4
star
71

Android-Gradle-Plugins

Kotlin
4
star
72

pc-nrfconnect-docs

Developer documentation for nRF Connect for Desktop. Read them at https://NordicSemiconductor.github.io/pc-nrfconnect-docs/
HTML
4
star
73

nrf-device-lister-js

List usb, serialport and jlink devices
JavaScript
4
star
74

svada

SVD Parsing for Python
Python
3
star
75

asset-tracker-cloud-device-ui-js

Provides a user interface via a web application to interact with a simulated device for the nRF Asset Tracker.
TypeScript
3
star
76

at_host-hex

Compiles the AT host sample for 9161DK, 9160DK, and Thingy:91 and publishes it regularly.
3
star
77

asset-tracker-cloud-firmware-azure

nRF Asset Tracker for Azure firmware.
C
3
star
78

object-to-env-js

Helper functions which converts an object to environment variable definitions
TypeScript
3
star
79

NAT-TestServer

Receives NAT test messages from the NAT-TestFirmware and logs them and timeout occurances to S3
TypeScript
3
star
80

cloud-aws-package-layered-lambdas-js

Packages lambda functions and layers for AWS lambda
TypeScript
3
star
81

KMM-BLE-Library

Kotlin
3
star
82

IOS-nRF-Wi-Fi-Provisioner

A Swift library and app for provisioning Nordic Wi-Fi devices to a network.
Swift
3
star
83

random-words-js

Returns random 8-letter words from the Webster's Unabridged Dictionary
JavaScript
3
star
84

npmx

Standalone drivers for Nordic PMICs
C
3
star
85

npmx-zephyr

npmx integration with Zephyr
C
2
star
86

NFC-Connect

Kotlin
2
star
87

cloud-azure-firmware-ci-runner-js

Runs firmware on a real device connected to Azure IoT hub.
TypeScript
2
star
88

pmic-npm6001-lib

C
2
star
89

cloud-device-helpers-js

Helper functions for interacting with the nRF9160 used during end-to-end tests
TypeScript
2
star
90

Android-nRF-Edge-Impulse

Connect to a Thing:53, collect sensor data over Bluetooth Low Energy and create your machine learning applications.
Kotlin
2
star
91

cloud-e2e-bdd-test-runner-js

Implementation of a test-runner for end-to-end tests of cloud-native applications using Gherkin features.
TypeScript
2
star
92

pc-nrfconnect-npm

TypeScript
2
star
93

cloud-aws-timestream-helpers-js

Helper functions which simplify working with AWS Timestream
TypeScript
1
star
94

asset-tracker-cloud-code-style-js

Code style definitions for all nRF Asset Tracker projects.
JavaScript
1
star
95

asset-tracker-cloud-app-aws-js

The nRF Asset Tracker Web Application for AWS is a reference single-page application (SPA) developed using TypeScript.
TypeScript
1
star
96

rsrp-bar-js

React component to render an RSRP bar or a failover icon in case the reported value is not valid
TypeScript
1
star
97

nrfcloud-location-services-tests-js

Verifies the nRF Cloud Location Services REST API.
TypeScript
1
star
98

from-env-js

Helper function which ensures that required environment variables are set.
TypeScript
1
star
99

cloud-azure-firmware-ci-feature-runner-action

Uses @nordicsemiconductor/e2e-bdd-test-runner to run features files of a firmware.
JavaScript
1
star
100

cloud-aws-cloudformation-helpers-js

Helper functions which simplify working with AWS CloudFormation stacks
TypeScript
1
star