RS ble cleanup

This commit is contained in:
Milos Kozak 2018-06-16 13:18:00 +02:00
parent 080bca6a8a
commit f14ba554f0

View file

@ -20,8 +20,6 @@ import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
@ -45,11 +43,11 @@ public class BLEComm {
private static final long WRITE_DELAY_MILLIS = 50; private static final long WRITE_DELAY_MILLIS = 50;
public static String UART_READ_UUID = "0000fff1-0000-1000-8000-00805f9b34fb"; private static String UART_READ_UUID = "0000fff1-0000-1000-8000-00805f9b34fb";
public static String UART_WRITE_UUID = "0000fff2-0000-1000-8000-00805f9b34fb"; private static String UART_WRITE_UUID = "0000fff2-0000-1000-8000-00805f9b34fb";
private byte PACKET_START_BYTE = (byte) 0xA5; private final byte PACKET_START_BYTE = (byte) 0xA5;
private byte PACKET_END_BYTE = (byte) 0x5A; private final byte PACKET_END_BYTE = (byte) 0x5A;
private static BLEComm instance = null; private static BLEComm instance = null;
public static BLEComm getInstance(DanaRSService service) { public static BLEComm getInstance(DanaRSService service) {
@ -58,16 +56,13 @@ public class BLEComm {
return instance; return instance;
} }
private final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();
private ScheduledFuture<?> scheduledDisconnection = null; private ScheduledFuture<?> scheduledDisconnection = null;
private DanaRS_Packet processsedMessage = null; private DanaRS_Packet processsedMessage = null;
private ArrayList<byte[]> mSendQueue = new ArrayList<>(); private final ArrayList<byte[]> mSendQueue = new ArrayList<>();
private BluetoothManager mBluetoothManager = null; private BluetoothManager mBluetoothManager = null;
private BluetoothAdapter mBluetoothAdapter = null; private BluetoothAdapter mBluetoothAdapter = null;
private BluetoothDevice mBluetoothDevice = null;
private String mBluetoothDeviceAddress = null;
private String mBluetoothDeviceName = null; private String mBluetoothDeviceName = null;
private BluetoothGatt mBluetoothGatt = null; private BluetoothGatt mBluetoothGatt = null;
@ -79,7 +74,7 @@ public class BLEComm {
private DanaRSService service; private DanaRSService service;
BLEComm(DanaRSService service) { private BLEComm(DanaRSService service) {
this.service = service; this.service = service;
initialize(); initialize();
} }
@ -138,15 +133,13 @@ public class BLEComm {
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
if (device == null) { if (device == null) {
log.debug("Device not found. Unable to connect."); log.debug("Device not found. Unable to connect from: " + from);
return false; return false;
} }
log.debug("Trying to create a new connection."); log.debug("Trying to create a new connection from: " + from);
mBluetoothGatt = device.connectGatt(service.getApplicationContext(), false, mGattCallback); mBluetoothGatt = device.connectGatt(service.getApplicationContext(), false, mGattCallback);
setCharacteristicNotification(getUARTReadBTGattChar(), true); setCharacteristicNotification(getUARTReadBTGattChar(), true);
mBluetoothDevice = device;
mBluetoothDeviceAddress = address;
mBluetoothDeviceName = device.getName(); mBluetoothDeviceName = device.getName();
return true; return true;
} }
@ -174,7 +167,7 @@ public class BLEComm {
SystemClock.sleep(2000); SystemClock.sleep(2000);
} }
public void close() { public synchronized void close() {
log.debug("BluetoothAdapter close"); log.debug("BluetoothAdapter close");
if (mBluetoothGatt == null) { if (mBluetoothGatt == null) {
return; return;
@ -184,15 +177,8 @@ public class BLEComm {
mBluetoothGatt = null; mBluetoothGatt = null;
} }
public BluetoothDevice getConnectDevice() {
return mBluetoothDevice;
}
public String getConnectDeviceAddress() { private String getConnectDeviceName() {
return mBluetoothDeviceAddress;
}
public String getConnectDeviceName() {
return mBluetoothDeviceName; return mBluetoothDeviceName;
} }
@ -229,19 +215,12 @@ public class BLEComm {
public void onCharacteristicChanged(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { public void onCharacteristicChanged(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
log.debug("onCharacteristicChanged" + (characteristic != null ? ":" + DanaRS_Packet.toHexString(characteristic.getValue()) : "")); log.debug("onCharacteristicChanged" + (characteristic != null ? ":" + DanaRS_Packet.toHexString(characteristic.getValue()) : ""));
addToReadBuffer(characteristic.getValue()); addToReadBuffer(characteristic.getValue());
new Thread(new Runnable() { new Thread(() -> readDataParsing()).start();
@Override
public void run() {
readDataParsing();
}
}).start();
} }
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
log.debug("onCharacteristicWrite" + (characteristic != null ? ":" + DanaRS_Packet.toHexString(characteristic.getValue()) : "")); log.debug("onCharacteristicWrite" + (characteristic != null ? ":" + DanaRS_Packet.toHexString(characteristic.getValue()) : ""));
new Thread(new Runnable() { new Thread(() -> {
@Override
public void run() {
synchronized (mSendQueue) { synchronized (mSendQueue) {
// after message sent, check if there is the rest of the message waiting and send it // after message sent, check if there is the rest of the message waiting and send it
if (mSendQueue.size() > 0) { if (mSendQueue.size() > 0) {
@ -250,12 +229,11 @@ public class BLEComm {
writeCharacteristic_NO_RESPONSE(getUARTWriteBTGattChar(), bytes); writeCharacteristic_NO_RESPONSE(getUARTWriteBTGattChar(), bytes);
} }
} }
}
}).start(); }).start();
} }
}; };
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) { private synchronized void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
log.debug("setCharacteristicNotification"); log.debug("setCharacteristicNotification");
if ((mBluetoothAdapter == null) || (mBluetoothGatt == null)) { if ((mBluetoothAdapter == null) || (mBluetoothGatt == null)) {
log.debug("BluetoothAdapter not initialized_ERROR"); log.debug("BluetoothAdapter not initialized_ERROR");
@ -266,7 +244,7 @@ public class BLEComm {
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
} }
public void readCharacteristic(BluetoothGattCharacteristic characteristic) { public synchronized void readCharacteristic(BluetoothGattCharacteristic characteristic) {
log.debug("readCharacteristic"); log.debug("readCharacteristic");
if ((mBluetoothAdapter == null) || (mBluetoothGatt == null)) { if ((mBluetoothAdapter == null) || (mBluetoothGatt == null)) {
log.debug("BluetoothAdapter not initialized_ERROR"); log.debug("BluetoothAdapter not initialized_ERROR");
@ -277,9 +255,8 @@ public class BLEComm {
mBluetoothGatt.readCharacteristic(characteristic); mBluetoothGatt.readCharacteristic(characteristic);
} }
public void writeCharacteristic_NO_RESPONSE(final BluetoothGattCharacteristic characteristic, final byte[] data) { private synchronized void writeCharacteristic_NO_RESPONSE(final BluetoothGattCharacteristic characteristic, final byte[] data) {
new Thread(new Runnable() { new Thread(() -> {
public void run() {
SystemClock.sleep(WRITE_DELAY_MILLIS); SystemClock.sleep(WRITE_DELAY_MILLIS);
if ((mBluetoothAdapter == null) || (mBluetoothGatt == null)) { if ((mBluetoothAdapter == null) || (mBluetoothGatt == null)) {
@ -293,25 +270,24 @@ public class BLEComm {
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE); characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
log.debug("writeCharacteristic:" + DanaRS_Packet.toHexString(data)); log.debug("writeCharacteristic:" + DanaRS_Packet.toHexString(data));
mBluetoothGatt.writeCharacteristic(characteristic); mBluetoothGatt.writeCharacteristic(characteristic);
}
}).start(); }).start();
} }
public BluetoothGattCharacteristic getUARTReadBTGattChar() { private BluetoothGattCharacteristic getUARTReadBTGattChar() {
if (UART_Read == null) { if (UART_Read == null) {
UART_Read = new BluetoothGattCharacteristic(UUID.fromString(UART_READ_UUID), BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY, 0); UART_Read = new BluetoothGattCharacteristic(UUID.fromString(UART_READ_UUID), BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY, 0);
} }
return UART_Read; return UART_Read;
} }
public BluetoothGattCharacteristic getUARTWriteBTGattChar() { private BluetoothGattCharacteristic getUARTWriteBTGattChar() {
if (UART_Write == null) { if (UART_Write == null) {
UART_Write = new BluetoothGattCharacteristic(UUID.fromString(UART_WRITE_UUID), BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE, 0); UART_Write = new BluetoothGattCharacteristic(UUID.fromString(UART_WRITE_UUID), BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE, 0);
} }
return UART_Write; return UART_Write;
} }
public List<BluetoothGattService> getSupportedGattServices() { private List<BluetoothGattService> getSupportedGattServices() {
log.debug("getSupportedGattServices"); log.debug("getSupportedGattServices");
if ((mBluetoothAdapter == null) || (mBluetoothGatt == null)) { if ((mBluetoothAdapter == null) || (mBluetoothGatt == null)) {
log.debug("BluetoothAdapter not initialized_ERROR"); log.debug("BluetoothAdapter not initialized_ERROR");
@ -329,7 +305,7 @@ public class BLEComm {
if (gattServices == null) { if (gattServices == null) {
return; return;
} }
String uuid = null; String uuid;
for (BluetoothGattService gattService : gattServices) { for (BluetoothGattService gattService : gattServices) {
List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics(); List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
@ -346,7 +322,7 @@ public class BLEComm {
} }
} }
private byte[] readBuffer = new byte[1024]; private final byte[] readBuffer = new byte[1024];
private int bufferLength = 0; private int bufferLength = 0;
private void addToReadBuffer(byte[] buffer) { private void addToReadBuffer(byte[] buffer) {
@ -634,7 +610,7 @@ public class BLEComm {
writeCharacteristic_NO_RESPONSE(getUARTWriteBTGattChar(), bytes); writeCharacteristic_NO_RESPONSE(getUARTWriteBTGattChar(), bytes);
} }
protected void SendPumpCheck() { private void SendPumpCheck() {
// 1st message sent to pump after connect // 1st message sent to pump after connect
byte[] bytes = BleCommandUtil.getInstance().getEncryptedPacket(BleCommandUtil.DANAR_PACKET__OPCODE_ENCRYPTION__PUMP_CHECK, null, getConnectDeviceName()); byte[] bytes = BleCommandUtil.getInstance().getEncryptedPacket(BleCommandUtil.DANAR_PACKET__OPCODE_ENCRYPTION__PUMP_CHECK, null, getConnectDeviceName());
log.debug(">>>>> " + "ENCRYPTION__PUMP_CHECK (0x00)" + " " + DanaRS_Packet.toHexString(bytes)); log.debug(">>>>> " + "ENCRYPTION__PUMP_CHECK (0x00)" + " " + DanaRS_Packet.toHexString(bytes));