GattSerialTransportProfile extends BaseProfile { @Override public void onCharacteristicChanged(GattClient client, BluetoothGattCharacteristic characteristic) { if (characteristic == mSerialCharacteristic) { byte[] data = mMessageAssembler.assemble(new GattSerialPacket(characteristic.getValue())); if (data != null) { if (BuildConfig.DEBUG) { Log.d(TAG, "Received data"); } if (mListener != null) { mListener.onMessageReceived(data); } else { client.disconnect(); } } } else { int index = BEAN_SCRATCH_UUIDS.indexOf(characteristic.getUuid()); if (index > -1) { index += 1; if (BuildConfig.DEBUG) { Log.d(TAG, "Received scratch bank update (" + index + ")"); } if (mListener != null) { try { ScratchBank bank = EnumParse.enumWithRawValue(ScratchBank.class, index); mListener.onScratchValueChanged(bank, characteristic.getValue()); } catch (NoEnumFoundException e) { Log.e(TAG, "Couldn't parse bank enum from scratch bank with index " + index); e.printStackTrace(); } } else { client.disconnect(); } } } } GattSerialTransportProfile(GattClient client, Handler handler); @Override void onProfileReady(); @Override void onCharacteristicWrite(GattClient client, BluetoothGattCharacteristic characteristic); @Override void onCharacteristicChanged(GattClient client, BluetoothGattCharacteristic characteristic); @Override void onReadRemoteRssi(GattClient client, int rssi); void abort(String message); void sendMessage(Buffer message); void setListener(SerialListener listener); String getName(); boolean isReady(); void clearReady(); static final int PACKET_TX_MAX_PAYLOAD_LENGTH; }
@Test public void testOnCharacteristicChanged() throws NoEnumFoundException { byte[] value = {1, 2, 3}; int index = 1; ScratchBank bank = EnumParse.enumWithRawValue(ScratchBank.class, index); gstp.setListener(mockListener); BluetoothGattCharacteristic mockChar = mock(BluetoothGattCharacteristic.class); when(mockChar.getUuid()).thenReturn(Constants.UUID_SCRATCH_CHAR_1); when(mockChar.getValue()).thenReturn(value); gstp.onCharacteristicChanged(mockGattClient, mockChar); verify(mockListener).onScratchValueChanged(bank, value); }