@@ -35,9 +35,23 @@ replace_value_at_position() {
3535# Function
3636check_presence () {
3737 TYPE=" $1 " # BLE MAC, LN
38- MATCH=" $2 "
38+ BLE_LN=" $2 "
39+ BLE_MAC=" $3 "
3940
4041 CURRENT_TIME_EPOCH=$( date +%s)
42+ MATCH=" ($BLE_LN |$BLE_MAC )"
43+
44+ if [ $BLE_MAC == " FF:FF:FF:FF:FF:FF" ]; then
45+ log_debug " check_presence; looking BLE_MAC for BLE_LN:$BLE_LN "
46+ if grepOutput=$( echo " ${BLTCTL_OUT} " | grep $BLE_LN | tail -1) ; then
47+ BLE_MAC=$( echo $grepOutput | grep -Eo $MAC_REGEX )
48+ log_info " check_presence; found BLE MAC addr:$BLE_MAC for BLE_LN:$BLE_LN "
49+ else
50+ log_debug " check_presence; did not find BLE MAC addr for BLE_LN:$BLE_LN "
51+ fi
52+ fi
53+ # return value to calling function
54+ echo $BLE_MAC
4155
4256 if echo " ${BLTCTL_OUT} " | grep -Eq " $MATCH " ; then
4357 log_info " VIN $VIN $TYPE $MATCH presence detected"
@@ -85,9 +99,20 @@ bluetoothctl_read() {
8599 # Read BLE data from bluetoothctl or an input file
86100 if [ -z $BLECTL_FILE_INPUT ]; then
87101 log_debug " Launching bluetoothctl to check for BLE presence"
88- BLECTL_TIMEOUT=11
89102 set +e
90- BLTCTL_OUT=$( bluetoothctl --timeout $BLECTL_TIMEOUT scan on 2>&1 | grep -v DEL)
103+ BLTCTL_OUT=$( {
104+ for bltctlCommand in " power on" devices " scan on" ; do
105+ echo " $bltctlCommand "
106+ sleep 0.2
107+ done
108+
109+ # scan for 18 seconds (Tesla adverstisement each ~9s)
110+ sleep 18
111+
112+ echo " scan off"
113+ echo " power off"
114+ echo " exit"
115+ } | bluetoothctl)
91116 set -e
92117 else
93118 # Read from file, great for testing w/ no Bluetooth adapter
@@ -132,39 +157,16 @@ listen_to_ble() {
132157 MQTT_TOPIC=" tesla_ble/$VIN /binary_sensor/presence"
133158
134159 # Check the presence using both MAC Addr and BLE Local Name
135- log_debug " $( echo " $BLTCTL_OUT " | grep -E " ($BLE_MAC |$BLE_LN )" ) "
136- check_presence " BLE MAC & LN" " ($BLE_MAC |$BLE_LN )"
160+ log_debug " BLTCTL_OUT:$( echo " $BLTCTL_OUT " | grep -E " ($BLE_MAC |$BLE_LN )" ) "
161+ macAddr=$( check_presence " BLE LN & MAC" $BLE_LN $BLE_MAC )
162+
163+ # Add the MAC address to BLE_MAC_LIST if it's not already present at the position
164+ if [ " $macAddr " != " $BLE_MAC " ]; then
165+ eval " BLE_MAC_LIST=\$ (echo \$ BLE_MAC_LIST | awk '{\$ ${position} =\" $macAddr \" ; print}')"
166+ log_debug " listen_to_ble; BLE_MAC_LIST:$BLE_MAC_LIST "
167+ fi
137168
138169 done
139170 sleep $PRESENCE_DETECTION_LOOP_DELAY
140171 done
141172}
142-
143- # ## scanBLEforMACaddr
144- # #
145- # Uses BLE Local Name derived from the VIN to match a MAC addr in the output
146- # # of the command bluetoothctl "devices" and "scan on"
147- # ##
148- scanBLEforMACaddr () {
149- # copied from legacy "scan_bluetooth" function. To decide if still relevant
150- # note there is this PR https://github.com/tesla-local-control/tesla-local-control-addon/pull/32
151- # quite old, but has the principles for auto populating the BLE MAC Address with only the VIN
152- vin=$1
153-
154- mac_regex=' ([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})'
155-
156- ble_ln=$( vinToBLEln $vin )
157-
158- log_info " Looking for vin:$vin in the BLE cache that matches ble_ln:$ble_ln "
159- if ! bltctl_out=$( bluetoothctl --timeout 2 devices | grep $ble_ln | grep -Eo $mac_regex ) ; then
160- log_notice " Couldn't find a match in the cache for ble_ln:$ble_ln for vin:$vin "
161- # Look for a BLE adverstisement matching ble_ln
162- log_notice " Scanning (10 seconds) for BLE advertisement that matches ble_ln:$ble_ln vin:$vin "
163- if ! bltctl_out=$( bluetoothctl --timeout 10 scan on | grep $ble_ln | grep -Eo $mac_regex ) ; then
164- log_notice " Couldn't find a BLE advertisement for ble_ln:$ble_ln vin:$vin "
165- return 1
166- fi
167- fi
168- echo $bltctl_out
169- return 0
170- }
0 commit comments