A Bluetooth kotlin multiplatform "Cross-Platform" library for iOS and Android

Related tags

Kotlin blue-falcon
Overview

Blue Falcon Blue-Falcon CI Kotlin badge badge badge badge badge

A Bluetooth "Cross Platform" Kotlin Multiplatform library for iOS, Android, MacOS, Raspberry Pi and Javascript.

Bluetooth in general has the same functionality for all platforms, e.g. connect to device, fetch services, fetch characteristics.

This library is the glue that brings those together so that mobile developers can use one common api to perform the bluetooth actions.

The idea is to have a common api for using bluetooth as the principle of bluetooth is the same but each platform ios and android has different apis which means you have to duplicate the logic for each platform.

What this library isn't? It is not a cross platform library, this is a multiplatform library. The difference? each platform is compiled down to the native code, so when you use the library in iOS, you are consuming an obj-c library and same principle for Android and so on.

Basic Usage

iOS & MacOS

Create an instance of BlueFalcon and then call the scan method.

By passing in a string uuid of the service uuid, you can filter to scan for only devices that have that service.

let blueFalcon = BlueFalcon(serviceUUID: nil)
blueFalcon.scan()

Android

Install

implementation 'dev.bluefalcon:blue-falcon-android:0.9.4'

And if you are using the debug variant:

implementation 'dev.bluefalcon:blue-falcon-android-debug:0.9.4'

The Android sdk requires an Application context, we do this by passing in on the BlueFalcon constructor, in this example we are calling the code from an activity(this).

By passing in a string uuid of the service uuid, you can filter to scan for only devices that have that service.

try {
    val blueFalcon = BlueFalcon(this, null)
    blueFalcon.scan()
} catch (exception: PermissionException) {
    //request the ACCESS_COARSE_LOCATION permission
}

Raspberry Pi

Install

The Raspberry Pi library is using Java.

implementation 'dev.bluefalcon:blue-falcon-rpi:0.9.4'

Javascript

Install

Simply copy the compiled javascript file (blue-falcon.js) to your web directory.

See the JS-Example for details on how to use.

Kotlin Multiplatform

Install

implementation 'dev.bluefalcon:blue-falcon:0.9.4'

Please look at the Kotlin Multiplatform example in the Examples folder.

BlueFalcon API

The basic functionality of the api is listed below, this should be a simplistic as possible and is the same in any platform.

    fun connect(bluetoothPeripheral: BluetoothPeripheral, autoConnect: Boolean)
    fun disconnect(bluetoothPeripheral: BluetoothPeripheral)
    fun scan()
    fun stopScanning()
    fun readCharacteristic(
        bluetoothPeripheral: BluetoothPeripheral,
        bluetoothCharacteristic: BluetoothCharacteristic
    )
    fun notifyCharacteristic(
        bluetoothPeripheral: BluetoothPeripheral,
        bluetoothCharacteristic: BluetoothCharacteristic,
        notify: Boolean
    )
    fun writeCharacteristic(
        bluetoothPeripheral: BluetoothPeripheral,
        bluetoothCharacteristic: BluetoothCharacteristic,
        value: String
    )
    fun readDescriptor(
        bluetoothPeripheral: BluetoothPeripheral,
        bluetoothCharacteristic: BluetoothCharacteristic,
        bluetoothCharacteristicDescriptor: BluetoothCharacteristicDescriptor
    )
    fun changeMTU(bluetoothPeripheral: BluetoothPeripheral, mtuSize: Int)

Examples

This repo contains examples for kotlin MP, ios and android in the examples folder, install their dependencies, and run it locally:

Kotlin MP

Open the kotlin MP example directory in InteliJ and then run the install targets.

iOS & MacOS

To run the MacOS & iOS example, you need to reference the relevant framework by including it in your poroject. Ensure your project has the Framework search paths referencing the framework directory.

Android

Open the root directory of the project in Android Studio and run the Android app target from the ide.

Raspberry Pi

This example can only be ran on a Raspberry pi, it will crash otherwise.

Javascript

Open the index.html file in a web browser.

Support

For a bug, feature request, or cool idea, please file a Github issue.

Two big little things

Keep in mind that Blue-Falcon is maintained by volunteers. Please be patient if you don’t immediately get an answer to your question; we all have jobs, families, obligations, and lives beyond this project.

Many thanks to everyone so far who has contributed to the project, it really means alot.

Comments
  • Notify not working on Android

    Notify not working on Android

    Describe the bug When calling the notifyCharacteristic, the onCharacteristicChanged never firing. I am getting a value for the read. And I have checked that the device indeed does support notifications using the LightBlue Application on IOS.

    To Reproduce Steps to reproduce the behavior:

    1. Connect to the device using blueFalcon.connect(bluetoothPeripheral, true)
    2. Wait until didDiscoverServices Returns
    3. Search through service to find the characteristic I want to nofify4.
    4. Call notifyCharacteristic on the returned characteristic

    char = service.characteristics.first { it.name == TESS5600_DATA_CHAR.lowercase() } bluetoothService.characteristicDelegate = this bluetoothService.notifyCharacteristic(bluetoothPeripheral, char!!, true)

    Expected behavior Expecting the onCharacteristicChanged to fire

    Smartphone

    • Device: Samsung Tablet
    • OS: Android API 31
    bug 
    opened by burnhamd 4
  • Windows 10 platform support

    Windows 10 platform support

    There is no a working solution to use Windows Bluetooth Api in Java/Kotlin desktop app. The only was is to switch to Linux or to C# Is it possible to port windows to use in kotlin somehow? Do you mind to schedule such feature?

    enhancement 
    opened by permiakover 7
  • changeMTU() method does not work as expected

    changeMTU() method does not work as expected

    When requesting a change of the MTU, the peripheral returned by the callback does not contain services and characteristics. Yet, if you keep a reference of previous characteristics, one cannot write or read on it anymore.

    The problem seems to lie in the fact that the MTU change request should appear between the connection and the discovery of services. Yet BlueFalcon abstracts this layer by calling directly the discovery of services as soon as the peripheral is connected.

    It could be interesting to add an MTU request within the connect method for instance.

    bug 
    opened by davidlepilote 2
  • BluetoothGatt: android.os.DeadObjectException

    BluetoothGatt: android.os.DeadObjectException

    Expected For creating a connection, I am using method connect() with autoConnect flag set to true. This is setup once and it is expected that after setting up this "autoConnect", there is no other action required and Android OS BLE stack will notify the app, once remote device becomes available, and this call will never time out.

    Actual Sometimes, I can reproduce following behavior: App is capable of connecting to device, but when app attempts to write bytes, it results in following exception: "BluetoothGatt: android.os.DeadObjectException"

    To Reproduce Steps to reproduce the behavior:

    1. call connect method with autoConnect flag set to true
    2. Wait a few hours
    3. After reconnection, writing to device will always result in "BluetoothGatt: android.os.DeadObjectException"

    Note: Even restarting the BT module will not solve the issue. What will solve the issue is reinitialising BlueFalcon library.

    Smartphone (please complete the following information):

    • Device: [Samsung Galaxy A5]
    • OS: [Android 7]
    • BlueFalcon version: [0.7.2]
    bug 
    opened by PeterKucera 0
  • GATT connection not closing (ANDROID)

    GATT connection not closing (ANDROID)

    Hi, during more stress test of my app, i've discovered that Android device after some time causes peripheral to stop accept connections and stops from being visible in ble scan. This is not a case for iOS. Steps to reproduce:

    1. Transport type LE
    2. Start LE scan if peripheral MAC found, connect
    3. If onConnected emited, stop LE scan
    4. acquire all characteristics (setup notifications etc)
    5. wait a little time
    6. disconnect
    7. wait a little time
    8. repeat from point 1

    After few loops, you will not be able to find peripheral MAC. Reseting peripheral or Android BT restores connectivity, removing Initializing BlueFalcon again also solves this issue. As far as i can tell, the GATT connection is held and this causes to reach maximum available connections. Good peripheral to test is Raspberry PI 3b+ with simple code using bluez.

    bug 
    opened by felislynx-silae 6
  • Writing characteristic not working after reconnect.

    Writing characteristic not working after reconnect.

    Issue created with #45 fix. After disconnect by calling disconnect method and reconnect again with device within same BlueFalcon instance, we won’t be able to write to gatt characteristic.

    It is because when we close the gatt connection in disconnect method, the method onConnectionStateChange from BluetoothGattCallback will never be called and as a result removeGatt method won’t be called. Thus the item in the Gatt list is not removed. In such cases when trying to write a characteristic we take an old object which is not currently connected to the device.

    In my opinion there are two solutions add additional call of removeGatt in disconnect method moving close() method from disconnect to onConnectionStateChange in BluetoothGattCallback so gatt will be closed after remove.

    First solution should work properly but it is not that clean and neat. Second is better but You need to check if moving close method won’t be an issue in disconnect scenarios and in that case You need to remove delegates.forEach { it.didDisconnect(bluetoothPeripheral) } from disconnect method to avoid calling it twice. This solution need to be tested very carefully.

    bug 
    opened by kazyh 3
Releases(0.10.10)
Owner
Andrew Reed
Mobile developer, keen interest in Kotlin Multiplatform
Andrew Reed
Cross-platform framework for building truly native mobile apps with Java or Kotlin. Write Once Run Anywhere support for iOS, Android, Desktop & Web.

Codename One - Cross Platform Native Apps with Java or Kotlin Codename One is a mobile first cross platform environment for Java and Kotlin developers

Codename One 1.4k Jan 9, 2023
KMM RSS Reader: an open-source, mobile, cross-platform application built with Kotlin Multiplatform Mobile.

KMM RSS Reader This is an open-source, mobile, cross-platform application built with Kotlin Multiplatform Mobile. It's a simple RSS reader, and you ca

Kotlin 1.4k Jan 4, 2023
Utility - The cross-platform native Kotlin command line tool template

Utility The cross-platform native Kotlin command line tool template. Usage Make

null 0 Jan 3, 2022
Real life Kotlin Multiplatform project with an iOS application developed in Swift with SwiftUI, an Android application developed in Kotlin with Jetpack Compose and a backed in Kotlin hosted on AppEngine.

Conferences4Hall Real life Kotlin Multiplatform project with an iOS application developed in Swift with SwiftUI, an Android application developed in K

Gérard Paligot 98 Dec 15, 2022
Dependency Injection library for Kotlin Multiplatform, support iOS and Android

Multiplatform-DI library for Kotlin Multiplatform Lightweight dependency injection framework for Kotlin Multiplatform application Dependency injection

Anna Zharkova 32 Nov 10, 2022
A local storage management library for Kotlin Multiplatform Mobile iOS and android

A local storage management library for Kotlin Multiplatform Mobile iOS and android Features iOS and Android local storage in one interface Provides ge

LINE 20 Oct 30, 2022
An app architecture for Kotlin/Native on Android/iOS. Use Kotlin Multiplatform Mobile.

An app architecture for Kotlin/Native on Android/iOS. Use Kotlin Multiplatform Mobile. 项目架构主要分为原生系统层、Android/iOS业务SDK层、KMM SDK层、KMM业务逻辑SDK层、iOS sdkfra

libill 4 Nov 20, 2022
Simple Kotlin Multiplatform PrayerTimes App for iOS and Android

Kotlin Multiplatform ___ _______ ___ / _ \_______ ___ _____ ___/_ __(_)_ _ ___ ___ / _ | __

Ahmed El-Helw 26 Nov 9, 2022
Location Service Manager for Kotlin Multiplatform Mobile iOS and android

Location Service Manager for Kotlin Multiplatform Mobile iOS and android Features Provides simple permission settings Dramatically reduce the amount o

LINE 55 Dec 10, 2022
Kotlin Multiplatform Mobile demo for Android and iOS - app for viewing Cat pictures

CatViewerDemo Android demo iOS demo Kotlin Multiplatform Mobile demo for Android and iOS. App for viewing Cat pictures from Cats API. This sample show

Martin Rajniak 111 Dec 18, 2022
A Kotlin Multiplatform Project using TMDB Api. Currently supports Android,iOS,Desktop and web platforms

A Kotlin Multiplatform Project using TMDB Api(https://www.themoviedb.org/). Currently this project is implemented in following platforms Andr

Jackson E J 11 Nov 10, 2022
BlurHash support for iOS, Android and JVM via Kotlin Multiplatform

blurhash A Kotlin Multiplatform library to use blurhash in your Android App, iOS / Mac App & JVM Backend. Android iOS JVM Why? If you've tried using b

Niklas Baudy 19 Nov 17, 2022
A Gradle plugin for Kotlin Multiplatform projects that generate a XCFramework for Apple targets or a FatFramework for iOS targets, and manages the publishing process in a CocoaPod Repository.

KMP Framework Bundler KMP Framework Bundler is a Gradle plugin for Kotlin Multiplatform projects that generate a XCFramework for Apple targets or a Fa

Marco Gomiero 17 Oct 29, 2022
Ethereum Web3 implementation for mobile (android & ios) Kotlin Multiplatform development

Mobile Kotlin web3 This is a Kotlin MultiPlatform library that ... Table of Contents Features Requirements Installation Usage Samples Set Up Locally C

IceRock Development 32 Aug 26, 2022
Kotlin Multiplatform Sample - Android, iOS, Web, Desktop

KMP-Sample Kotlin Multiplatform Sample Android iOS Web (Compose for web) Desktop (Compose for desktop) ?? Structure Diagram ?? Build At least android

안홍범 14 Dec 13, 2022
🍭 GithubSearchKMM - Github Repos Search - Android - iOS - Kotlin Multiplatform Mobile using Jetpack Compose, SwiftUI, FlowRedux, Coroutines Flow, Dagger Hilt, Koin Dependency Injection, shared KMP ViewModel, Clean Architecture

GithubSearchKMM Github Repos Search - Kotlin Multiplatform Mobile using Jetpack Compose, SwiftUI, FlowRedux, Coroutines Flow, Dagger Hilt, Koin Depend

Petrus Nguyễn Thái Học 50 Jan 7, 2023
This library provides common speech features for ASR including MFCCs and filterbank energies for Android and iOS.

Kotlin Speech Features Quick Links ?? Introduction This library is a complete port of python_speech_features in pure Kotlin available for Android and

Merlyn Mind 13 Oct 7, 2022
A tiny Kotlin multiplatform library that assists in saving and restoring objects to and from disk using kotlinx.coroutines, kotlinx.serialisation and okio

Store A tiny Kotlin multiplatform library that assists in saving and restoring objects to and from disk using kotlinx.coroutines, kotlinx.serialisatio

Isuru Rajapakse 98 Jan 3, 2023
Server Sent Events (SSE) client multiplatform library made with Kotlin and backed by coroutines

OkSSE OkSSE is an client for Server Sent events protocol written in Kotlin Multiplatform. The implementation is written according to W3C Recommendatio

BioWink GmbH 39 Nov 4, 2022