View Single Post
Old 04-21-2025, 05:10 PM   #5
hanginwithdaddo
Enthusiast
hanginwithdaddo doesn't litterhanginwithdaddo doesn't litter
 
hanginwithdaddo's Avatar
 
Posts: 32
Karma: 122
Join Date: Apr 2025
Location: Pennsylvania, USA
Device: Kindle Signature, 12th Gen.
Something else that I've noticed is that the kindle will disconnect from the audio device after it displays the screen saver. I've had to add a cron job that runs every 5 minutes to re-establish the connection so that it's always available and my Bluetooth speaker (which is a portable one that too will go to sleep if not connected too for a period of a few minutes) doesn't go into a deep sleep. I also invoke this same script when I'm about to play media (I play chimes like a grandfather clock) to ensure that the audio device is connected. Below is a copy of the script I'm using.

The import of scripts via "source" are my way of configuring the script with various variables (in all caps) that the script relies on. I'll include these below after the main script.

btconnect.sh:

Code:
#!/bin/bash
IFS=" 	
"
PATH="/bin:/usr/bin:/sbin:/usr/sbin"
CMD=$(basename $0)
CMDPATH=$(dirname "$0")
if [[ "${CMDPATH}" == "" ]]; then CMDPATH="./"; fi
CMDPATH=$(realpath "${CMDPATH}")

source "${CMDPATH}/../config/config.sh"
source "${CMDPATH}/../config/btconfig.sh"
source "${CMDPATH}/shLib/fn_logger.sh"

logfile="${LOGDIR}/btconnect"
connectedDeviceName=$(lipc-get-prop com.lab126.btfd BTconnectedDevName 2>/dev/null)
if [[ "${DEBUG}" == 1 ]]; then (fn_loginfo "${CMD}" "BTDEVICE_NAME = \"${BTDEVICE_NAME}\", connectedDeviceName = \"$connectedDeviceName\"") 2>> "${logfile}"; fi

if [[ "${connectedDeviceName}" != "${BTDEVICE_NAME}" ]]
then
    # Determine if the device is actively charging. It's
    # possible that the device can be plugged in and isCharging
    # be 0 (e.g., the battery is at 99% charged), meaning the
    # device is allowing itself to discharge before entering a
    # charing state again, so as to protect the battery.
    #
    # Therefore, we check both the charging indicator and battery level
    # when determining whether or not we should prevent the device
    # from suspending.
    #
    # On startup, battLevel could be "" and waiting for a battLevelChanged
    # event could be futile, since the battery level might not change.
    # Therefore, just busy wait for the device to record one.
    battLevel=$(lipc-get-prop com.lab126.powerd battLevel 2>/dev/null)
    while [[ -z "${battLevel}" ]]
    do
        if [[ "${DEBUG}" == 1 ]]; then (fn_loginfo "${CMD}" "waiting for battery level to be assigned") 2>> "${logfile}"; fi
	sleep 10
        battLevel=$(lipc-get-prop com.lab126.powerd battLevel 2>/dev/null)
    done

    # The same might be said of the isCharging property.
    charging=$(lipc-get-prop com.lab126.powerd isCharging 2>/dev/null)
    while [[ -z "${charging}" ]]
    do
        if [[ "${DEBUG}" == 1 ]]; then (fn_loginfo "${CMD}" "waiting for charging property to be assigned") 2>> "${logfile}"; fi
        sleep 10
        charging=$(lipc-get-prop com.lab126.powerd isCharging 2>/dev/null)
    done

    # Only establish a bluetooth connection if we are actively charging
    # or the battery level is greater than the threshold that we've
    # defined.
    if [[ "${charging}" -eq 1 || "${battLevel}" -ge "${BATTLEVELTHRESHOLD}" ]]
    then
        # Ensure that the desired Bluetooth audio device is connected.
        if [[ "${DEBUG}" == 1 ]]; then (fn_loginfo "${CMD}" "connecting to \"${BTDEVICE_NAME}\"") 2>> "${logfile}"; fi
        lipc-set-prop com.lab126.btfd Connect "${BTDEVICE_MACADDR}"

	# The following does not seem to have any effect. I thought maybe
        # it might help keep the Bluetooth connection alive all the time.
        #
        # lipc-set-prop com.lab126.btfd ensureBTconnection 1

        sleep 5		# Give the device a chance to connect.

        # else, it's already connected.
    fi
fi
config.sh:
Code:
#!/bin/bash
#
# This file is generated automatically. Please do not edit.
DEBUG=1
PLAYER="/usr/bin/mplay"
JQ="/mnt/us/daddo/bin/jq-linux-armhf"
HASEIPS_V2="true"
EIPS="/usr/sbin/eips_v2"
FBINK="/mnt/us/extensions/MRInstaller/bin/KHF/fbink"
LOGDIR="/mnt/us/daddo/log"

# If the device's battery level goes below the value below we
# will allow the device to suspend. Otherwise, we will
# abort any suspend requests if the screen saver is
# visible. The device is not always in a charging state
# even when plugged in, so we cannot rely on the charging
# state alone to know whether to keep the device from
# suspending.
#
# This threshold also applies to such activities as keeping the
# bluetooth connection alive. If the device is not charing and
# the batter level falls below this threshold, we won't maintain
# a bluetooth connection.
BATTLEVELTHRESHOLD=97
btconfig.sh:
Code:
#!/bin/bash
#
# See the following file for the MAC address and
# name to use when connecting bluetooth devices.
# This file lists all the devices that are known
# and previously connected.
#
# /var/local/zbluetooth/bt_config.conf
#
# Look specifically at the first line of each
# deviced section and the "Name" entry.
#
# Example:
#
# [49:41:7e:db:ad:f3]
# Timestamp = 1745019543
# Name = EWA Audio A106Pro
# DevClass = 2360324
# DevType = 1
# AddrType = 0
# Manufacturer = 0
# LmpVer = 0
# LmpSubVer = 0
# LinkKeyType = 4
# PinLength = 0
# LinkKey = f325a97f6c37c9baef86f828c40a8f85
# Service = 0000111e-0000-1000-8000-00805f9b34fb 0000110b-0000-1000-8000-00805f9b3
# AvdtpVersion = 259
#
#
#BTDEVICE_MACADDR="2c:71:ff:e2:d9:cd" # echo
#BTDEVICE_NAME="Echo Dot-0A0"

BTDEVICE_MACADDR="49:41:7e:db:ad:f3"  # small speaker
BTDEVICE_NAME="EWA Audio A106Pro"
fn_logger.sh:
Code:
function fn_logerr() {
    local commandAndMethod="$1"
    local message="$2"
    local exception="$3"
    message="$(date) ERROR ${commandAndMethod} - ${message}"
    if [[ ! -z "${exception}" ]]
    then
        message+="; Exception: ${exception}"
    fi
    echo "${message}" >&2
}

function fn_loginfo() {
    local commandAndMethod=$1
    local info=$2
    echo "$(date) INFO ${commandAndMethod}: ${info}" >&2
}

function fn_logwarning() {
    local commandAndMethod=$1
    local warning=$2
    echo "$(date) WARNING ${commandAndMethod}: ${warning}" >&2
}
hanginwithdaddo is offline   Reply With Quote