|
| 1 | +package net.sharksystem.asap.android.lora; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.util.Log; |
| 5 | + |
| 6 | +import net.sharksystem.asap.android.service.ASAPService; |
| 7 | +import net.sharksystem.asap.android.service.MacLayerEngine; |
| 8 | + |
| 9 | +import android.bluetooth.BluetoothAdapter; |
| 10 | +import android.bluetooth.BluetoothDevice; |
| 11 | +import android.bluetooth.BluetoothSocket; |
| 12 | + |
| 13 | +import java.io.BufferedReader; |
| 14 | +import java.io.IOException; |
| 15 | +import java.io.InputStreamReader; |
| 16 | +import java.util.UUID; |
| 17 | + |
| 18 | +public class LoRaEngine extends MacLayerEngine { |
| 19 | + |
| 20 | + private static final String CLASS_LOG_TAG = "ASAPLoRaEngine"; |
| 21 | + |
| 22 | + |
| 23 | + private static LoRaEngine engine = null; |
| 24 | + private BluetoothDevice btDevice = null; |
| 25 | + |
| 26 | + /** |
| 27 | + * - Aufbau Verbindung zu BLE o. Bluetooth UART Schnittstelle |
| 28 | + * - Prüfen der Verbindung zum LoRa Board AT <-> AT+OK |
| 29 | + * |
| 30 | + * @param ASAPService |
| 31 | + * @param context |
| 32 | + * @return |
| 33 | + */ |
| 34 | + public static LoRaEngine getASAPLoRaEngine(ASAPService ASAPService, |
| 35 | + Context context) { |
| 36 | + if (LoRaEngine.engine == null) { |
| 37 | + LoRaEngine.engine = new LoRaEngine(ASAPService, context); |
| 38 | + } |
| 39 | + |
| 40 | + return LoRaEngine.engine; |
| 41 | + } |
| 42 | + |
| 43 | + public static LoRaEngine getASAPLoRaEngine() { |
| 44 | + return LoRaEngine.engine; |
| 45 | + } |
| 46 | + |
| 47 | + public LoRaEngine(ASAPService asapService, Context context) { |
| 48 | + super(asapService, context); |
| 49 | + } |
| 50 | + |
| 51 | + private void initBluetooth() throws ASAPLoRaException { |
| 52 | + BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); |
| 53 | + btAdapter.cancelDiscovery(); |
| 54 | + |
| 55 | + for (BluetoothDevice btDevice : btAdapter.getBondedDevices()) { |
| 56 | + if (btDevice.getName().indexOf("ASAP-LoRa") == 0) { //TODO: What about more than 1 paired ASAP-LoRa Board? Or 1 avail and 1 unavail? |
| 57 | + this.btDevice = btDevice; |
| 58 | + break; |
| 59 | + } |
| 60 | + } |
| 61 | + if (this.btDevice == null) |
| 62 | + throw new ASAPLoRaException("Please pair to an ASAP-LoRa Board before Starting LoRa!"); |
| 63 | + |
| 64 | + /** |
| 65 | + * uses UUID of Serial Devices for now - https://www.bluetooth.com/specifications/assigned-numbers/ |
| 66 | + * Might be a good idea to move another uuid to define a ASAP-LoRa Node |
| 67 | + */ |
| 68 | + try { |
| 69 | + BluetoothSocket btSocket = btDevice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); |
| 70 | + btSocket.connect(); |
| 71 | + btSocket.getOutputStream().write("AT".getBytes()); |
| 72 | + |
| 73 | + while(true){ //TODO: Do not Activewait... |
| 74 | + if(btSocket.getInputStream().available() > 0) { |
| 75 | + BufferedReader br = new BufferedReader(new InputStreamReader(btSocket.getInputStream())); |
| 76 | + StringBuilder sb = new StringBuilder(btSocket.getInputStream().available()); |
| 77 | + do { |
| 78 | + sb.append(br.readLine()).append("\n"); |
| 79 | + } while(br.ready()); |
| 80 | + Log.i(this.CLASS_LOG_TAG, "LoRa Board said: "+sb.toString()); |
| 81 | + break; |
| 82 | + } |
| 83 | + } |
| 84 | + } catch (IOException e) { |
| 85 | + e.printStackTrace(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void start() { |
| 91 | + Log.i(this.CLASS_LOG_TAG, "MacLayerEngine.start() called"); |
| 92 | + try { |
| 93 | + this.initBluetooth(); |
| 94 | + } catch (ASAPLoRaException e) { |
| 95 | + Log.e(this.CLASS_LOG_TAG, e.getMessage()); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void stop() { |
| 101 | + Log.i(this.CLASS_LOG_TAG, "MacLayerEngine.stop() called"); |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public boolean tryReconnect() { |
| 107 | + Log.i(this.CLASS_LOG_TAG, "MacLayerEngine.tryReconnect() called"); |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void checkConnectionStatus() { |
| 113 | + Log.i(this.CLASS_LOG_TAG, "MacLayerEngine.checkConnectionStatus() called"); |
| 114 | + |
| 115 | + } |
| 116 | +} |
0 commit comments