diff options
Diffstat (limited to 'src/main/java/com/google/android/mobly/snippet/bundled/utils/JsonSerializer.java')
-rw-r--r-- | src/main/java/com/google/android/mobly/snippet/bundled/utils/JsonSerializer.java | 63 |
1 files changed, 32 insertions, 31 deletions
diff --git a/src/main/java/com/google/android/mobly/snippet/bundled/utils/JsonSerializer.java b/src/main/java/com/google/android/mobly/snippet/bundled/utils/JsonSerializer.java index 45af4a4..fc7d17b 100644 --- a/src/main/java/com/google/android/mobly/snippet/bundled/utils/JsonSerializer.java +++ b/src/main/java/com/google/android/mobly/snippet/bundled/utils/JsonSerializer.java @@ -21,6 +21,7 @@ import android.net.DhcpInfo; import android.net.wifi.SupplicantState; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiInfo; +import android.os.Build; import android.os.ParcelUuid; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -28,7 +29,6 @@ import java.lang.reflect.Modifier; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; -import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -127,42 +127,43 @@ public class JsonSerializer { private JSONObject serializeBluetoothDevice(BluetoothDevice data) throws JSONException { JSONObject result = new JSONObject(); result.put("Address", data.getAddress()); + final String bondStateFieldName = "BondState"; switch (data.getBondState()) { - case 10: - result.put("BondState", "None"); + case BluetoothDevice.BOND_NONE: + result.put(bondStateFieldName, "BOND_NONE"); break; - case 11: - result.put("BondState", "Bonding"); + case BluetoothDevice.BOND_BONDING: + result.put(bondStateFieldName, "BOND_BONDING"); break; - case 12: - result.put("BondState", "Bonded"); - } - if (data.getName() == null) { - result.put("Name", "Null"); - } else { - result.put("Name", data.getName()); - } - switch (data.getType()) { - case 0: - result.put("Type", "Unknown"); - break; - case 1: - result.put("Type", "Classic"); + case BluetoothDevice.BOND_BONDED: + result.put(bondStateFieldName, "BOND_BONDED"); break; - case 2: - result.put("Type", "Le"); - break; - case 3: - result.put("Type", "Dual"); } - if (data.getUuids() == null) { - result.put("UUIDs", "Null"); - } else { - ArrayList<String> uuids = new ArrayList<String>(); - for (ParcelUuid uuid : data.getUuids()) { - uuids.add(uuid.toString()); + result.put("Name", data.getName()); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + final String deviceTypeFieldName = "DeviceType"; + switch (data.getType()) { + case BluetoothDevice.DEVICE_TYPE_CLASSIC: + result.put(deviceTypeFieldName, "DEVICE_TYPE_CLASSIC"); + break; + case BluetoothDevice.DEVICE_TYPE_LE: + result.put(deviceTypeFieldName, "DEVICE_TYPE_LE"); + break; + case BluetoothDevice.DEVICE_TYPE_DUAL: + result.put(deviceTypeFieldName, "DEVICE_TYPE_DUAL"); + break; + case BluetoothDevice.DEVICE_TYPE_UNKNOWN: + result.put(deviceTypeFieldName, "DEVICE_TYPE_UNKNOWN"); + break; + } + ParcelUuid[] parcelUuids = data.getUuids(); + if (parcelUuids != null) { + ArrayList<String> uuidStrings = new ArrayList<>(parcelUuids.length); + for (ParcelUuid parcelUuid : parcelUuids) { + uuidStrings.add(parcelUuid.getUuid().toString()); + } + result.put("UUIDs", uuidStrings); } - result.put("UUIDs", new JSONArray(uuids)); } return result; } |