Skip to content

Commit

Permalink
Refactor for Android 13 methods being deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
weliem committed Oct 28, 2023
1 parent aee8d6e commit 31f9aa4
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 79 deletions.
45 changes: 35 additions & 10 deletions blessed/src/main/java/com/welie/blessed/BluetoothCentral.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Martijn van Welie
* Copyright (c) 2023 Martijn van Welie
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -22,17 +22,37 @@
*/
package com.welie.blessed

import android.annotation.SuppressLint
import android.bluetooth.BluetoothDevice
import java.util.*

/**
* This class represent a remote Central
*/
class BluetoothCentral internal constructor(address: String, name: String?) {
val address: String
private val name: String?
@SuppressLint("MissingPermission")
@Suppress("unused")
class BluetoothCentral internal constructor(val device: BluetoothDevice) {
var currentMtu = 23
fun getName(): String {
return name ?: ""

val address: String
get() = device.address
val name: String
get() = if (device.name == null) "" else device.name
val bondState: BondState
get() = BondState.fromValue(device.bondState)

/**
* Create a bond
*/
fun createBond(): Boolean {
return device.createBond()
}

/**
* Confirm pairing
*/
fun setPairingConfirmation(confirm: Boolean): Boolean {
return device.setPairingConfirmation(confirm)
}

/**
Expand All @@ -41,16 +61,21 @@ class BluetoothCentral internal constructor(address: String, name: String?) {
* This value is derived from the current negotiated MTU or the maximum characteristic length (512)
*/
fun getMaximumWriteValueLength(writeType: WriteType): Int {
Objects.requireNonNull(writeType, "writetype is null")
return when (writeType) {
WriteType.WITH_RESPONSE -> 512
WriteType.SIGNED -> currentMtu - 15
else -> currentMtu - 3
}
}

init {
this.address = Objects.requireNonNull(address, "address is null")
this.name = name
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || javaClass != other.javaClass) return false
val that = other as BluetoothCentral
return device.address == that.device.address
}

override fun hashCode(): Int {
return Objects.hash(device)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ class BluetoothCentralManager(private val context: Context) {
Logger.d(TAG, "peripheral with address '%s' not in Bluetooth cache, autoconnecting by scanning", peripheral.address)
scannedPeripherals.remove(peripheral.address)
unconnectedPeripherals[peripheral.address] = peripheral
autoConnectPeripheralByScan(peripheral.address, NULL())
autoConnectPeripheralByScan(peripheral.address)
return
}

Expand All @@ -520,7 +520,7 @@ class BluetoothCentralManager(private val context: Context) {
}
}

private fun autoConnectPeripheralByScan(peripheralAddress: String, peripheralCallback: BluetoothPeripheralCallback) {
private fun autoConnectPeripheralByScan(peripheralAddress: String) {
if (reconnectPeripheralAddresses.contains(peripheralAddress)) {
Logger.w(TAG, "peripheral already on list for reconnection")
return
Expand Down
Loading

0 comments on commit 31f9aa4

Please sign in to comment.