Loading android/app/src/com/android/bluetooth/tbs/TbsGatt.java +3 −0 Original line number Diff line number Diff line Loading @@ -478,6 +478,8 @@ public class TbsGatt { private void notifyCharacteristicChanged(BluetoothDevice device, BluetoothGattCharacteristic characteristic, byte[] value) { if (getDeviceAuthorization(device) != BluetoothDevice.ACCESS_ALLOWED) return; if (value == null) return; if (mBluetoothGattServer != null) { mBluetoothGattServer.notifyCharacteristicChanged(device, characteristic, false, value); Loading @@ -487,6 +489,7 @@ public class TbsGatt { private void notifyCharacteristicChanged(BluetoothDevice device, BluetoothGattCharacteristic characteristic) { if (getDeviceAuthorization(device) != BluetoothDevice.ACCESS_ALLOWED) return; if (mBluetoothGattServer != null) { mBluetoothGattServer.notifyCharacteristicChanged(device, characteristic, false); } Loading android/app/tests/unit/src/com/android/bluetooth/tbs/TbsGattTest.java +46 −0 Original line number Diff line number Diff line Loading @@ -133,6 +133,7 @@ public class TbsGattTest { Assert.assertNotNull(mMockGattServer); verify(mMockGattServer).addService(mGattServiceCaptor.capture()); doReturn(mGattServiceCaptor.getValue()).when(mMockGattServer).getService(any(UUID.class)); Assert.assertNotNull(mMockGattServer); } Loading Loading @@ -823,6 +824,51 @@ public class TbsGattTest { eq(BluetoothGatt.GATT_INSUFFICIENT_AUTHORIZATION), eq(0), any()); } @Test public void testCharacteristicNotifyOnAuthorization() { prepareDefaultService(); Assert.assertNotNull(mGattServiceCaptor.getValue()); BluetoothGattCharacteristic characteristic = getCharacteristic(TbsGatt.UUID_STATUS_FLAGS); configureNotifications(mFirstDevice, characteristic, true); configureNotifications(mSecondDevice, characteristic, true); doReturn(mGattServiceCaptor.getValue()).when(mMockGattServer).getService(any(UUID.class)); Assert.assertNotNull(mGattServiceCaptor.getValue()); // Leave it as unauthorized yet doReturn(BluetoothDevice.ACCESS_REJECTED) .when(mMockTbsService) .getDeviceAuthorization(any(BluetoothDevice.class)); int statusFlagValue = TbsGatt.STATUS_FLAG_SILENT_MODE_ENABLED; // Assume no update on one of the characteristics BluetoothGattCharacteristic characteristic2 = getCharacteristic(TbsGatt.UUID_CALL_STATE); characteristic2.setValue((byte[]) null); Assert.assertNotNull(mGattServiceCaptor.getValue()); // Call it once but expect no notification for the unauthorized device byte[] valueBytes = new byte[2]; valueBytes[0] = (byte) (statusFlagValue & 0xFF); valueBytes[1] = (byte) ((statusFlagValue >> 8) & 0xFF); mTbsGatt.setSilentModeFlag(); verify(mMockGattServer, times(0)) .notifyCharacteristicChanged(any(), eq(characteristic), eq(false), eq(valueBytes)); // Expect a single notification for the just authorized device doReturn(BluetoothDevice.ACCESS_ALLOWED) .when(mMockTbsService) .getDeviceAuthorization(any(BluetoothDevice.class)); Assert.assertNotNull(mGattServiceCaptor.getValue()); mTbsGatt.onDeviceAuthorizationSet(mFirstDevice); verify(mMockGattServer, times(0)) .notifyCharacteristicChanged(any(), eq(characteristic2), eq(false)); verify(mMockGattServer, times(1)) .notifyCharacteristicChanged(any(), eq(characteristic), eq(false)); } @Test public void testCharacteristicReadAccessUnknownUnauthorized() { prepareDefaultService(); Loading Loading
android/app/src/com/android/bluetooth/tbs/TbsGatt.java +3 −0 Original line number Diff line number Diff line Loading @@ -478,6 +478,8 @@ public class TbsGatt { private void notifyCharacteristicChanged(BluetoothDevice device, BluetoothGattCharacteristic characteristic, byte[] value) { if (getDeviceAuthorization(device) != BluetoothDevice.ACCESS_ALLOWED) return; if (value == null) return; if (mBluetoothGattServer != null) { mBluetoothGattServer.notifyCharacteristicChanged(device, characteristic, false, value); Loading @@ -487,6 +489,7 @@ public class TbsGatt { private void notifyCharacteristicChanged(BluetoothDevice device, BluetoothGattCharacteristic characteristic) { if (getDeviceAuthorization(device) != BluetoothDevice.ACCESS_ALLOWED) return; if (mBluetoothGattServer != null) { mBluetoothGattServer.notifyCharacteristicChanged(device, characteristic, false); } Loading
android/app/tests/unit/src/com/android/bluetooth/tbs/TbsGattTest.java +46 −0 Original line number Diff line number Diff line Loading @@ -133,6 +133,7 @@ public class TbsGattTest { Assert.assertNotNull(mMockGattServer); verify(mMockGattServer).addService(mGattServiceCaptor.capture()); doReturn(mGattServiceCaptor.getValue()).when(mMockGattServer).getService(any(UUID.class)); Assert.assertNotNull(mMockGattServer); } Loading Loading @@ -823,6 +824,51 @@ public class TbsGattTest { eq(BluetoothGatt.GATT_INSUFFICIENT_AUTHORIZATION), eq(0), any()); } @Test public void testCharacteristicNotifyOnAuthorization() { prepareDefaultService(); Assert.assertNotNull(mGattServiceCaptor.getValue()); BluetoothGattCharacteristic characteristic = getCharacteristic(TbsGatt.UUID_STATUS_FLAGS); configureNotifications(mFirstDevice, characteristic, true); configureNotifications(mSecondDevice, characteristic, true); doReturn(mGattServiceCaptor.getValue()).when(mMockGattServer).getService(any(UUID.class)); Assert.assertNotNull(mGattServiceCaptor.getValue()); // Leave it as unauthorized yet doReturn(BluetoothDevice.ACCESS_REJECTED) .when(mMockTbsService) .getDeviceAuthorization(any(BluetoothDevice.class)); int statusFlagValue = TbsGatt.STATUS_FLAG_SILENT_MODE_ENABLED; // Assume no update on one of the characteristics BluetoothGattCharacteristic characteristic2 = getCharacteristic(TbsGatt.UUID_CALL_STATE); characteristic2.setValue((byte[]) null); Assert.assertNotNull(mGattServiceCaptor.getValue()); // Call it once but expect no notification for the unauthorized device byte[] valueBytes = new byte[2]; valueBytes[0] = (byte) (statusFlagValue & 0xFF); valueBytes[1] = (byte) ((statusFlagValue >> 8) & 0xFF); mTbsGatt.setSilentModeFlag(); verify(mMockGattServer, times(0)) .notifyCharacteristicChanged(any(), eq(characteristic), eq(false), eq(valueBytes)); // Expect a single notification for the just authorized device doReturn(BluetoothDevice.ACCESS_ALLOWED) .when(mMockTbsService) .getDeviceAuthorization(any(BluetoothDevice.class)); Assert.assertNotNull(mGattServiceCaptor.getValue()); mTbsGatt.onDeviceAuthorizationSet(mFirstDevice); verify(mMockGattServer, times(0)) .notifyCharacteristicChanged(any(), eq(characteristic2), eq(false)); verify(mMockGattServer, times(1)) .notifyCharacteristicChanged(any(), eq(characteristic), eq(false)); } @Test public void testCharacteristicReadAccessUnknownUnauthorized() { prepareDefaultService(); Loading