|
| 1 | +package com.mageddo.linux.bluetoothfix; |
| 2 | + |
| 3 | + |
| 4 | +import com.mageddo.commons.exec.CommandLines; |
| 5 | +import com.mageddo.commons.exec.ExecutionValidationFailedException; |
| 6 | +import com.mageddo.commons.lang.Threads; |
| 7 | +import lombok.extern.slf4j.Slf4j; |
| 8 | +import org.apache.commons.exec.CommandLine; |
| 9 | +import org.apache.commons.lang3.time.StopWatch; |
| 10 | + |
| 11 | +@Slf4j |
| 12 | +public class BluetoothConnector { |
| 13 | + public static final boolean PRINT_OUT = false; |
| 14 | + public static final int BLUETOOTH_POWER_ON_DELAY = 1000; |
| 15 | + private final int timeoutSecs = 10; |
| 16 | + |
| 17 | + public void connect(String deviceId) { |
| 18 | + |
| 19 | + if (this.isSoundDeviceConfigured(deviceId)) { |
| 20 | + log.info("status=bluetooth-device-already-configured-and-working, deviceId={}", deviceId); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + final StopWatch stopWatch = StopWatch.createStarted(); |
| 25 | + Occurrence status = null; |
| 26 | + do { |
| 27 | + |
| 28 | + stopWatch.split(); |
| 29 | + |
| 30 | + if (status != null) { |
| 31 | + switch (status) { |
| 32 | + case CONNECTED_BUT_SOUND_NOT_CONFIGURED: |
| 33 | + case ERROR_CONNECTION_BUSY: |
| 34 | + this.disconnect(deviceId); |
| 35 | + break; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + this.restartService(); |
| 40 | + status = this.connect0(deviceId); |
| 41 | + |
| 42 | + log.info( |
| 43 | + "status=tried, occurrence={}, time={}", |
| 44 | + status, stopWatch.getTime() - stopWatch.getSplitTime() |
| 45 | + ); |
| 46 | + Threads.sleep(1000); |
| 47 | + |
| 48 | + } while (status != Occurrence.CONNECTED); |
| 49 | + log.info( |
| 50 | + "status=successfullyConnected!, device={}, totalTime={}", |
| 51 | + deviceId, stopWatch.getTime() |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + boolean disconnect(String deviceId) { |
| 56 | + try { |
| 57 | + final CommandLines.Result result = CommandLines.exec( |
| 58 | + "bluetoothctl --timeout %d disconnect %s", timeoutSecs, deviceId |
| 59 | + ) |
| 60 | + .checkExecution(); |
| 61 | + log.info("status=disconnected, {}", result.toString(PRINT_OUT)); |
| 62 | + return true; |
| 63 | + } catch (ExecutionValidationFailedException e) { |
| 64 | + log.info("status=failedToDisconnect, {}", e.result() |
| 65 | + .toString(PRINT_OUT)); |
| 66 | + return false; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + CommandLines.Result restartService() { |
| 71 | + |
| 72 | + log.warn("systemctl will ask you for root password to restart bluetooth service ..."); |
| 73 | + |
| 74 | + final CommandLine cmd = new CommandLine("/bin/sh") |
| 75 | + .addArguments(new String[]{ |
| 76 | + "-c", |
| 77 | + "systemctl restart bluetooth.service", |
| 78 | + }, false); |
| 79 | + final CommandLines.Result result = CommandLines.exec(cmd) |
| 80 | + .checkExecution(); |
| 81 | + log.info("status=restarted, {}", result.toString(PRINT_OUT)); |
| 82 | + Threads.sleep(BLUETOOTH_POWER_ON_DELAY); // wait some time to bluetooth power on |
| 83 | + return result; |
| 84 | + } |
| 85 | + |
| 86 | + boolean isConnected(String deviceId) { |
| 87 | + final CommandLines.Result result = CommandLines.exec( |
| 88 | + "bluetoothctl info %s", deviceId |
| 89 | + ) |
| 90 | + .checkExecution(); |
| 91 | + final String out = result.getOutAsString(); |
| 92 | + if (out.contains("Connected: yes")) { |
| 93 | + return true; |
| 94 | + } else if (out.contains("Connected: no")) { |
| 95 | + return false; |
| 96 | + } else { |
| 97 | + throw new IllegalStateException(String.format("cant check if it's connected: %s", out)); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + Occurrence connect0(String deviceId) { |
| 102 | + try { |
| 103 | + log.info("status=tryConnecting, device={}", deviceId); |
| 104 | + final CommandLines.Result result = CommandLines |
| 105 | + .exec( |
| 106 | + "bluetoothctl --timeout %d connect %s", timeoutSecs, deviceId |
| 107 | + ) |
| 108 | + .checkExecution(); |
| 109 | + final BluetoothConnector.Occurrence occurrence = OccurrenceParser.parse(result); |
| 110 | + if (occurrence != null) { |
| 111 | + return occurrence; |
| 112 | + } |
| 113 | + final Occurrence occur = this.connectionOccurrenceCheck(deviceId); |
| 114 | + log.info("status=done, occurrence={}", occur); |
| 115 | + return occur; |
| 116 | + } catch (ExecutionValidationFailedException e) { |
| 117 | + return OccurrenceParser.parse(e.result()); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + Occurrence connectionOccurrenceCheck(String deviceId) { |
| 122 | + final boolean connected = this.isConnected(deviceId); |
| 123 | + if (connected) { |
| 124 | + if (this.isSoundDeviceConfigured(deviceId)) { |
| 125 | + return Occurrence.CONNECTED; |
| 126 | + } |
| 127 | + return Occurrence.CONNECTED_BUT_SOUND_NOT_CONFIGURED; |
| 128 | + } else { |
| 129 | + return Occurrence.DISCONNECTED; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * A device like the following must be displayed when bluetooth audio is working |
| 135 | + * bluez_sink.94_DB_56_F5_78_41.a2dp_sink |
| 136 | + */ |
| 137 | + boolean isSoundDeviceConfigured(String deviceId) { |
| 138 | + final String audioSinkId = String.format( |
| 139 | + "bluez_sink.%s.a2dp_sink", deviceId.replaceAll(":", "_") |
| 140 | + ); |
| 141 | + final CommandLine cmd = new CommandLine("/bin/sh") |
| 142 | + .addArguments(new String[]{"-c", "pactl list | grep 'Sink'"}, false); |
| 143 | + |
| 144 | + final CommandLines.Result result = CommandLines.exec(cmd) |
| 145 | + .checkExecution(); |
| 146 | + |
| 147 | + final boolean found = result |
| 148 | + .getOutAsString() |
| 149 | + .contains(audioSinkId); |
| 150 | + |
| 151 | + log.info("found={}, {}", found, result.toString(PRINT_OUT)); |
| 152 | + return found; |
| 153 | + |
| 154 | + } |
| 155 | + |
| 156 | + public enum Occurrence { |
| 157 | + ERROR_CONNECTION_BUSY, |
| 158 | + CONNECTED, |
| 159 | + DISCONNECTED, |
| 160 | + ERROR_UNKNOWN, |
| 161 | + CONNECTED_BUT_SOUND_NOT_CONFIGURED; |
| 162 | + } |
| 163 | + |
| 164 | +} |
0 commit comments