Pebble 2: Ignore all GATT communication with all other that the current device
Fixes a bad bug where disconnecting from another BLE device caused the Pebble2 to disconnect
This commit is contained in:
parent
2d4645f6cc
commit
a5263141d7
|
@ -31,7 +31,6 @@ class PebbleGATTClient extends BluetoothGattCallback {
|
|||
private static final UUID CONNECTION_PARAMETERS_CHARACTERISTIC = UUID.fromString("00000005-328E-0FBB-C642-1AA6699BDADA");
|
||||
private static final UUID CHARACTERISTIC_CONFIGURATION_DESCRIPTOR = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
|
||||
|
||||
private final String mBtDeviceAddress;
|
||||
private final BluetoothDevice mBtDevice;
|
||||
private final Context mContext;
|
||||
private final PebbleLESupport mPebbleLESupport;
|
||||
|
@ -45,7 +44,6 @@ class PebbleGATTClient extends BluetoothGattCallback {
|
|||
mContext = context;
|
||||
mBtDevice = btDevice;
|
||||
mPebbleLESupport = pebbleLESupport;
|
||||
mBtDeviceAddress = btDevice.getAddress();
|
||||
}
|
||||
|
||||
boolean initialize() {
|
||||
|
@ -54,10 +52,10 @@ class PebbleGATTClient extends BluetoothGattCallback {
|
|||
}
|
||||
|
||||
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
|
||||
if (!gatt.getDevice().getAddress().equals(mBtDeviceAddress)) {
|
||||
LOG.info("onCharacteristicChanged() unexpected device: " + gatt.getDevice().getAddress() + " , expected: " + mBtDeviceAddress);
|
||||
if (!mPebbleLESupport.isExpectedDevice(gatt.getDevice())) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (characteristic.getUuid().equals(MTU_CHARACTERISTIC)) {
|
||||
int newMTU = characteristic.getIntValue(FORMAT_UINT16, 0);
|
||||
LOG.info("Pebble requested MTU: " + newMTU);
|
||||
|
@ -68,10 +66,10 @@ class PebbleGATTClient extends BluetoothGattCallback {
|
|||
}
|
||||
|
||||
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
|
||||
if (!gatt.getDevice().getAddress().equals(mBtDeviceAddress)) {
|
||||
LOG.info("onCharacteristicRead() unexpected device: " + gatt.getDevice().getAddress() + " , expected: " + mBtDeviceAddress);
|
||||
if (!mPebbleLESupport.isExpectedDevice(gatt.getDevice())) {
|
||||
return;
|
||||
}
|
||||
|
||||
LOG.info("onCharacteristicRead() status = " + status);
|
||||
if (status == BluetoothGatt.GATT_SUCCESS) {
|
||||
LOG.info("onCharacteristicRead()" + characteristic.getUuid().toString() + " " + GB.hexdump(characteristic.getValue(), 0, -1));
|
||||
|
@ -85,10 +83,10 @@ class PebbleGATTClient extends BluetoothGattCallback {
|
|||
}
|
||||
|
||||
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
|
||||
if (!gatt.getDevice().getAddress().equals(mBtDeviceAddress)) {
|
||||
LOG.info("onConnectionStateChange() unexpected device: " + gatt.getDevice().getAddress() + " , expected: " + mBtDeviceAddress);
|
||||
if (!mPebbleLESupport.isExpectedDevice(gatt.getDevice())) {
|
||||
return;
|
||||
}
|
||||
|
||||
LOG.info("onConnectionStateChange() status = " + status + " newState = " + newState);
|
||||
if (newState == BluetoothGatt.STATE_CONNECTED) {
|
||||
LOG.info("calling discoverServices()");
|
||||
|
@ -99,10 +97,10 @@ class PebbleGATTClient extends BluetoothGattCallback {
|
|||
}
|
||||
|
||||
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
|
||||
if (!gatt.getDevice().getAddress().equals(mBtDeviceAddress)) {
|
||||
LOG.info("onCharacteristcsWrite unexpected device: " + gatt.getDevice().getAddress() + " , expected: " + mBtDeviceAddress);
|
||||
if (!mPebbleLESupport.isExpectedDevice(gatt.getDevice())) {
|
||||
return;
|
||||
}
|
||||
|
||||
LOG.info("onCharacteristicWrite() " + characteristic.getUuid());
|
||||
if (characteristic.getUuid().equals(PAIRING_TRIGGER_CHARACTERISTIC) || characteristic.getUuid().equals(CONNECTIVITY_CHARACTERISTIC)) {
|
||||
//mBtDevice.createBond(); // did not work when last tried
|
||||
|
@ -120,10 +118,10 @@ class PebbleGATTClient extends BluetoothGattCallback {
|
|||
}
|
||||
|
||||
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor bluetoothGattDescriptor, int status) {
|
||||
if (!gatt.getDevice().getAddress().equals(mBtDeviceAddress)) {
|
||||
LOG.info("onDescriptorWrite() unexpected device: " + gatt.getDevice().getAddress() + " , expected: " + mBtDeviceAddress);
|
||||
if (!mPebbleLESupport.isExpectedDevice(gatt.getDevice())) {
|
||||
return;
|
||||
}
|
||||
|
||||
LOG.info("onDescriptorWrite() status=" + status);
|
||||
|
||||
UUID CHARACTERISTICUUID = bluetoothGattDescriptor.getCharacteristic().getUuid();
|
||||
|
@ -138,10 +136,10 @@ class PebbleGATTClient extends BluetoothGattCallback {
|
|||
}
|
||||
|
||||
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
|
||||
if (!gatt.getDevice().getAddress().equals(mBtDeviceAddress)) {
|
||||
LOG.info("onServicesDiscovered() unexpected device: " + gatt.getDevice().getAddress() + " , expected: " + mBtDeviceAddress);
|
||||
if (!mPebbleLESupport.isExpectedDevice(gatt.getDevice())) {
|
||||
return;
|
||||
}
|
||||
|
||||
LOG.info("onServicesDiscovered() status = " + status);
|
||||
if (status == BluetoothGatt.GATT_SUCCESS) {
|
||||
BluetoothGattCharacteristic connectionPararmharacteristic = gatt.getService(SERVICE_UUID).getCharacteristic(CONNECTION_PARAMETERS_CHARACTERISTIC);
|
||||
|
|
|
@ -74,6 +74,10 @@ class PebbleGATTServer extends BluetoothGattServerCallback {
|
|||
}
|
||||
|
||||
public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) {
|
||||
if (!mPebbleLESupport.isExpectedDevice(device)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!characteristic.getUuid().equals(READ_CHARACTERISTICS)) {
|
||||
LOG.warn("unexpected read request");
|
||||
return;
|
||||
|
@ -88,6 +92,10 @@ class PebbleGATTServer extends BluetoothGattServerCallback {
|
|||
|
||||
public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic,
|
||||
boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
|
||||
if (!mPebbleLESupport.isExpectedDevice(device)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!characteristic.getUuid().equals(WRITE_CHARACTERISTICS)) {
|
||||
LOG.warn("unexpected write request");
|
||||
return;
|
||||
|
@ -117,6 +125,10 @@ class PebbleGATTServer extends BluetoothGattServerCallback {
|
|||
}
|
||||
|
||||
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
|
||||
if (!mPebbleLESupport.isExpectedDevice(device)) {
|
||||
return;
|
||||
}
|
||||
|
||||
LOG.info("Connection state change for device: " + device.getAddress() + " status = " + status + " newState = " + newState);
|
||||
if (newState == BluetoothGattServer.STATE_DISCONNECTED) {
|
||||
mPebbleLESupport.close();
|
||||
|
@ -126,6 +138,10 @@ class PebbleGATTServer extends BluetoothGattServerCallback {
|
|||
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor,
|
||||
boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
|
||||
|
||||
if (!mPebbleLESupport.isExpectedDevice(device)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!descriptor.getCharacteristic().getUuid().equals(WRITE_CHARACTERISTICS)) {
|
||||
LOG.warn("unexpected write request");
|
||||
return;
|
||||
|
@ -148,6 +164,10 @@ class PebbleGATTServer extends BluetoothGattServerCallback {
|
|||
|
||||
@Override
|
||||
public void onMtuChanged(BluetoothDevice device, int mtu) {
|
||||
if (!mPebbleLESupport.isExpectedDevice(device)) {
|
||||
return;
|
||||
}
|
||||
|
||||
LOG.info("Pebble requested mtu for server: " + mtu);
|
||||
mPebbleLESupport.setMTU(mtu);
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ import java.io.PipedOutputStream;
|
|||
|
||||
public class PebbleLESupport {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(PebbleLESupport.class);
|
||||
private final BluetoothDevice mBtDevice;
|
||||
private PipeReader mPipeReader;
|
||||
private PebbleGATTServer mPebbleGATTServer;
|
||||
private PebbleGATTClient mPebbleGATTClient;
|
||||
|
@ -34,11 +35,11 @@ public class PebbleLESupport {
|
|||
|
||||
BluetoothManager manager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
|
||||
BluetoothAdapter adapter = manager.getAdapter();
|
||||
BluetoothDevice btDevice = adapter.getRemoteDevice(btDeviceAddress);
|
||||
mPebbleGATTServer = new PebbleGATTServer(this, context, btDevice);
|
||||
mBtDevice = adapter.getRemoteDevice(btDeviceAddress);
|
||||
mPebbleGATTServer = new PebbleGATTServer(this, context, mBtDevice);
|
||||
mPebbleGATTServer.initialize();
|
||||
|
||||
mPebbleGATTClient = new PebbleGATTClient(this, context, btDevice);
|
||||
mPebbleGATTClient = new PebbleGATTClient(this, context, mBtDevice);
|
||||
mPebbleGATTClient.initialize();
|
||||
}
|
||||
|
||||
|
@ -149,5 +150,12 @@ public class PebbleLESupport {
|
|||
}
|
||||
}
|
||||
|
||||
boolean isExpectedDevice(BluetoothDevice device) {
|
||||
if (!device.getAddress().equals(mBtDevice.getAddress())) {
|
||||
LOG.info("unhandled device: " + device.getAddress() + " , ignoring, will only talk to " + mBtDevice.getAddress());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue