diff --git a/bluetooth/1.0/default/bluetooth_address.cc b/bluetooth/1.0/default/bluetooth_address.cc index b917de9c03..656d78dd5d 100644 --- a/bluetooth/1.0/default/bluetooth_address.cc +++ b/bluetooth/1.0/default/bluetooth_address.cc @@ -83,6 +83,34 @@ bool BluetoothAddress::get_local_address(uint8_t* local_addr) { valid_bda = true; } + /* Generate new BDA if necessary */ + if (!valid_bda) { + char bdstr[kStringLength + 1]; + + /* No autogen BDA. Generate one now. */ + local_addr[0] = 0x22; + local_addr[1] = 0x22; + local_addr[2] = (uint8_t)rand(); + local_addr[3] = (uint8_t)rand(); + local_addr[4] = (uint8_t)rand(); + local_addr[5] = (uint8_t)rand(); + + /* Convert to ascii, and store as a persistent property */ + bytes_to_string(local_addr, bdstr); + + ALOGE("%s: No preset BDA! Generating BDA: %s for prop %s", __func__, + (char*)bdstr, PERSIST_BDADDR_PROPERTY); + ALOGE("%s: This is a bug in the platform! Please fix!", __func__); + + if (property_set(PERSIST_BDADDR_PROPERTY, (char*)bdstr) < 0) { + ALOGE("%s: Failed to set random BDA in prop %s", __func__, + PERSIST_BDADDR_PROPERTY); + valid_bda = false; + } else { + valid_bda = true; + } + } + return valid_bda; } diff --git a/bluetooth/1.0/default/test/bluetooth_address_test.cc b/bluetooth/1.0/default/test/bluetooth_address_test.cc index 9f80ec2d1a..adcd9c157c 100644 --- a/bluetooth/1.0/default/test/bluetooth_address_test.cc +++ b/bluetooth/1.0/default/test/bluetooth_address_test.cc @@ -219,10 +219,18 @@ TEST_F(BluetoothAddressTest, get_local_address) { EXPECT_TRUE(BluetoothAddress::get_local_address(address)); EXPECT_TRUE(memcmp(address, kTestAddr1_bytes, BluetoothAddress::kBytes) == 0); - // File contains a zero address. + // File contains a zero address. A random address will be generated. FileWriteString(kAddrPath, kZeros); EXPECT_TRUE(property_set(PROPERTY_BT_BDADDR_PATH, kAddrPath) == 0); - EXPECT_FALSE(BluetoothAddress::get_local_address(address)); + EXPECT_TRUE(property_set(PERSIST_BDADDR_PROPERTY, kTestAddrBad1) == 0); + EXPECT_TRUE(BluetoothAddress::get_local_address(address)); + EXPECT_TRUE(memcmp(address, kZeros_bytes, BluetoothAddress::kBytes) != 0); + char prop[PROP_VALUE_MAX] = "Before reading"; + EXPECT_TRUE(property_get(PERSIST_BDADDR_PROPERTY, prop, NULL) == + BluetoothAddress::kStringLength); + char address_str[BluetoothAddress::kStringLength + 1]; + BluetoothAddress::bytes_to_string(address, address_str); + EXPECT_TRUE(memcmp(address_str, prop, BluetoothAddress::kStringLength) == 0); // Factory property contains an address. EXPECT_TRUE(property_set(PERSIST_BDADDR_PROPERTY, kTestAddrBad1) == 0);