Commit 5af6594c authored by Kenji Bungo's avatar Kenji Bungo
Browse files

Minor fixes: Prevent gattserver from closing self on disconnect (this...

Minor fixes: Prevent gattserver from closing self on disconnect (this de-registered services). other connection fixes.
parent abfe25d5
......@@ -22,7 +22,7 @@ import java.util.List;
import static android.bluetooth.BluetoothGatt.GATT_SUCCESS;
//The flow here is confusing.
// Step 1. Read the hashmap. If it is populate it, diff it to ours.
// Step 1. Read the hashmap. If it is populated, diff it to ours.
// Step 2. Write the blocklength (one byte) and the hash of our qr.
// Step 3. Once we have written, read the blockmap, this allows us to sparsely populate.
// Step 4. Write to each block that is marked 0 in the blockmap.
......@@ -42,7 +42,6 @@ class BleGattConnector {
private byte[] expectedblockmap;
private HandlerQueueManager taskQueue;
private BluetoothGatt currentGatt;
private static final int WRITE_DELAY_MS=500;
private WriteCompleteListener completeCallback;
private BleGattConnector(){}
......@@ -103,7 +102,7 @@ class BleGattConnector {
gatt.discoverServices();
currentGatt=gatt;
}
else if(currentGatt!=null){currentGatt.close(); currentGatt=null;}
else if(currentGatt!=null){gatt.close(); currentGatt=null;}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
......@@ -117,7 +116,7 @@ class BleGattConnector {
catch (NullPointerException e){
e.printStackTrace();
completeCallback.writeComplete(false, gatt.getDevice().getAddress());
gatt.disconnect();
close();
return;
}
for(BluetoothGattCharacteristic c : characteristics)
......@@ -297,7 +296,7 @@ class BleGattConnector {
Handler mHandler;
List<Runnable> taskList;
Runnable currentTask;
public static final int DELAY = 500;
public static final int DELAY = 250;
public HandlerQueueManager(){
mHandler = new Handler(Looper.getMainLooper());
taskList = new ArrayList<>();
......
......@@ -182,7 +182,6 @@ class BleGattServer {
// //reconnect, we didn't catch all of it!
// }
// }
bluetoothGattServer.close();
}
else{
currentDevice=device;
......
......@@ -40,7 +40,8 @@ public class BleManager extends Service {
private BluetoothLeAdvertiser bluetoothLeAdvertiser;
private Handler mHandler;
private HandlerThread handlerThread;
//we should hold any QR data we are trying to work with while publishing so we are not dependent on the activity.
//we should hold any QR data we are trying to work with while publishing so we are not dependent on scoped memory.
private String qrData;
//our server/connector we will use for communications.
......@@ -93,7 +94,7 @@ public class BleManager extends Service {
mHandler.removeCallbacks(retryAdvertise);
if(enable) {
if(bluetoothLeAdvertiser==null){mHandler.postDelayed(retryAdvertise, 1000); return;}
if(bleGattServer!=null){bleGattServer.clearClientList((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE));}
if(bleGattServer!=null){bleGattServer.clearClientList((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE)); bleGattServer.close();}
if (/*!mBluetoothAdapter.isMultipleAdvertisementSupported() || */bluetoothLeAdvertiser==null){
if(DEBUG){Log.d(TAG, "Advertiser was null! quiting...");}
return;
......@@ -112,7 +113,8 @@ public class BleManager extends Service {
bluetoothLeAdvertiser.startAdvertising(settings, data, advertisingCallback);
}
else {
if(bleGattServer!=null){bleGattServer.clearClientList((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE));}
if(bleGattServer!=null){bleGattServer.clearClientList((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE));
bleGattServer.close();}
if(DEBUG){Log.d(TAG, "stopping advertising");}
if (bluetoothLeAdvertiser != null) {
bluetoothLeAdvertiser.stopAdvertising(advertisingCallback);
......@@ -169,17 +171,17 @@ public class BleManager extends Service {
connectedAddresses.add(address);
}
};
BleGattConnector.WriteCompleteListener writeCallback = new BleGattConnector.WriteCompleteListener() {
@Override
public void writeComplete(boolean success, String address) {
if(DEBUG){Log.d(TAG, "Write callback from gat connector. Status: "+success);}
if(!success)
{
bleGattConnector = new BleGattConnector(mBluetoothAdapter, BleManager.this, address, qrData,writeCallback);
connectedAddresses.remove(address);
scanLeDevice(true);
}
else{
connectedAddresses.remove(address);
if(bleGattConnector!=null){bleGattConnector.close(); bleGattConnector=null;}
scanLeDevice(true);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment