Skip to content

Commit

Permalink
Merge commit '556c8d5a4dd7cfb80902614889f99538ba7a1a2c'
Browse files Browse the repository at this point in the history
* commit '556c8d5a4dd7cfb80902614889f99538ba7a1a2c':
  Using SparceArrays & Other Cleanup
  Public/Private Updates
  Throbber Fixed and XY3/XY2 Samples Calls Updated
  Simple Java List Works
  BLE Power Adjustments
  Update OtaUpdate.kt
  Updated OTA with new connection wrapper
  XY3 Fix & Start of Java Sample
  • Loading branch information
Bob Nies committed Feb 11, 2019
2 parents f5a4cd3 + 556c8d5 commit 1ec9ef0
Show file tree
Hide file tree
Showing 89 changed files with 1,741 additions and 623 deletions.
Binary file modified .DS_Store
Binary file not shown.
6 changes: 6 additions & 0 deletions ble-android-library/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11/"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry kind="output" path="bin/default"/>
</classpath>
2 changes: 1 addition & 1 deletion ble-android-library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ buildscript {
}

dependencies {
classpath "com.android.tools.build:gradle:3.3.0"
classpath 'com.android.tools.build:gradle:3.3.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.20"
classpath "com.github.dcendents:android-maven-gradle-plugin:$mavenVersion"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package network.xyo.ble.gatt
package network.xyo.ble

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
Expand All @@ -11,10 +11,38 @@ import java.util.concurrent.Executors
import kotlin.coroutines.AbstractCoroutineContextElement
import kotlin.coroutines.Continuation
import kotlin.coroutines.ContinuationInterceptor
import kotlin.coroutines.CoroutineContext

open class XYBluetoothBase(context: Context) : XYBase() {

enum class Status(val status: Short) {
Success (0x00),
UnknownBtLeCommand(0x01),
UnknownConnectionIdentifier(0x02),
AuthenticationFailure(0x03),
PinOrKeyMissing(0x06),
MemoryCapacityExceeded(0x07),
Timeout(0x08),
CommandDisallowed(0x0c),
InvalidBtLeCommandParameters(0x12),
RemoteUserTerminatedConnection(0x13),
RemoteDevTerminationFromLowResources(0x14),
RemoteDevTerminationFromPowerOff(0x15),
LocalHostTerminatedConnection(0x16),
UnsupportedRemoteFeature(0x1a),
InvalidLmpParameters(0x1e),
UnspecifiedError(0x1f),
LmpResponseTimeout(0x22),
LmpPduNotAllowed(0x24),
InstantPassed(0x28),
PairingWithUnitKeyUnsupported(0x29),
DifferentTransactionCollision(0x2a),
ControllerBusy(0x3a),
ConnIntervalUnacceptable(0x3b),
DirectedAdvertiserTimeout(0x3c),
TerminatedDueToMicFailure(0x3d),
FailedToEstablish(0x3e)
}

//we store this since on initial creation, the applicationContext may not yet be available
private val _context = context

Expand Down Expand Up @@ -82,16 +110,16 @@ open class XYBluetoothBase(context: Context) : XYBase() {
get() = bluetoothAdapter?.isOffloadedScanBatchingSupported ?: false


companion object {
companion object: XYBase() {
//this is the thread that all calls should happen on for gatt calls.
val BluetoothThread: CoroutineContext

init {
BluetoothThread = if (android.os.Build.VERSION.SDK_INT >= 26) {
internal val BluetoothThread = when {
(android.os.Build.VERSION.SDK_INT >= 26) -> {
Executors.newSingleThreadExecutor().asCoroutineDispatcher()
} else if (android.os.Build.VERSION.SDK_INT >= 21) {
}
(android.os.Build.VERSION.SDK_INT >= 21) -> {
Executors.newSingleThreadExecutor().asCoroutineDispatcher()
} else {
}
else -> {
//if the device is before 20, use the UI thread for the BLE calls
object : AbstractCoroutineContextElement(ContinuationInterceptor), ContinuationInterceptor {
override fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package network.xyo.ble
import android.os.Build
import network.xyo.core.XYBase

class CallByVersion : XYBase() {
class XYCallByVersion : XYBase() {

class Call(
val version: Int,
Expand All @@ -16,7 +16,7 @@ class CallByVersion : XYBase() {

private val calls = ArrayList<Call>()

fun add(version: Int, closure: () -> Unit): CallByVersion {
fun add(version: Int, closure: () -> Unit): XYCallByVersion {
calls.add(Call(version, closure))
return this
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package network.xyo.ble

abstract class XYPromise<T> {
open fun resolve(value: T?) {}
open fun reject(error: String) {}
}
104 changes: 53 additions & 51 deletions ble-android-library/src/main/kotlin/network/xyo/ble/ads/XYBleAd.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,55 @@ open class XYBleAd(buffer: ByteBuffer) : XYBase() {
val type: Byte
var data: ByteArray? = null

enum class AdTypes(val id: Short) {
Flags(0x01),
Incomplete16BitServiceUuids(0x02),
Complete16BitServiceUuids(0x03),
Incomplete32BitServiceUuids(0x04),
Complete32BitServiceUuids(0x05),
Incomplete128BitServiceUuids(0x06),
Complete128BitServiceUuids(0x07),
ShortenedLocalName(0x08),
CompleteLocalName(0x09),
TxPowerLevel(0x0a),
ClassOfDevice(0x0d),
SimplePairingHashC(0x0e),
SimplePairingHashC192(0x0e),
SimpleParingRandomizerR(0x0f),
SimpleParingRandomizerR192(0x0f),
DeviceId(0x10),
SecurityManagerTkValue(0x10),
SecurityManagerOutOfBandFlags(0x11),
SlaveConnectionIntervalRange(0x12),
ListOf16BitServiceSolicitationUuids(0x14),
ListOf128BitServiceSolicitationUuids(0x15),
ServiceData(0x16),
ServiceData16BitUuid(0x16),
PublicTargetAddress(0x17),
RandomTargetAddress(0x18),
Appearance(0x19),
AdvertisingInterval(0x1a),
LeBluetoothDeviceAddress(0x1b),
LeRole(0x1c),
SimpleParingHashC256(0x1d),
SimpleParingRandomizerR256(0x1e),
ListOf32BitServiceSolicitationUuids(0x1f),
ServiceData32BitUuid(0x20),
ServiceData128BitUuid(0x21),
LeSecureConnectionsConfirmationValue(0x22),
LeSecureConnectionsRandomValue(0x23),
Uri(0x24),
IndoorPositioning(0x25),
TransportDiscoveryData(0x26),
LeSupportedFeatures(0x27),
ChannelMapUpdateIndication(0x28),
PbAdv(0x29),
MeshMessage(0x2a),
MeshBeacon(0x2b),
ThreeDInformationData(0x3d),
ManufacturerSpecificData(0xff)
}

init {
if (size > 0) {
type = buffer.get()
Expand All @@ -26,6 +75,10 @@ open class XYBleAd(buffer: ByteBuffer) : XYBase() {
}
}

override fun equals(other: Any?): Boolean {
return hashCode() == other.hashCode()
}

override fun hashCode(): Int {
val p = 16777619
var hash = 216613626
Expand Down Expand Up @@ -62,55 +115,4 @@ open class XYBleAd(buffer: ByteBuffer) : XYBase() {
override fun toString(): String {
return "Type: $type, Bytes: ${data?.contentToString()}"
}

companion object {
enum class AdTypes(val id: Short) {
Flags(0x01),
Incomplete16BitServiceUuids(0x02),
Complete16BitServiceUuids(0x03),
Incomplete32BitServiceUuids(0x04),
Complete32BitServiceUuids(0x05),
Incomplete128BitServiceUuids(0x06),
Complete128BitServiceUuids(0x07),
ShortenedLocalName(0x08),
CompleteLocalName(0x09),
TxPowerLevel(0x0a),
ClassOfDevice(0x0d),
SimplePairingHashC(0x0e),
SimplePairingHashC192(0x0e),
SimpleParingRandomizerR(0x0f),
SimpleParingRandomizerR192(0x0f),
DeviceId(0x10),
SecurityManagerTkValue(0x10),
SecurityManagerOutOfBandFlags(0x11),
SlaveConnectionIntervalRange(0x12),
ListOf16BitServiceSolicitationUuids(0x14),
ListOf128BitServiceSolicitationUuids(0x15),
ServiceData(0x16),
ServiceData16BitUuid(0x16),
PublicTargetAddress(0x17),
RandomTargetAddress(0x18),
Appearance(0x19),
AdvertisingInterval(0x1a),
LeBluetoothDeviceAddress(0x1b),
LeRole(0x1c),
SimpleParingHashC256(0x1d),
SimpleParingRandomizerR256(0x1e),
ListOf32BitServiceSolicitationUuids(0x1f),
ServiceData32BitUuid(0x20),
ServiceData128BitUuid(0x21),
LeSecureConnectionsConfirmationValue(0x22),
LeSecureConnectionsRandomValue(0x23),
Uri(0x24),
IndoorPositioning(0x25),
TransportDiscoveryData(0x26),
LeSupportedFeatures(0x27),
ChannelMapUpdateIndication(0x28),
PbAdv(0x29),
MeshMessage(0x2a),
MeshBeacon(0x2b),
ThreeDInformationData(0x3d),
ManufacturerSpecificData(0xff)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ open class XY2BluetoothDevice(context: Context, scanResult: XYScanResult, hash:
val extendedControlService = ExtendedControlService(this)
val sensorService = SensorService(this)

override fun find(): Deferred<XYBluetoothResult<Int>> {
return controlService.buzzerSelect.set(2)
override fun find() = connection {
return@connection controlService.buzzerSelect.set(2).await()
}

override fun stopFind(): Deferred<XYBluetoothResult<Int>> {
return controlService.buzzerSelect.set(-1)
override fun stopFind() = connection {
return@connection controlService.buzzerSelect.set(-1).await()
}

override val prefix = "xy:ibeacon"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,28 +72,28 @@ open class XY3BluetoothDevice(context: Context, scanResult: XYScanResult, hash:

override val prefix = "xy:ibeacon"

override fun find(): Deferred<XYBluetoothResult<Int>> {
return controlService.buzzerSelect.set(2)
override fun find() = connection {
return@connection controlService.buzzerSelect.set(2).await()
}

override fun stopFind(): Deferred<XYBluetoothResult<Int>> {
return controlService.buzzerSelect.set(-1)
override fun stopFind() = connection {
return@connection controlService.buzzerSelect.set(-1).await()
}

override fun lock(): Deferred<XYBluetoothResult<ByteArray>> {
return basicConfigService.lock.set(DEFAULT_LOCK_CODE)
override fun lock() = connection {
return@connection basicConfigService.lock.set(DEFAULT_LOCK_CODE).await()
}

override fun unlock(): Deferred<XYBluetoothResult<ByteArray>> {
return basicConfigService.unlock.set(DEFAULT_LOCK_CODE)
override fun unlock() = connection {
return@connection basicConfigService.unlock.set(DEFAULT_LOCK_CODE).await()
}

override fun stayAwake(): Deferred<XYBluetoothResult<Int>> {
return extendedConfigService.registration.set(1)
override fun stayAwake() = connection {
return@connection extendedConfigService.registration.set(1).await()
}

override fun fallAsleep(): Deferred<XYBluetoothResult<Int>> {
return extendedConfigService.registration.set(0)
override fun fallAsleep() = connection {
return@connection extendedConfigService.registration.set(0).await()
}

override fun onDetect(scanResult: XYScanResult?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import android.bluetooth.BluetoothGatt
import android.bluetooth.BluetoothGattCallback
import android.bluetooth.BluetoothGattCharacteristic
import android.content.Context
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import network.xyo.ble.firmware.OtaFile
import network.xyo.ble.firmware.OtaUpdate
import network.xyo.ble.firmware.XY4OtaUpdate
import network.xyo.ble.firmware.XYOtaFile
import network.xyo.ble.firmware.XYOtaUpdate
import network.xyo.ble.gatt.peripheral.XYBluetoothError
import network.xyo.ble.gatt.peripheral.XYBluetoothResult
import network.xyo.ble.scanner.XYScanResult
Expand Down Expand Up @@ -39,7 +38,7 @@ open class XY4BluetoothDevice(context: Context, scanResult: XYScanResult, hash:

private var lastButtonPressTime = 0L

private var updater: OtaUpdate? = null
private var updater: XY4OtaUpdate? = null

private val buttonListener = object : BluetoothGattCallback() {
override fun onCharacteristicChanged(gatt: BluetoothGatt?, characteristic: BluetoothGattCharacteristic?) {
Expand Down Expand Up @@ -108,18 +107,18 @@ open class XY4BluetoothDevice(context: Context, scanResult: XYScanResult, hash:
}
}

override fun updateFirmware(filename: String, listener: OtaUpdate.Listener) {
val otaFile = OtaFile.getByFileName(filename)
val updater = OtaUpdate(this, otaFile)
override fun updateFirmware(folderName:String, filename: String, listener: XYOtaUpdate.Listener) {
val otaFile = XYOtaFile.getByName(folderName, filename)
val updater = XY4OtaUpdate(this, otaFile)

updater.addListener("XY4BluetoothDevice", listener)
updater.start()
}

override fun updateFirmware(stream: InputStream, listener: OtaUpdate.Listener) {
override fun updateFirmware(stream: InputStream, listener: XYOtaUpdate.Listener) {

val otaFile = OtaFile.getByFileStream(stream)
updater = OtaUpdate(this, otaFile)
val otaFile = XYOtaFile.getByStream(stream)
updater = XY4OtaUpdate(this, otaFile)

updater?.addListener("XY4BluetoothDevice", listener)
updater?.start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package network.xyo.ble.devices

import android.bluetooth.BluetoothDevice
import android.content.Context
import android.util.SparseArray
import network.xyo.ble.scanner.XYScanResult
import network.xyo.core.XYBase
import java.util.*
Expand All @@ -19,21 +20,23 @@ open class XYAppleBluetoothDevice(context: Context, device: BluetoothDevice, has

fun enable(enable: Boolean) {
if (enable) {
manufacturerToCreator[MANUFACTURER_ID] = creator
manufacturerToCreator.append(MANUFACTURER_ID, creator)
} else {
manufacturerToCreator.remove(MANUFACTURER_ID)
}
}

internal val typeToCreator = HashMap<Byte, XYCreator>()
internal val typeToCreator = SparseArray<XYCreator>()

internal val creator = object : XYCreator() {
override fun getDevicesFromScanResult(context: Context, scanResult: XYScanResult, globalDevices: ConcurrentHashMap<String, XYBluetoothDevice>, foundDevices: HashMap<String, XYBluetoothDevice>) {
for ((typeId, creator) in typeToCreator) {

for (i in 0 until typeToCreator.size()) {
val typeId = typeToCreator.keyAt(i)
val bytes = scanResult.scanRecord?.getManufacturerSpecificData(MANUFACTURER_ID)
if (bytes != null) {
if (bytes[0] == typeId) {
creator.getDevicesFromScanResult(context, scanResult, globalDevices, foundDevices)
if (bytes[0] == typeId.toByte()) {
typeToCreator.get(typeId)?.getDevicesFromScanResult(context, scanResult, globalDevices, foundDevices)
return
}
}
Expand Down
Loading

0 comments on commit 1ec9ef0

Please sign in to comment.