Bluetooth Server

This library provides support for acting as a Bluetooth Server: advertising and exposing GATT attributes for clients to read, write and subscribe to.

Installing

This library is available on Maven Central. You can import Kaluga Bluetooth Server as follows:

repositories {
    // ...
    mavenCentral()
}
// ...
dependencies {
    // ...
    implementation("com.splendo.kaluga:bluetooth-server:$kalugaVersion")
}

Usage

Check out the full documentation

Create a BluetoothServer through the BluetoothServerBuilder (or via BluetoothBuilder.createServer() from the bluetooth module). In it, specify the advertising and the attributes to support:


val server = builder.createServer(context) {
    advertise {
        localName = "Kaluga Server"
        serviceUUIDs(kalugaUUID)
    }
    service(kalugaUUID) {
        characteristic(characteristicUUID) {
            readable {
                GattResponse.ReadSuccess(byteArrayOf())
            }
            writable { _, data, _ ->
                println("Did write ${data.toHexString()}")
                GattResponse.WriteSuccess
            }
            notifiable {
                onSubscribe {
                    coroutineScope.launch {
                        notify(byteArrayOf())
                    }
                }
            }
            
            // Alternative notification
            flowOf(byteArrayOf()).collectTo(coroutineScope) {
                triggerNotification()
            }
            
            // Ignored on iOS
            descriptor(descriptorUUID) {
                
            }
        }
    }
}

val notifiableCharacteristic = server.services[kalugaUUID].characteristics[characteristicUUID] as LocalCharacteristic.Notifiable
notifiableCharacteristic.notifyAll(byteArrayOf())

server.close() // server must be closed when done

For (de)serializing the data exchanged with clients, see the BluetoothFormat documentation in bluetooth-base.