diff --git a/BluetoothLeGatt/Application/build.gradle b/BluetoothLeGatt/Application/build.gradle index 206747b7..2b205ed5 100644 --- a/BluetoothLeGatt/Application/build.gradle +++ b/BluetoothLeGatt/Application/build.gradle @@ -6,7 +6,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:3.4.2' + classpath 'com.android.tools.build:gradle:4.1.1' } } @@ -18,18 +18,10 @@ repositories { } dependencies { - - - implementation "com.android.support:support-v4:28.0.0" - implementation "com.android.support:support-v13:28.0.0" - implementation "com.android.support:cardview-v7:28.0.0" - implementation "com.android.support:appcompat-v7:28.0.0" - - - - - - + implementation 'androidx.legacy:legacy-support-v4:1.0.0' + implementation 'androidx.legacy:legacy-support-v13:1.0.0' + implementation 'androidx.cardview:cardview:1.0.0' + implementation 'androidx.appcompat:appcompat:1.2.0' } // The sample build uses multiple directories to @@ -41,11 +33,11 @@ List dirs = [ 'template'] // boilerplate code that is generated by the sample template process android { - compileSdkVersion 28 + compileSdkVersion 30 defaultConfig { - minSdkVersion 18 - targetSdkVersion 28 + minSdkVersion 23 + targetSdkVersion 30 } compileOptions { diff --git a/BluetoothLeGatt/Application/src/main/AndroidManifest.xml b/BluetoothLeGatt/Application/src/main/AndroidManifest.xml index d3cf2575..763c52fa 100644 --- a/BluetoothLeGatt/Application/src/main/AndroidManifest.xml +++ b/BluetoothLeGatt/Application/src/main/AndroidManifest.xml @@ -33,6 +33,10 @@ + + + + diff --git a/BluetoothLeGatt/Application/src/main/java/com/example/android/bluetoothlegatt/BluetoothLeService.java b/BluetoothLeGatt/Application/src/main/java/com/example/android/bluetoothlegatt/BluetoothLeService.java index 694faaf9..97bfd210 100644 --- a/BluetoothLeGatt/Application/src/main/java/com/example/android/bluetoothlegatt/BluetoothLeService.java +++ b/BluetoothLeGatt/Application/src/main/java/com/example/android/bluetoothlegatt/BluetoothLeService.java @@ -128,7 +128,7 @@ private void broadcastUpdate(final String action, // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) { int flag = characteristic.getProperties(); - int format = -1; + int format; if ((flag & 0x01) != 0) { format = BluetoothGattCharacteristic.FORMAT_UINT16; Log.d(TAG, "Heart rate format UINT16."); @@ -216,7 +216,7 @@ public boolean connect(final String address) { } // Previously connected device. Try to reconnect. - if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) + if (address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) { Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection."); if (mBluetoothGatt.connect()) { diff --git a/BluetoothLeGatt/Application/src/main/java/com/example/android/bluetoothlegatt/DeviceControlActivity.java b/BluetoothLeGatt/Application/src/main/java/com/example/android/bluetoothlegatt/DeviceControlActivity.java index dc2f90bd..385c48d9 100644 --- a/BluetoothLeGatt/Application/src/main/java/com/example/android/bluetoothlegatt/DeviceControlActivity.java +++ b/BluetoothLeGatt/Application/src/main/java/com/example/android/bluetoothlegatt/DeviceControlActivity.java @@ -53,12 +53,11 @@ public class DeviceControlActivity extends Activity { private TextView mConnectionState; private TextView mDataField; - private String mDeviceName; private String mDeviceAddress; private ExpandableListView mGattServicesList; private BluetoothLeService mBluetoothLeService; private ArrayList> mGattCharacteristics = - new ArrayList>(); + new ArrayList<>(); private boolean mConnected = false; private BluetoothGattCharacteristic mNotifyCharacteristic; @@ -158,7 +157,7 @@ public void onCreate(Bundle savedInstanceState) { setContentView(R.layout.gatt_services_characteristics); final Intent intent = getIntent(); - mDeviceName = intent.getStringExtra(EXTRAS_DEVICE_NAME); + String mDeviceName = intent.getStringExtra(EXTRAS_DEVICE_NAME); mDeviceAddress = intent.getStringExtra(EXTRAS_DEVICE_ADDRESS); // Sets up UI references. @@ -246,34 +245,32 @@ private void displayData(String data) { // on the UI. private void displayGattServices(List gattServices) { if (gattServices == null) return; - String uuid = null; + String uuid; String unknownServiceString = getResources().getString(R.string.unknown_service); String unknownCharaString = getResources().getString(R.string.unknown_characteristic); - ArrayList> gattServiceData = new ArrayList>(); + ArrayList> gattServiceData = new ArrayList<>(); ArrayList>> gattCharacteristicData - = new ArrayList>>(); - mGattCharacteristics = new ArrayList>(); + = new ArrayList<>(); + mGattCharacteristics = new ArrayList<>(); // Loops through available GATT Services. for (BluetoothGattService gattService : gattServices) { - HashMap currentServiceData = new HashMap(); + HashMap currentServiceData = new HashMap<>(); uuid = gattService.getUuid().toString(); currentServiceData.put( LIST_NAME, SampleGattAttributes.lookup(uuid, unknownServiceString)); currentServiceData.put(LIST_UUID, uuid); gattServiceData.add(currentServiceData); - ArrayList> gattCharacteristicGroupData = - new ArrayList>(); + ArrayList> gattCharacteristicGroupData = new ArrayList<>(); List gattCharacteristics = gattService.getCharacteristics(); - ArrayList charas = - new ArrayList(); + ArrayList charas = new ArrayList<>(); // Loops through available Characteristics. for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) { charas.add(gattCharacteristic); - HashMap currentCharaData = new HashMap(); + HashMap currentCharaData = new HashMap<>(); uuid = gattCharacteristic.getUuid().toString(); currentCharaData.put( LIST_NAME, SampleGattAttributes.lookup(uuid, unknownCharaString)); diff --git a/BluetoothLeGatt/Application/src/main/java/com/example/android/bluetoothlegatt/DeviceScanActivity.java b/BluetoothLeGatt/Application/src/main/java/com/example/android/bluetoothlegatt/DeviceScanActivity.java index 59367fcf..ebd9fd08 100644 --- a/BluetoothLeGatt/Application/src/main/java/com/example/android/bluetoothlegatt/DeviceScanActivity.java +++ b/BluetoothLeGatt/Application/src/main/java/com/example/android/bluetoothlegatt/DeviceScanActivity.java @@ -16,6 +16,7 @@ package com.example.android.bluetoothlegatt; +import android.Manifest; import android.app.Activity; import android.app.ListActivity; import android.bluetooth.BluetoothAdapter; @@ -36,6 +37,8 @@ import android.widget.TextView; import android.widget.Toast; +import androidx.core.content.ContextCompat; + import java.util.ArrayList; /** @@ -57,6 +60,8 @@ public void onCreate(Bundle savedInstanceState) { getActionBar().setTitle(R.string.title_devices); mHandler = new Handler(); + checkPermission(); + // Use this check to determine whether BLE is supported on the device. Then you can // selectively disable BLE-related features. if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { @@ -74,7 +79,6 @@ public void onCreate(Bundle savedInstanceState) { if (mBluetoothAdapter == null) { Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show(); finish(); - return; } } @@ -156,6 +160,20 @@ protected void onListItemClick(ListView l, View v, int position, long id) { startActivity(intent); } + private void checkPermission() { + int check = ContextCompat.checkSelfPermission( + this, Manifest.permission.ACCESS_FINE_LOCATION); + + if (check != PackageManager.PERMISSION_GRANTED){ + requestPermissions( + new String[]{ + Manifest.permission.ACCESS_COARSE_LOCATION, + Manifest.permission.ACCESS_FINE_LOCATION + }, + 1); + } + } + private void scanLeDevice(final boolean enable) { if (enable) { // Stops scanning after a pre-defined scan period. @@ -179,12 +197,12 @@ public void run() { // Adapter for holding devices found through scanning. private class LeDeviceListAdapter extends BaseAdapter { - private ArrayList mLeDevices; - private LayoutInflater mInflator; + private final ArrayList mLeDevices; + private final LayoutInflater mInflator; public LeDeviceListAdapter() { super(); - mLeDevices = new ArrayList(); + mLeDevices = new ArrayList<>(); mInflator = DeviceScanActivity.this.getLayoutInflater(); } @@ -244,7 +262,7 @@ public View getView(int i, View view, ViewGroup viewGroup) { } // Device scan callback. - private BluetoothAdapter.LeScanCallback mLeScanCallback = + private final BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { @Override diff --git a/BluetoothLeGatt/Application/src/main/java/com/example/android/bluetoothlegatt/SampleGattAttributes.java b/BluetoothLeGatt/Application/src/main/java/com/example/android/bluetoothlegatt/SampleGattAttributes.java index e8db74cd..d7b03552 100644 --- a/BluetoothLeGatt/Application/src/main/java/com/example/android/bluetoothlegatt/SampleGattAttributes.java +++ b/BluetoothLeGatt/Application/src/main/java/com/example/android/bluetoothlegatt/SampleGattAttributes.java @@ -22,7 +22,7 @@ * This class includes a small subset of standard GATT attributes for demonstration purposes. */ public class SampleGattAttributes { - private static HashMap attributes = new HashMap(); + private static final HashMap attributes = new HashMap<>(); public static String HEART_RATE_MEASUREMENT = "00002a37-0000-1000-8000-00805f9b34fb"; public static String CLIENT_CHARACTERISTIC_CONFIG = "00002902-0000-1000-8000-00805f9b34fb"; diff --git a/BluetoothLeGatt/gradle.properties b/BluetoothLeGatt/gradle.properties index 0bc4294e..8c3a5350 100644 --- a/BluetoothLeGatt/gradle.properties +++ b/BluetoothLeGatt/gradle.properties @@ -18,3 +18,5 @@ # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true +android.enableJetifier=true +android.useAndroidX=true diff --git a/BluetoothLeGatt/gradle/wrapper/gradle-wrapper.properties b/BluetoothLeGatt/gradle/wrapper/gradle-wrapper.properties index c4486d47..4dc579e5 100644 --- a/BluetoothLeGatt/gradle/wrapper/gradle-wrapper.properties +++ b/BluetoothLeGatt/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,6 @@ +#Sun Nov 29 20:33:08 CET 2020 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip