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 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 static final UUID CHARACTERISTIC_CONFIGURATION_DESCRIPTOR = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
|
||||||
|
|
||||||
private final String mBtDeviceAddress;
|
|
||||||
private final BluetoothDevice mBtDevice;
|
private final BluetoothDevice mBtDevice;
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
private final PebbleLESupport mPebbleLESupport;
|
private final PebbleLESupport mPebbleLESupport;
|
||||||
|
@ -45,7 +44,6 @@ class PebbleGATTClient extends BluetoothGattCallback {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mBtDevice = btDevice;
|
mBtDevice = btDevice;
|
||||||
mPebbleLESupport = pebbleLESupport;
|
mPebbleLESupport = pebbleLESupport;
|
||||||
mBtDeviceAddress = btDevice.getAddress();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean initialize() {
|
boolean initialize() {
|
||||||
|
@ -54,10 +52,10 @@ class PebbleGATTClient extends BluetoothGattCallback {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
|
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
|
||||||
if (!gatt.getDevice().getAddress().equals(mBtDeviceAddress)) {
|
if (!mPebbleLESupport.isExpectedDevice(gatt.getDevice())) {
|
||||||
LOG.info("onCharacteristicChanged() unexpected device: " + gatt.getDevice().getAddress() + " , expected: " + mBtDeviceAddress);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (characteristic.getUuid().equals(MTU_CHARACTERISTIC)) {
|
if (characteristic.getUuid().equals(MTU_CHARACTERISTIC)) {
|
||||||
int newMTU = characteristic.getIntValue(FORMAT_UINT16, 0);
|
int newMTU = characteristic.getIntValue(FORMAT_UINT16, 0);
|
||||||
LOG.info("Pebble requested MTU: " + newMTU);
|
LOG.info("Pebble requested MTU: " + newMTU);
|
||||||
|
@ -68,10 +66,10 @@ class PebbleGATTClient extends BluetoothGattCallback {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
|
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
|
||||||
if (!gatt.getDevice().getAddress().equals(mBtDeviceAddress)) {
|
if (!mPebbleLESupport.isExpectedDevice(gatt.getDevice())) {
|
||||||
LOG.info("onCharacteristicRead() unexpected device: " + gatt.getDevice().getAddress() + " , expected: " + mBtDeviceAddress);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG.info("onCharacteristicRead() status = " + status);
|
LOG.info("onCharacteristicRead() status = " + status);
|
||||||
if (status == BluetoothGatt.GATT_SUCCESS) {
|
if (status == BluetoothGatt.GATT_SUCCESS) {
|
||||||
LOG.info("onCharacteristicRead()" + characteristic.getUuid().toString() + " " + GB.hexdump(characteristic.getValue(), 0, -1));
|
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) {
|
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
|
||||||
if (!gatt.getDevice().getAddress().equals(mBtDeviceAddress)) {
|
if (!mPebbleLESupport.isExpectedDevice(gatt.getDevice())) {
|
||||||
LOG.info("onConnectionStateChange() unexpected device: " + gatt.getDevice().getAddress() + " , expected: " + mBtDeviceAddress);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG.info("onConnectionStateChange() status = " + status + " newState = " + newState);
|
LOG.info("onConnectionStateChange() status = " + status + " newState = " + newState);
|
||||||
if (newState == BluetoothGatt.STATE_CONNECTED) {
|
if (newState == BluetoothGatt.STATE_CONNECTED) {
|
||||||
LOG.info("calling discoverServices()");
|
LOG.info("calling discoverServices()");
|
||||||
|
@ -99,10 +97,10 @@ class PebbleGATTClient extends BluetoothGattCallback {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
|
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
|
||||||
if (!gatt.getDevice().getAddress().equals(mBtDeviceAddress)) {
|
if (!mPebbleLESupport.isExpectedDevice(gatt.getDevice())) {
|
||||||
LOG.info("onCharacteristcsWrite unexpected device: " + gatt.getDevice().getAddress() + " , expected: " + mBtDeviceAddress);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG.info("onCharacteristicWrite() " + characteristic.getUuid());
|
LOG.info("onCharacteristicWrite() " + characteristic.getUuid());
|
||||||
if (characteristic.getUuid().equals(PAIRING_TRIGGER_CHARACTERISTIC) || characteristic.getUuid().equals(CONNECTIVITY_CHARACTERISTIC)) {
|
if (characteristic.getUuid().equals(PAIRING_TRIGGER_CHARACTERISTIC) || characteristic.getUuid().equals(CONNECTIVITY_CHARACTERISTIC)) {
|
||||||
//mBtDevice.createBond(); // did not work when last tried
|
//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) {
|
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor bluetoothGattDescriptor, int status) {
|
||||||
if (!gatt.getDevice().getAddress().equals(mBtDeviceAddress)) {
|
if (!mPebbleLESupport.isExpectedDevice(gatt.getDevice())) {
|
||||||
LOG.info("onDescriptorWrite() unexpected device: " + gatt.getDevice().getAddress() + " , expected: " + mBtDeviceAddress);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG.info("onDescriptorWrite() status=" + status);
|
LOG.info("onDescriptorWrite() status=" + status);
|
||||||
|
|
||||||
UUID CHARACTERISTICUUID = bluetoothGattDescriptor.getCharacteristic().getUuid();
|
UUID CHARACTERISTICUUID = bluetoothGattDescriptor.getCharacteristic().getUuid();
|
||||||
|
@ -138,10 +136,10 @@ class PebbleGATTClient extends BluetoothGattCallback {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
|
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
|
||||||
if (!gatt.getDevice().getAddress().equals(mBtDeviceAddress)) {
|
if (!mPebbleLESupport.isExpectedDevice(gatt.getDevice())) {
|
||||||
LOG.info("onServicesDiscovered() unexpected device: " + gatt.getDevice().getAddress() + " , expected: " + mBtDeviceAddress);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG.info("onServicesDiscovered() status = " + status);
|
LOG.info("onServicesDiscovered() status = " + status);
|
||||||
if (status == BluetoothGatt.GATT_SUCCESS) {
|
if (status == BluetoothGatt.GATT_SUCCESS) {
|
||||||
BluetoothGattCharacteristic connectionPararmharacteristic = gatt.getService(SERVICE_UUID).getCharacteristic(CONNECTION_PARAMETERS_CHARACTERISTIC);
|
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) {
|
public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) {
|
||||||
|
if (!mPebbleLESupport.isExpectedDevice(device)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!characteristic.getUuid().equals(READ_CHARACTERISTICS)) {
|
if (!characteristic.getUuid().equals(READ_CHARACTERISTICS)) {
|
||||||
LOG.warn("unexpected read request");
|
LOG.warn("unexpected read request");
|
||||||
return;
|
return;
|
||||||
|
@ -88,6 +92,10 @@ class PebbleGATTServer extends BluetoothGattServerCallback {
|
||||||
|
|
||||||
public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic,
|
public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic,
|
||||||
boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
|
boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
|
||||||
|
if (!mPebbleLESupport.isExpectedDevice(device)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!characteristic.getUuid().equals(WRITE_CHARACTERISTICS)) {
|
if (!characteristic.getUuid().equals(WRITE_CHARACTERISTICS)) {
|
||||||
LOG.warn("unexpected write request");
|
LOG.warn("unexpected write request");
|
||||||
return;
|
return;
|
||||||
|
@ -117,6 +125,10 @@ class PebbleGATTServer extends BluetoothGattServerCallback {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
|
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);
|
LOG.info("Connection state change for device: " + device.getAddress() + " status = " + status + " newState = " + newState);
|
||||||
if (newState == BluetoothGattServer.STATE_DISCONNECTED) {
|
if (newState == BluetoothGattServer.STATE_DISCONNECTED) {
|
||||||
mPebbleLESupport.close();
|
mPebbleLESupport.close();
|
||||||
|
@ -126,6 +138,10 @@ class PebbleGATTServer extends BluetoothGattServerCallback {
|
||||||
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor,
|
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor,
|
||||||
boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
|
boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
|
||||||
|
|
||||||
|
if (!mPebbleLESupport.isExpectedDevice(device)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!descriptor.getCharacteristic().getUuid().equals(WRITE_CHARACTERISTICS)) {
|
if (!descriptor.getCharacteristic().getUuid().equals(WRITE_CHARACTERISTICS)) {
|
||||||
LOG.warn("unexpected write request");
|
LOG.warn("unexpected write request");
|
||||||
return;
|
return;
|
||||||
|
@ -148,6 +164,10 @@ class PebbleGATTServer extends BluetoothGattServerCallback {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onMtuChanged(BluetoothDevice device, int mtu) {
|
public void onMtuChanged(BluetoothDevice device, int mtu) {
|
||||||
|
if (!mPebbleLESupport.isExpectedDevice(device)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
LOG.info("Pebble requested mtu for server: " + mtu);
|
LOG.info("Pebble requested mtu for server: " + mtu);
|
||||||
mPebbleLESupport.setMTU(mtu);
|
mPebbleLESupport.setMTU(mtu);
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ import java.io.PipedOutputStream;
|
||||||
|
|
||||||
public class PebbleLESupport {
|
public class PebbleLESupport {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(PebbleLESupport.class);
|
private static final Logger LOG = LoggerFactory.getLogger(PebbleLESupport.class);
|
||||||
|
private final BluetoothDevice mBtDevice;
|
||||||
private PipeReader mPipeReader;
|
private PipeReader mPipeReader;
|
||||||
private PebbleGATTServer mPebbleGATTServer;
|
private PebbleGATTServer mPebbleGATTServer;
|
||||||
private PebbleGATTClient mPebbleGATTClient;
|
private PebbleGATTClient mPebbleGATTClient;
|
||||||
|
@ -34,11 +35,11 @@ public class PebbleLESupport {
|
||||||
|
|
||||||
BluetoothManager manager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
|
BluetoothManager manager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
|
||||||
BluetoothAdapter adapter = manager.getAdapter();
|
BluetoothAdapter adapter = manager.getAdapter();
|
||||||
BluetoothDevice btDevice = adapter.getRemoteDevice(btDeviceAddress);
|
mBtDevice = adapter.getRemoteDevice(btDeviceAddress);
|
||||||
mPebbleGATTServer = new PebbleGATTServer(this, context, btDevice);
|
mPebbleGATTServer = new PebbleGATTServer(this, context, mBtDevice);
|
||||||
mPebbleGATTServer.initialize();
|
mPebbleGATTServer.initialize();
|
||||||
|
|
||||||
mPebbleGATTClient = new PebbleGATTClient(this, context, btDevice);
|
mPebbleGATTClient = new PebbleGATTClient(this, context, mBtDevice);
|
||||||
mPebbleGATTClient.initialize();
|
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