BluetoothGatt.writeCharacteristic return false half the time - java

Actually I make an update through Bluetooth. In first time I erase the memory bank and then I write an hexa File on it.
But half the time an update will don't work correctly, for every data I transfer the first writeCharacteristic will return false.
It happen on an entire update half the time.
I try in debug mode but the method never return false in that case, of course it's probably a delay problem, but I can't increase the time.
This is my code for send my data :
public void sendTX(final byte[] sMessage) {
BluetoothGattService service = mBluetoothGatt.getService(UUID_SERVICE_SERIAL);
if (service != null && sMessage != null) {
Log.d(TAG,"sMessage : " + sMessage);
final BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID_TX);
if (characteristic != null) {
Thread thread = new Thread() {
public void run() {
if (sMessage.length > 20) {
for (int i = 0; i < sMessage.length; i += 20) {
byte[] byteArraySplit = Arrays.copyOfRange(sMessage, i, i + 20 < sMessage.length ? i + 20 : sMessage.length);
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
characteristic.setValue(byteArraySplit);
while(!mBluetoothGatt.writeCharacteristic(characteristic)) {
try {
TimeUnit.MILLISECONDS.sleep(15);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} else {
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
characteristic.setValue(sMessage);
while(!mBluetoothGatt.writeCharacteristic(characteristic)){
try {
TimeUnit.MILLISECONDS.sleep(15);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
};
thread.start();
} else {
Log.d(TAG, "UUID TX null");
}
} else {
Log.d(TAG, "Service BLE null");
}
}
And this is the code of the native writeCharacteristic method :
public boolean writeCharacteristic(BluetoothGattCharacteristic characteristic) {
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0
&& (characteristic.getProperties() &
BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) return false;
if (VDBG) Log.d(TAG, "writeCharacteristic() - uuid: " + characteristic.getUuid());
if (mService == null || mClientIf == 0 || characteristic.getValue() == null) return false;
BluetoothGattService service = characteristic.getService();
if (service == null) return false;
BluetoothDevice device = service.getDevice();
if (device == null) return false;
synchronized(mDeviceBusy) {
if (mDeviceBusy) return false;
mDeviceBusy = true;
}
try {
mService.writeCharacteristic(mClientIf, device.getAddress(),
characteristic.getInstanceId(), characteristic.getWriteType(),
AUTHENTICATION_NONE, characteristic.getValue());
} catch (RemoteException e) {
Log.e(TAG,"",e);
mDeviceBusy = false;
return false;
}
return true;
}

Never use timeouts to try to workaround this issue. The proper way is to wait for the callback and then perform the next request. See Android BLE BluetoothGatt.writeDescriptor() return sometimes false.

Related

Reduce My for loop complexity to 4 as per sonar confiuration

I integrated code to sonarqube for quality code and set method complexity is 4.
Please help me to reduce this for loop complexity to 4
for (int i = 1; i <= retryCount + count; i++) {
try {
if (cat!= null)
grps = cat.getAllGroups(category);
flag = true;
} catch (Exception e) {
if (i >= retryCount + count) {
throw new MyException(new Fault(1009,
new Object[] { e.getMessage() }));
} else {
if (e.getMessage().contains("No Records Found")) {
break;
} else {
String status = handleIOAutomationException(ctx, e);
if (status.equalsIgnoreCase("none")) {
throw new MyException(new Fault(1009,
new Object[] { e.getMessage() }));
} else if (status.equalsIgnoreCase("some")) {
}
}
}
}
if (flag) {
break;
}
}
Try to extract your logic into its own sub methods and try make these shorter. You should also try to make your identifiers more readable.
public void method() {
...
int maxLoops = retryCount + count;
for (int i = 1; i <= maxLoops; i++) {
if (tryToGetAllGroups(maxLoops, i))
break;
}
}
private boolean tryToGetAllGroups(int maxLoops, int i) throws MyException {
try {
if (cat != null)
grps = cat.getAllGroups(category);
return true;
} catch (Exception e) { // you should make Exception more specific!
if (i >= maxLoops) {
return throwFinalException(e);
} else {
if (tryToGetCat(e)) return true;
}
}
return false;
}
private boolean tryToGetCat(Exception e) throws MyException {
if (e.getMessage().contains("No Records Found")) {
return true;
} else {
String status = handleIOAutomationException(ctx, e);
if (status.equalsIgnoreCase("none")) {
throwFinalException(e);
} else if (status.equalsIgnoreCase("some")) {
// try to get cat here
}
}
return false;
}
private boolean throwFinalException(Exception e) throws MyException {
throw new MyException(new Fault(1009,
new Object[]{e.getMessage()}));
}

OnNmeaMessageListener is not working even when called using the addNmeaListener

This is the Interface which I'm using to get the Nmea and the altitude but when the onNmeaMessageListener is being called it does not execute it. I'm calling it using gpsManager which is LocationManager variable,
the code neither works nor shows up as an error.
OnNmeaMessageListener onNmeaMessageListener = (nmea, timestamp) -> {
Log.d("TrackManager", " NMEA : " + nmea + " TimeStamp : " + timestamp);
if (trackStarted && !trackPaused) {
if (nmea.startsWith("$GPGGA") || nmea.startsWith("$GPRMC")) {
Log.d("TrackManager", "NMEA:" + nmea);
try {
out.write(nmea.getBytes());
if (continuesMode) {
dataOut.write(nmea.getBytes());
bufferedNMEALines++;
if (bufferedNMEALines >= 10) {
flushContinuesTrack();
//dataOut.reset();
bufferedNMEALines = 0;
}
} else {
outCompressed.write(nmea.getBytes());
}
// Log.d("TrackManager","NMEA:" + nmea);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (nmea.startsWith("$GPGGA")) {
String[] nmeaSplit = nmea.split(",");
if (nmeaSplit.length > 10) {
if (nmeaSplit[9].length() > 0) {
try {
mslAltitude = Float.parseFloat(nmeaSplit[9]);
} catch (NumberFormatException ex) {
mslAltitude = 0;
}
} else {
mslAltitude = 0.0f;
}
}
}
}
}
if (markStarted && hasFix() && nmea.startsWith("$GPRMC")) {
markProgress++;
trackListener.onMarkProgress(markProgress);
if (markProgress >= markMax) {
stopMark();
}
}
};
This is how I'm calling it
gpsManager.addNmeaListener(onNmeaMessageListener);
EDIT
Declaration:
private LocationManager gpsManager;
Initialized in onCreate like so:
gpsManager = (LocationManager)
getSystemService(LOCATION_SERVICE);
Also got Location updated:
gpsManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER,
GPS_UPDATE_TIME, 0, gpsListener);
NMEA strings come from live GPS receivers; they're not cached by the OS. LocationManager.PASSIVE_PROVIDER is not going to turn on the GPS, so that choice of provider is inappropriate for this use case. Try GPS_PROVIDER instead.

ArrayList IndexOutOfBoundsException even though handled

I have a method which tries to get the current message in an arraylist of mesages and if there are none then it returns null, however I get an index out of bounds exception and I can't understand why
public Message getCurrent() {
if(this.size() <= 0) {
return null;
}else {
return this.get(currentMessageIndex);
}
}
The following calls the above method in another class and throws the exception:
public void run() {
while (running) {
//Message msg = clientQueue.getLast(); // Matches EEEEE in ServerReceiver
Message msg = clientQueue.getCurrent();
System.out.flush();
if (msg != null) {
if (msg.getSent() == false) {
client.println(msg);// Matches FFFFF in ClientReceiver
client.flush();
msg.setSent();
}
}
}
return;
}
public Message getCurrent() {
if(this.size() <= 0) {
return null;
}else {
return (this.size() > currentMessageIndex) ? this.get(currentMessageIndex) : null;
}}
Can you try with this, I have handled fail over case.
Just use
this.size()-1
Instead of
this.size()

Calling external method from runnable task.

I have waitandsweep method in which I create a runnable task and schedule it using threadpoolexecutor. From runnable task I am calling external method isSliceDone() eventhough the value is true, it is not honouring it. meaning the control is going to next else if part. I think there is some synchronization issue here. Can someone help pls ?
protected void waitAndSweep(final String symbol) {
try {
runnable = new Runnable() {
#Override
public void run() {
try {
if (isSliceDone()) {
long timeSinceLastSliceQtySent = lastSliceSentTime == 0 ? getInterval() : System.currentTimeMillis() - lastSliceSentTime;
long waitTime = timeSinceLastSliceQtySent >= getInterval() ? 0 : getInterval() - timeSinceLastSliceQtySent;
if (waitTime > 0) {
logTradeEvent("waitAndSweep", symbol, "waittime: " + waitTime);
Thread.sleep(waitTime);
}
} else if (sweepInterval > 0) {
// It always comes here eventhough isSliceDone is true.
logTradeEvent("waitAndSweep", symbol, "sweepInterval: " + sweepInterval);
Thread.sleep(sweepInterval);
}
callSweep(symbol);
} catch (Exception e) {
}
}
};
Future<?> self = threadPoolExecutor.submit(runnable);
} catch (Exception e) {
}
}
private boolean isSliceDone() {
synchronized (this) {
double sliceQuantity = getSliceQuantity();
if (Quant.equals(sentSliceQty % sliceQuantity, 0.0) && Quant.equals(executedSliceQty % sliceQuantity, 0.0)) {
return Quant.greater(sentSliceQty, executedSliceQty) ? false : true;
}
return false;
}
}

Java server socket sending multiple messages issue

We have a java socket program where the server gets data from many devices and works fine. At times the server needs to send some command to the devices. When it sends individual commands it works fine. The problem comes when it sends multiple commands, only the first one is successful. We cant figure out why the rest fails. Below is the snippet showing how the message is sent. Should I set a delay after a message is sent?
public static void main(String[] args) {
new sServer7888();
}
sServer7888() {
try{
final ServerSocket serverSocketConn = new ServerSocket(7888);
while (true){
try{
Socket socketConn1 = serverSocketConn.accept();
new Thread(new ConnectionHandler(socketConn1)).start();
}
catch(Exception e){
e.printStackTrace(System.out);
}
}
}
catch (Exception e) {
e.printStackTrace(System.out);
}
}
class ConnectionHandler implements Runnable {
private Socket receivedSocketConn1;
ConnectionHandler(Socket receivedSocketConn1) {
this.receivedSocketConn1=receivedSocketConn1;
}
public void run() {
while ((nextChar=readIn1.read()) != -1) {
completeMessage += (char) nextChar;
if (nextChar == '*')
{
String[] splitResult = completeMessage .split(",");
String header=splitResult[0].trim().substring(0,4);
if((header.equals("$ACK")){
//update the message sent from the server as already acknowledge.
}
else{
//run query to find if there are any message to be sent out to the devices
while(rsOC1.next()){
commandText = rsOC1.getString("commandText");
writeOut1.write(commandText);
writeOut1.write("\r\n");
writeOut1.flush();
}
//now process the normal message receive from the devices.
}
completeMessage="";
}
}
}
}
If your device is sending ACK on Every message and Server is able to receive it then you can proceed in following way with your server side program.
EDIT
I have updated the code as per the requirement analysis. Let me know if any discrepancy is found after implementing it.
Thread.sleep(1000) is not the reliable solution for above case because
we are not knowing how long the device might take to execute previous
command sent by Server .
public void run()
{
int i = -1;
ArrayList<String> list = new ArrayList<String>();
while ((nextChar=readIn1.read()) != -1)
{
boolean isCompleteMessage = readMessage(nextChar);
if (isCompleteMessage)
{
String[] splitResult = completeMessage .split(",");
String header=splitResult[0].trim().substring(0,4);
if((header.equals("$ACK"))
{
String id = null;
if (i != -1)
{
id = list.get(i);
id = id.substring(0,id.indexOf("^"));
}
//update the message sent from the server as already acknowledge using id extracted above.
if ( i == 0)
{
list.remove(i);
if (list.size() == 0)
{
i = -1;
}
else
{
commandText = list.get(i);
writeOut1.write(commandText.substring((commandText.indexOf("^")) + 1));
writeOut1.write("\r\n");
writeOut1.flush();
}
}
}
else
{
//process here the normal message receive from the devices.
if (i == -1)
{
list = getRecords();
if (list.size() > 0)
{
i = 0;
commandText = list.get(i);
writeOut1.write(commandText.substring((commandText.indexOf("^")) + 1));
writeOut1.write("\r\n");
writeOut1.flush();
}
}
else
{
commandText = list.get(i);
writeOut1.write(commandText.substring((commandText.indexOf("^")) + 1));
writeOut1.write("\r\n");
writeOut1.flush();
}
}
completeMessage = "";
}
}
}
public boolean readMessage(int nextChar)
{
completeMessage += (char)nextChar;
if (((char)nextChar) == '*')
{
return true;
}
else
{
return false;
}
}
//Retreive all commands from database and returns the ArrayList containing those commands.
public ArrayList<String> getRecords()
{
ArrayList<String> list = new ArrayList<String>();
Statement stat = null;
ResultSet rsOC1 = null;
try
{
stat = con.createStatement();
rsOC1 = stat.executeQuery("Query for message retrieval from database");
while (rsOC1.next())
{
String sElement = rs0C1.getString("commandID") + "^" + rs0C1.getString("commandText");
list.add(sElement);
}
}
catch (Exception ex){}
finally
{
if (rs0C1 != null)
{
try
{
rs0C1.close();
} catch () {}
}
if (stat != null)
{
try
{
stat.close();
} catch () {}
}
return list;
}
}

Categories

Resources