Skip to content

Commit

Permalink
Merge pull request #30 from Reedyuk/descriptor-support
Browse files Browse the repository at this point in the history
Added ability to read descriptor.
  • Loading branch information
Reedyuk authored Apr 7, 2020
2 parents 0934725 + 47b4fb1 commit 9ad269a
Show file tree
Hide file tree
Showing 29 changed files with 233 additions and 37 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ![Blue Falcon](bluefalcon.png) Blue-Falcon [![Build Status](https://api.travis-ci.com/Reedyuk/blue-falcon.svg?branch=master)](https://api.travis-ci.com/Reedyuk/blue-falcon) [![Kotlin](https://img.shields.io/badge/kotlin-1.3.70-blue.svg)](http://kotlinlang.org)
# ![Blue Falcon](bluefalcon.png) Blue-Falcon [![Build Status](https://api.travis-ci.com/Reedyuk/blue-falcon.svg?branch=master)](https://api.travis-ci.com/Reedyuk/blue-falcon) [![Kotlin](https://img.shields.io/badge/kotlin-1.3.71-blue.svg)](http://kotlinlang.org)

A Bluetooth "Cross Platform" Kotlin Multiplatform library for iOS and Android.

Expand Down Expand Up @@ -32,7 +32,7 @@ blueFalcon.scan()
#### Install

```kotlin
implementation 'dev.bluefalcon:library-android:0.5.6'
implementation 'dev.bluefalcon:library-android:0.6.1'
```

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).
Expand Down Expand Up @@ -71,6 +71,11 @@ The basic functionality of the api is listed below, this should be a simplistic
bluetoothCharacteristic: BluetoothCharacteristic,
value: String
)
fun readDescriptor(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic,
bluetoothCharacteristicDescriptor: BluetoothCharacteristicDescriptor
)
fun changeMTU(bluetoothPeripheral: BluetoothPeripheral, mtuSize: Int)
```

Expand All @@ -80,7 +85,7 @@ This repo contains examples for kotlin MP, ios and android in the examples folde

### Kotlin MP

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

### iOS

Expand Down
6 changes: 3 additions & 3 deletions examples/KotlinMP/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'org.jetbrains.kotlin.multiplatform' version '1.3.50'
id 'org.jetbrains.kotlin.multiplatform' version '1.3.71'
}
repositories {
mavenLocal()
Expand Down Expand Up @@ -60,7 +60,7 @@ kotlin {
dependencies {
implementation kotlin('stdlib-common')
implementation("dev.bluefalcon:library:$bluefalcon")
implementation ("org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.2")
implementation ("org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.5-native-mt")
}
}
commonTest {
Expand All @@ -83,7 +83,7 @@ kotlin {
iosMain {
dependencies {
implementation kotlin('stdlib')
implementation ("org.jetbrains.kotlinx:kotlinx-coroutines-core-native:1.3.2")
implementation ("org.jetbrains.kotlinx:kotlinx-coroutines-core-native:1.3.5-native-mt")
}
}
iosTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package sample

import dev.bluefalcon.BlueFalcon
import dev.bluefalcon.BlueFalconDelegate
import dev.bluefalcon.BluetoothCharacteristic
import dev.bluefalcon.BluetoothPeripheral
import dev.bluefalcon.*

expect fun BluetoothService.scan()

Expand Down Expand Up @@ -44,6 +41,13 @@ class BluetoothService(private val blueFalcon: BlueFalcon) {
bluetoothCharacteristic: BluetoothCharacteristic
) {
blueFalcon.readCharacteristic(bluetoothPeripheral, bluetoothCharacteristic)
bluetoothCharacteristic.descriptors.forEach { descriptor ->
blueFalcon.readDescriptor(
bluetoothPeripheral,
bluetoothCharacteristic,
descriptor
)
}
}

fun notifyCharacteristic(
Expand Down Expand Up @@ -87,6 +91,13 @@ class BluetoothService(private val blueFalcon: BlueFalcon) {
deviceConnectDelegate?.didDiscoverServices(bluetoothPeripheral)
}

override fun didReadDescriptor(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristicDescriptor: BluetoothCharacteristicDescriptor
) {
println("read descriptor ${bluetoothCharacteristicDescriptor}")
}

override fun didCharacteristcValueChanged(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic
Expand Down
2 changes: 1 addition & 1 deletion examples/KotlinMP/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
classpath 'com.android.tools.build:gradle:3.6.0-rc01'
}
}
repositories {
Expand Down
4 changes: 2 additions & 2 deletions examples/KotlinMP/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
kotlin.code.style=official
bluefalcon=0.5.4
kotlin_version=1.3.50
bluefalcon=0.6.1
kotlin_version=1.3.71
android_tools_version=3.4.1
anko_version=0.10.8
4 changes: 2 additions & 2 deletions examples/KotlinMP/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Thu Oct 17 20:24:39 BST 2019
distributionUrl=https\://services.gradle.org/distributions/gradle-5.5.1-all.zip
#Thu Apr 02 19:49:11 BST 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
Expand Down
4 changes: 2 additions & 2 deletions examples/KotlinMP/iosApp/iosApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.3;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
OTHER_LDFLAGS = "-v";
Expand Down Expand Up @@ -463,7 +463,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.3;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
MTL_ENABLE_DEBUG_INFO = NO;
OTHER_LDFLAGS = "-v";
SDKROOT = iphoneos;
Expand Down
2 changes: 1 addition & 1 deletion examples/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.5-native-mt'

implementation "org.jetbrains.anko:anko:$anko_version"
implementation "org.jetbrains.anko:anko-support-v4:$anko_version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@ class DeviceServiceAdapter(var viewModels : List<DeviceCharacteristicViewModel>)
}
}
}
if (item.descriptors.isNotEmpty()) {
verticalLayout {
textView("Descriptors") {
typeface = Typeface.DEFAULT_BOLD
}
linearLayout {
textView("Values")
item.descriptorValues.forEach {
textView(" ${it.key}: ${it.value} ")
}
}
linearLayout {
button("Read") {
onClick {
item.readDescriptorTapped()
}
}
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.bluefalcon.services

import android.bluetooth.BluetoothDevice
import dev.bluefalcon.*
import java.util.*
import android.os.Build
Expand All @@ -18,8 +17,6 @@ class BluetoothService: BlueFalconDelegate {

init {
blueFalcon.delegates.add(this)
// By default this is AUTO.
blueFalcon.transportMethod = BluetoothDevice.TRANSPORT_LE
}

fun scan() {
Expand Down Expand Up @@ -58,6 +55,15 @@ class BluetoothService: BlueFalconDelegate {
blueFalcon.writeCharacteristic(bluetoothPeripheral, bluetoothCharacteristic, value)
}

fun readDescriptor(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic,
bluetoothCharacteristicDescriptor: BluetoothCharacteristicDescriptor
) {
log("btservice readDescriptor")
blueFalcon.readDescriptor(bluetoothPeripheral, bluetoothCharacteristic, bluetoothCharacteristicDescriptor)
}

override fun didDiscoverDevice(bluetoothPeripheral: BluetoothPeripheral) {
if (devices.firstOrNull {
it.bluetoothDevice.address == bluetoothPeripheral.bluetoothDevice.address
Expand All @@ -78,6 +84,13 @@ class BluetoothService: BlueFalconDelegate {
blueFalcon.changeMTU(bluetoothPeripheral, 250)
}

override fun didReadDescriptor(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristicDescriptor: BluetoothCharacteristicDescriptor
) {
characteristicDelegates[bluetoothCharacteristicDescriptor.characteristic.uuid]?.descriptorValueChanged(bluetoothCharacteristicDescriptor)
}

override fun didRssiUpdate(bluetoothPeripheral: BluetoothPeripheral) {
print("Rssi updated.")
}
Expand Down Expand Up @@ -105,4 +118,5 @@ interface BluetoothServiceConnectedDeviceDelegate {

interface BluetoothServiceCharacteristicDelegate {
fun characteristcValueChanged(bluetoothCharacteristic: BluetoothCharacteristic)
fun descriptorValueChanged(bluetoothCharacteristicDescriptor: BluetoothCharacteristicDescriptor)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package dev.bluefalcon.viewModels

import android.app.AlertDialog
import android.bluetooth.BluetoothGattCharacteristic
import dev.bluefalcon.BlueFalconApplication
import dev.bluefalcon.BluetoothPeripheral
import java.nio.charset.Charset
import android.text.InputType
import android.widget.EditText
import dev.bluefalcon.BluetoothCharacteristic
import dev.bluefalcon.*
import dev.bluefalcon.activities.DeviceServiceActivity
import dev.bluefalcon.services.BluetoothServiceCharacteristicDelegate
import java.util.*


class DeviceCharacteristicViewModel(
Expand All @@ -20,13 +19,26 @@ class DeviceCharacteristicViewModel(

var notify = false
val id = characteristic.characteristic.uuid
var descriptorValues: MutableMap<UUID, String> = mutableMapOf()
val descriptors = characteristic.descriptors

init {
BlueFalconApplication.instance.bluetoothService.characteristicDelegates[characteristic.characteristic.uuid] = this
}

fun value(): String? = characteristic.value

fun readDescriptorTapped() {
log("readDescriptorTapped number of descriptors: ${characteristic.descriptors.size}")
characteristic.descriptors.forEach { descriptor ->
BlueFalconApplication.instance.bluetoothService.readDescriptor(
device,
characteristic,
descriptor
)
}
}

fun readCharacteristicTapped() {
BlueFalconApplication.instance.bluetoothService.readCharacteristic(
device,
Expand Down Expand Up @@ -67,4 +79,10 @@ class DeviceCharacteristicViewModel(
deviceServiceViewModel.notifyValueChanged()
}

override fun descriptorValueChanged(bluetoothCharacteristicDescriptor: BluetoothCharacteristicDescriptor) {

descriptorValues[bluetoothCharacteristicDescriptor.uuid] = String(bluetoothCharacteristicDescriptor.value)
deviceServiceViewModel.notifyValueChanged()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class DeviceActivityUI(private val viewModel: DeviceViewModel) : AnkoComponent<D
}.onItemClick { _, _, index, _ ->
viewModel.deviceAdapter.getItem(index).let {
owner.startActivity<DeviceServiceActivity>(
"service" to viewModel.deviceAdapter.getItem(index),
"service" to viewModel.deviceAdapter.getItem(index).service,
"device" to viewModel.bluetoothPeripheral.bluetoothDevice
)
}
Expand Down
4 changes: 4 additions & 0 deletions examples/ios/Blue-Falcon.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
0014BA4D2436356F002037EB /* Descriptor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0014BA4C2436356F002037EB /* Descriptor.swift */; };
00F292BB23204BCC00CC1B2E /* BluetoothService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 00F292BA23204BCC00CC1B2E /* BluetoothService.swift */; };
00F292BD2320507600CC1B2E /* BluetoothScanningState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 00F292BC2320507600CC1B2E /* BluetoothScanningState.swift */; };
29BA30B3CD68DA1563FEDE72 /* Pods_Blue_FalconTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B526CB3F28C7FE940A5E947C /* Pods_Blue_FalconTests.framework */; };
Expand Down Expand Up @@ -54,6 +55,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
0014BA4C2436356F002037EB /* Descriptor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Descriptor.swift; sourceTree = "<group>"; };
00F292BA23204BCC00CC1B2E /* BluetoothService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BluetoothService.swift; sourceTree = "<group>"; };
00F292BC2320507600CC1B2E /* BluetoothScanningState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BluetoothScanningState.swift; sourceTree = "<group>"; };
12998B1BEDD755009B26AC20 /* Pods-Blue-Falcon.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Blue-Falcon.release.xcconfig"; path = "Target Support Files/Pods-Blue-Falcon/Pods-Blue-Falcon.release.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -258,6 +260,7 @@
children = (
49BD3E88231C575900960C6F /* BluetoothConnectionState.swift */,
00F292BC2320507600CC1B2E /* BluetoothScanningState.swift */,
0014BA4C2436356F002037EB /* Descriptor.swift */,
);
path = Models;
sourceTree = "<group>";
Expand Down Expand Up @@ -477,6 +480,7 @@
files = (
4902C50023119B76009D1416 /* DeviceCharacteristicCellViewModel.swift in Sources */,
498B07FE231C405800CA8EC1 /* ActivityIndicator.swift in Sources */,
0014BA4D2436356F002037EB /* Descriptor.swift in Sources */,
498B0800231C413800CA8EC1 /* Strings.swift in Sources */,
4902C4ED231127B4009D1416 /* DevicesViewModel.swift in Sources */,
4902C4F423112873009D1416 /* DevicesView.swift in Sources */,
Expand Down
14 changes: 14 additions & 0 deletions examples/ios/Blue-Falcon/Models/Descriptor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Descriptor.swift
// Blue-Falcon
//
// Created by Andrew Reed on 02/04/2020.
// Copyright © 2020 Andrew Reed. All rights reserved.
//

import Foundation

struct Descriptor {
let id = UUID()
let data: String
}
16 changes: 16 additions & 0 deletions examples/ios/Blue-Falcon/Services/BluetoothService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ class BluetoothService {
value: value
)
}

func readDescriptor(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: CBCharacteristic,
bluetoothDescriptor: CBDescriptor
) {
blueFalcon.readDescriptor(
bluetoothPeripheral: bluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic(characteristic: bluetoothCharacteristic),
bluetoothCharacteristicDescriptor: bluetoothDescriptor
)
}

func removeDetectedDeviceDelegate(delegate: BluetoothServiceDetectedDeviceDelegate) {
for (index, storedDelegate) in AppDelegate.instance.bluetoothService.detectedDeviceDelegates.enumerated() {
Expand Down Expand Up @@ -94,6 +106,10 @@ class BluetoothService {
}

extension BluetoothService: BlueFalconDelegate {

func didReadDescriptor(bluetoothPeripheral: BluetoothPeripheral, bluetoothCharacteristicDescriptor: CBDescriptor) {
print("BT Service didReadDescriptor -> \(bluetoothCharacteristicDescriptor.value)")
}

func didRssiUpdate(bluetoothPeripheral: BluetoothPeripheral) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ class DeviceCharacteristicCellViewModel: Identifiable, ObservableObject {
var id: CBUUID
let name: String
let characteristic: CBCharacteristic
let descriptors: [CBDescriptor]
var descriptorData: [Descriptor] {
descriptors.compactMap {
guard let data = $0.value as? Data else { return nil }
let dataString = String(decoding: data, as: UTF8.self)
return Descriptor(data: dataString)
}
}
let device: BluetoothPeripheral
@Published var notify: Bool = false
@Published var characterisicValue: String? = nil
Expand All @@ -26,6 +34,7 @@ class DeviceCharacteristicCellViewModel: Identifiable, ObservableObject {
self.name = characteristic.uuid.description
self.characteristic = characteristic
self.device = device
self.descriptors = characteristic.descriptors ?? []
}

func onAppear() {
Expand Down
Loading

0 comments on commit 9ad269a

Please sign in to comment.