Skip to content

Commit

Permalink
Removed Result list on Android. Fixed bug that caused stale connectio…
Browse files Browse the repository at this point in the history
…n to device.
  • Loading branch information
pauldemarco committed Sep 6, 2017
1 parent 1702e44 commit 4d764f7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public class FlutterBluePlugin implements MethodCallHandler {
private final EventChannel characteristicNotifiedChannel;
private final BluetoothManager mBluetoothManager;
private BluetoothAdapter mBluetoothAdapter;
private final Map<String, Result> mConnectionRequests = new HashMap<>();
private final Map<String, BluetoothGatt> mGattServers = new HashMap<>();

/**
Expand Down Expand Up @@ -187,20 +186,10 @@ public void onMethodCall(MethodCall call, Result result) {
}
}

// If the device is completely new, connect and add to list
if(!mGattServers.containsKey(deviceId)){
BluetoothGatt gattServer = device.connectGatt(registrar.activity(), options.getAndroidAutoConnect(), mGattCallback);
mGattServers.put(deviceId, gattServer);
}

// Update the connection requests list with this Result instance
synchronized (mConnectionRequests) {
Result r = mConnectionRequests.remove(deviceId);
if(r != null) {
r.error("connect_cancelled", "another connection attempt to this device has started", null);
}
mConnectionRequests.put(deviceId, result);
}
// New request, connect and add gattServer to Map
BluetoothGatt gattServer = device.connectGatt(registrar.activity(), options.getAndroidAutoConnect(), mGattCallback);
mGattServers.put(deviceId, gattServer);
result.success(null);
break;
}

Expand All @@ -211,12 +200,6 @@ public void onMethodCall(MethodCall call, Result result) {
if(gattServer != null) {
gattServer.close();
}
synchronized (mConnectionRequests) {
Result r = mConnectionRequests.remove(deviceId);
if(r != null) {
r.error("connect_cancelled", "the connect request was cancelled", null);
}
}
result.success(null);
break;
}
Expand Down Expand Up @@ -718,29 +701,7 @@ public void onCancel(Object o) {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
Log.d(TAG, "onConnectionStateChange: ");
synchronized (mConnectionRequests) {
Result result = mConnectionRequests.remove(gatt.getDevice().getAddress());
if(result != null) {
if(status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_CONNECTED) {
final Protos.BluetoothDevice p = Protos.BluetoothDevice.newBuilder()
.setName(gatt.getDevice().getName())
.setRemoteId(gatt.getDevice().getAddress())
.setType(Protos.BluetoothDevice.Type.forNumber(gatt.getDevice().getType()))
.build();
result.success(p.toByteArray());
} else if(status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_DISCONNECTED) {
result.error("connect_cancelled", "the device has been disconnected", null);
} else {
result.error("connect_error", "Error in BluetoothGattCallback, status:" + status + " newState:" + newState, null);
}
}
}
// Main method call, for onStateChanged streams
try {
channel.invokeMethod("DeviceState", ProtoMaker.from(gatt.getDevice(), newState).toByteArray());
} catch (Exception e) {
e.printStackTrace();
}
channel.invokeMethod("DeviceState", ProtoMaker.from(gatt.getDevice(), newState).toByteArray());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ static Protos.CharacteristicProperties from(int properties) {
.build();
}

static Protos.DeviceStateResponse from(BluetoothDevice device, int state) throws Exception {
static Protos.DeviceStateResponse from(BluetoothDevice device, int state) {
Protos.DeviceStateResponse.Builder p = Protos.DeviceStateResponse.newBuilder();
switch(state) {
case BluetoothProfile.STATE_DISCONNECTING:
Expand All @@ -134,7 +134,7 @@ static Protos.DeviceStateResponse from(BluetoothDevice device, int state) throws
p.setState(Protos.DeviceStateResponse.BluetoothDeviceState.DISCONNECTED);
break;
default:
throw new Exception("device state is unknown");
break;
}
p.setRemoteId(device.getAddress());
return p.build();
Expand Down
5 changes: 1 addition & 4 deletions lib/src/flutter_blue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,7 @@ class FlutterBlue {
subscription.cancel();
});

await _channel
.invokeMethod('connect', request.writeToBuffer())
.then((List<int> data) => new protos.BluetoothDevice.fromBuffer(data))
.then((d) => new BluetoothDevice.fromProto(d));
await _channel.invokeMethod('connect', request.writeToBuffer());

subscription = device.onStateChanged().listen(
controller.add,
Expand Down

0 comments on commit 4d764f7

Please sign in to comment.