Loading android/app/jni/com_android_bluetooth_btservice_AdapterService.cpp +14 −0 Original line number Diff line number Diff line Loading @@ -1293,6 +1293,19 @@ static void requestMaximumTxDataLengthNative(JNIEnv* env, jobject obj, env->ReleaseByteArrayElements(address, addr, 1); } static int getMetricIdNative(JNIEnv* env, jobject obj, jbyteArray address) { ALOGV("%s", __func__); if (!sBluetoothInterface) return 0; // 0 is invalid id jbyte* addr = env->GetByteArrayElements(address, nullptr); if (addr == nullptr) { jniThrowIOException(env, EINVAL); return 0; } RawAddress addr_obj = {}; addr_obj.FromOctets((uint8_t*)addr); return sBluetoothInterface->get_metric_id(addr_obj); } static JNINativeMethod sMethods[] = { /* name, signature, funcPtr */ {"classInitNative", "()V", (void*)classInitNative}, Loading Loading @@ -1325,6 +1338,7 @@ static JNINativeMethod sMethods[] = { {"interopDatabaseClearNative", "()V", (void*)interopDatabaseClearNative}, {"interopDatabaseAddNative", "(I[BI)V", (void*)interopDatabaseAddNative}, {"obfuscateAddressNative", "([B)[B", (void*)obfuscateAddressNative}, {"getMetricIdNative", "([B)I", (void*)getMetricIdNative}, {"connectSocketNative", "([BI[BIII)I", (void*)connectSocketNative}, {"createSocketChannelNative", "(ILjava/lang/String;[BIII)I", (void*)createSocketChannelNative}, Loading android/app/src/com/android/bluetooth/btservice/AdapterService.java +15 −0 Original line number Diff line number Diff line Loading @@ -3002,6 +3002,19 @@ public class AdapterService extends Service { return obfuscateAddressNative(Utils.getByteAddress(device)); } /** * Get an incremental id of Bluetooth metrics and log * * @param device Bluetooth device * @return int of id for Bluetooth metrics and logging, 0 if the device is invalid */ public int getMetricId(BluetoothDevice device) { if (device == null) { return 0; } return getMetricIdNative(Utils.getByteAddress(device)); } static native void classInitNative(); native boolean initNative(boolean startRestricted, boolean isNiapMode); Loading Loading @@ -3081,6 +3094,8 @@ public class AdapterService extends Service { private native byte[] obfuscateAddressNative(byte[] address); private native int getMetricIdNative(byte[] address); /*package*/ native int connectSocketNative( byte[] address, int type, byte[] uuid, int port, int flag, int callingUid); Loading android/app/tests/unit/src/com/android/bluetooth/btservice/AdapterServiceTest.java +77 −0 Original line number Diff line number Diff line Loading @@ -716,4 +716,81 @@ public class AdapterServiceTest { } return true; } /** * Test: Get id for null address * Check if returned value from {@link AdapterService#getMetricId(BluetoothDevice)} is * 0 when device address is null */ @Test public void testGetMetricId_NullAddress() { Assert.assertEquals(mAdapterService.getMetricId(null), 0); } /** * Test: Get id when Bluetooth is disabled * Check whether the returned value meets expectation */ @Test public void testGetMetricId_BluetoothDisabled() { Assert.assertFalse(mAdapterService.getState() == BluetoothAdapter.STATE_ON); BluetoothDevice device = TestUtils.getTestDevice(BluetoothAdapter.getDefaultAdapter(), 0); int id = mAdapterService.getMetricId(device); Assert.assertTrue(id > 0); } /** * Test: Get id when Bluetooth is enabled * Check whether the returned value meets expectation */ @Test public void testGetMetricId_BluetoothEnabled() { Assert.assertFalse(mAdapterService.getState() == BluetoothAdapter.STATE_ON); doEnable(0, false); Assert.assertTrue(mAdapterService.getState() == BluetoothAdapter.STATE_ON); BluetoothDevice device = TestUtils.getTestDevice(BluetoothAdapter.getDefaultAdapter(), 0); int id = mAdapterService.getMetricId(device); Assert.assertTrue(id > 0); } /** * Test: Check if id gotten stays the same after toggling Bluetooth */ @Test public void testGetMetricId_PersistentBetweenToggle() { Assert.assertFalse(mAdapterService.getState() == BluetoothAdapter.STATE_ON); BluetoothDevice device = TestUtils.getTestDevice(BluetoothAdapter.getDefaultAdapter(), 0); int id1 = mAdapterService.getMetricId(device); Assert.assertTrue(id1 > 0); // Enable doEnable(0, false); Assert.assertTrue(mAdapterService.getState() == BluetoothAdapter.STATE_ON); int id2 = mAdapterService.getMetricId(device); Assert.assertEquals(id2, id1); // Disable doDisable(0, false); Assert.assertFalse(mAdapterService.getState() == BluetoothAdapter.STATE_ON); int id3 = mAdapterService.getMetricId(device); Assert.assertEquals(id3, id1); } /** * Test: Check if id gotten stays the same after re-initializing * {@link AdapterService} */ @Test public void testgetMetricId_PersistentBetweenAdapterServiceInitialization() throws PackageManager.NameNotFoundException { Assert.assertFalse(mAdapterService.getState() == BluetoothAdapter.STATE_ON); BluetoothDevice device = TestUtils.getTestDevice(BluetoothAdapter.getDefaultAdapter(), 0); int id1 = mAdapterService.getMetricId(device); Assert.assertTrue(id1 > 0); tearDown(); setUp(); Assert.assertFalse(mAdapterService.getState() == BluetoothAdapter.STATE_ON); int id2 = mAdapterService.getMetricId(device); Assert.assertEquals(id2, id1); } } Loading
android/app/jni/com_android_bluetooth_btservice_AdapterService.cpp +14 −0 Original line number Diff line number Diff line Loading @@ -1293,6 +1293,19 @@ static void requestMaximumTxDataLengthNative(JNIEnv* env, jobject obj, env->ReleaseByteArrayElements(address, addr, 1); } static int getMetricIdNative(JNIEnv* env, jobject obj, jbyteArray address) { ALOGV("%s", __func__); if (!sBluetoothInterface) return 0; // 0 is invalid id jbyte* addr = env->GetByteArrayElements(address, nullptr); if (addr == nullptr) { jniThrowIOException(env, EINVAL); return 0; } RawAddress addr_obj = {}; addr_obj.FromOctets((uint8_t*)addr); return sBluetoothInterface->get_metric_id(addr_obj); } static JNINativeMethod sMethods[] = { /* name, signature, funcPtr */ {"classInitNative", "()V", (void*)classInitNative}, Loading Loading @@ -1325,6 +1338,7 @@ static JNINativeMethod sMethods[] = { {"interopDatabaseClearNative", "()V", (void*)interopDatabaseClearNative}, {"interopDatabaseAddNative", "(I[BI)V", (void*)interopDatabaseAddNative}, {"obfuscateAddressNative", "([B)[B", (void*)obfuscateAddressNative}, {"getMetricIdNative", "([B)I", (void*)getMetricIdNative}, {"connectSocketNative", "([BI[BIII)I", (void*)connectSocketNative}, {"createSocketChannelNative", "(ILjava/lang/String;[BIII)I", (void*)createSocketChannelNative}, Loading
android/app/src/com/android/bluetooth/btservice/AdapterService.java +15 −0 Original line number Diff line number Diff line Loading @@ -3002,6 +3002,19 @@ public class AdapterService extends Service { return obfuscateAddressNative(Utils.getByteAddress(device)); } /** * Get an incremental id of Bluetooth metrics and log * * @param device Bluetooth device * @return int of id for Bluetooth metrics and logging, 0 if the device is invalid */ public int getMetricId(BluetoothDevice device) { if (device == null) { return 0; } return getMetricIdNative(Utils.getByteAddress(device)); } static native void classInitNative(); native boolean initNative(boolean startRestricted, boolean isNiapMode); Loading Loading @@ -3081,6 +3094,8 @@ public class AdapterService extends Service { private native byte[] obfuscateAddressNative(byte[] address); private native int getMetricIdNative(byte[] address); /*package*/ native int connectSocketNative( byte[] address, int type, byte[] uuid, int port, int flag, int callingUid); Loading
android/app/tests/unit/src/com/android/bluetooth/btservice/AdapterServiceTest.java +77 −0 Original line number Diff line number Diff line Loading @@ -716,4 +716,81 @@ public class AdapterServiceTest { } return true; } /** * Test: Get id for null address * Check if returned value from {@link AdapterService#getMetricId(BluetoothDevice)} is * 0 when device address is null */ @Test public void testGetMetricId_NullAddress() { Assert.assertEquals(mAdapterService.getMetricId(null), 0); } /** * Test: Get id when Bluetooth is disabled * Check whether the returned value meets expectation */ @Test public void testGetMetricId_BluetoothDisabled() { Assert.assertFalse(mAdapterService.getState() == BluetoothAdapter.STATE_ON); BluetoothDevice device = TestUtils.getTestDevice(BluetoothAdapter.getDefaultAdapter(), 0); int id = mAdapterService.getMetricId(device); Assert.assertTrue(id > 0); } /** * Test: Get id when Bluetooth is enabled * Check whether the returned value meets expectation */ @Test public void testGetMetricId_BluetoothEnabled() { Assert.assertFalse(mAdapterService.getState() == BluetoothAdapter.STATE_ON); doEnable(0, false); Assert.assertTrue(mAdapterService.getState() == BluetoothAdapter.STATE_ON); BluetoothDevice device = TestUtils.getTestDevice(BluetoothAdapter.getDefaultAdapter(), 0); int id = mAdapterService.getMetricId(device); Assert.assertTrue(id > 0); } /** * Test: Check if id gotten stays the same after toggling Bluetooth */ @Test public void testGetMetricId_PersistentBetweenToggle() { Assert.assertFalse(mAdapterService.getState() == BluetoothAdapter.STATE_ON); BluetoothDevice device = TestUtils.getTestDevice(BluetoothAdapter.getDefaultAdapter(), 0); int id1 = mAdapterService.getMetricId(device); Assert.assertTrue(id1 > 0); // Enable doEnable(0, false); Assert.assertTrue(mAdapterService.getState() == BluetoothAdapter.STATE_ON); int id2 = mAdapterService.getMetricId(device); Assert.assertEquals(id2, id1); // Disable doDisable(0, false); Assert.assertFalse(mAdapterService.getState() == BluetoothAdapter.STATE_ON); int id3 = mAdapterService.getMetricId(device); Assert.assertEquals(id3, id1); } /** * Test: Check if id gotten stays the same after re-initializing * {@link AdapterService} */ @Test public void testgetMetricId_PersistentBetweenAdapterServiceInitialization() throws PackageManager.NameNotFoundException { Assert.assertFalse(mAdapterService.getState() == BluetoothAdapter.STATE_ON); BluetoothDevice device = TestUtils.getTestDevice(BluetoothAdapter.getDefaultAdapter(), 0); int id1 = mAdapterService.getMetricId(device); Assert.assertTrue(id1 > 0); tearDown(); setUp(); Assert.assertFalse(mAdapterService.getState() == BluetoothAdapter.STATE_ON); int id2 = mAdapterService.getMetricId(device); Assert.assertEquals(id2, id1); } }