Skip to content

Commit

Permalink
issuetracker.google.com/issues/237308366
Browse files Browse the repository at this point in the history
  • Loading branch information
AbandonedCart committed Sep 13, 2023
1 parent 4958afc commit 0a42afc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 91 deletions.
8 changes: 4 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@
tools:ignore="ScopedStorage" />
<uses-permission android:name="${installPermission}" />
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<!-- uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" / -->
<uses-permission android:name="android.permission.BLUETOOTH" tools:remove="android:maxSdkVersion" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"
android:usesPermissionFlags="neverForLocation" tools:targetApi="s" />
android:usesPermissionFlags="neverForLocation" tools:targetApi="S" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
android:maxSdkVersion="30" />
android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.CAMERA" />

<uses-feature android:name="android.hardware.camera.any" android:required="false" />
<uses-feature android:name="android.software.webview" android:required="false" />
<uses-feature android:name="android.hardware.nfc" android:required="false" />
<uses-feature android:name="android.hardware.bluetooth" android:required="false" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="false" />
<uses-feature android:name="${wearUsesFeature}" android:required="false" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,17 @@ class BluetoothHandler(
) {
private var mBluetoothAdapter: BluetoothAdapter? = null
private val listener: BluetoothListener
private var onRequestLocationQ: ActivityResultLauncher<Array<String>>
private var onRequestLocationQ: ActivityResultLauncher<String>
private var onRequestBluetoothS: ActivityResultLauncher<Array<String>>
private var onRequestBluetooth: ActivityResultLauncher<Intent>
private var onRequestLocation: ActivityResultLauncher<String>
private var onRequestAdapter: ActivityResultLauncher<Intent>

init {
onRequestLocationQ = registry.register(
"LocationQ", ActivityResultContracts.RequestMultiplePermissions()
) { permissions: Map<String, Boolean> ->
var isLocationAvailable = false
permissions.entries.forEach {
if (it.key == Manifest.permission.ACCESS_FINE_LOCATION)
isLocationAvailable = it.value
}
if (isLocationAvailable) requestBluetooth(context) else listener.onPermissionsFailed()
"LocationQ", ActivityResultContracts.RequestPermission()
) { granted ->
if (granted) requestBluetooth(context) else listener.onPermissionsFailed()
}
onRequestBluetoothS = registry.register(
"BluetoothS", ActivityResultContracts.RequestMultiplePermissions()
Expand Down Expand Up @@ -110,7 +105,7 @@ class BluetoothHandler(
.setPositiveButton(R.string.accept) { dialog: DialogInterface, _: Int ->
dialog.dismiss()
if (Version.isQuinceTart)
onRequestLocationQ.launch(PERMISSIONS_LOCATION)
onRequestLocationQ.launch(PERMISSION_FINE_LOCATION)
else
onRequestLocation.launch(PERMISSION_COURSE_LOCATION)
}
Expand Down Expand Up @@ -143,7 +138,7 @@ class BluetoothHandler(
if (BuildConfig.GOOGLE_PLAY)
showLocationRequest(activity)
else
onRequestLocationQ.launch(PERMISSIONS_LOCATION)
onRequestLocationQ.launch(PERMISSION_FINE_LOCATION)
}
}
}
Expand Down Expand Up @@ -229,10 +224,6 @@ class BluetoothHandler(
companion object {
private const val PERMISSION_COURSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION
private const val PERMISSION_FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION
private val PERMISSIONS_LOCATION = arrayOf(
PERMISSION_FINE_LOCATION,
PERMISSION_COURSE_LOCATION
)
@RequiresApi(Build.VERSION_CODES.S)
private val PERMISSIONS_BLUETOOTH = arrayOf(
Manifest.permission.BLUETOOTH_SCAN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,10 +478,12 @@ class GattService : Service() {
getCharacteristicValue(characteristic, value)
}

@Deprecated("Deprecated in Java", ReplaceWith("if (status == BluetoothGatt.GATT_SUCCESS) getCharacteristicValue(characteristic)", "android.bluetooth.BluetoothGatt"))
override fun onCharacteristicRead(
gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int
) {
if (status == BluetoothGatt.GATT_SUCCESS) getCharacteristicValue(characteristic)
if (status == BluetoothGatt.GATT_SUCCESS)
getCharacteristicValue(characteristic)
}

override fun onCharacteristicWrite(
Expand All @@ -508,6 +510,7 @@ class GattService : Service() {
}
}

@Deprecated("Deprecated in Java", ReplaceWith("getCharacteristicValue(characteristic)"))
override fun onCharacteristicChanged(
gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic
) {
Expand Down Expand Up @@ -640,40 +643,13 @@ class GattService : Service() {
setResponseDescriptors(characteristic)
}

/**
* Retrieves a list of supported GATT services on the connected device. This should be
* invoked only after `BluetoothGatt#discoverServices()` completes successfully.
*
* @return A `List` of supported services.
*/
private val supportedGattServices: List<BluetoothGattService>?
get() = mBluetoothGatt?.services

@Throws(IllegalAccessException::class, UnsupportedOperationException::class)
fun setPuckServicesUUID() {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
throw IllegalAccessException(getString(R.string.fail_bluetooth_adapter))
}
val services = supportedGattServices
if (services.isNullOrEmpty()) {
throw UnsupportedOperationException(
getString(R.string.gatt_services_null, serviceType.logTag)
)
}
for (customService in services) {
when (customService.uuid) {
Nordic.NUS -> {
legacyInterface = false
break
}
Nordic.LegacyNUS -> {
legacyInterface = true
break
}
else -> {
continue
}
}
legacyInterface = mBluetoothGatt!!.services.any { service ->
service.characteristics.any { it.uuid == Nordic.LegacyNUS }
}
setCharacteristicRX()
}
Expand All @@ -682,35 +658,16 @@ class GattService : Service() {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
throw IllegalAccessException(getString(R.string.fail_bluetooth_adapter))
}
val services = supportedGattServices
if (services.isNullOrEmpty()) {
throw UnsupportedOperationException(
getString(R.string.gatt_services_null, serviceType.logTag)
)
}
for (customService in services) {
when (customService.uuid) {
Nordic.NUS -> {
omllboInterface = false
break
}
Nordic.OmllboNUS -> {
omllboInterface = true
break
}
else -> {
continue
}
}
omllboInterface = mBluetoothGatt!!.services.any { service ->
service.characteristics.any { it.uuid == Nordic.OmllboNUS }
}
setCharacteristicRX()
}

@Throws(IllegalAccessException::class, UnsupportedOperationException::class)
fun setCharacteristicRX() {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
if (mBluetoothAdapter == null || mBluetoothGatt == null)
throw IllegalAccessException(getString(R.string.fail_bluetooth_adapter))
}
mCharacteristicRX = mBluetoothGatt!!.getService(GattNUS).getCharacteristic(GattRX)
?: mBluetoothGatt!!.services.find { service ->
service.characteristics.any {
Expand All @@ -725,9 +682,8 @@ class GattService : Service() {

@Throws(IllegalAccessException::class, UnsupportedOperationException::class)
fun setCharacteristicTX() {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
if (mBluetoothAdapter == null || mBluetoothGatt == null)
throw IllegalAccessException(getString(R.string.fail_bluetooth_adapter))
}
mCharacteristicTX = mBluetoothGatt!!.getService(GattNUS).getCharacteristic(GattTX)
?: mBluetoothGatt!!.services.find { service ->
service.characteristics.any {
Expand Down Expand Up @@ -1244,23 +1200,20 @@ class GattService : Service() {
companion object {
private var legacyInterface = false
private var omllboInterface = false
val GattNUS: UUID = if (legacyInterface)
Nordic.LegacyNUS
else if (omllboInterface)
Nordic.OmllboNUS
else
Nordic.NUS
val GattTX: UUID = if (legacyInterface)
Nordic.LegacyTX
else if (omllboInterface)
Nordic.OmllboTX
else
Nordic.TX
val GattRX: UUID = if (legacyInterface)
Nordic.LegacyRX
else if (omllboInterface)
Nordic.OmllboRX
else
Nordic.RX
val GattNUS: UUID = when {
legacyInterface -> Nordic.LegacyNUS
omllboInterface -> Nordic.OmllboNUS
else -> Nordic.NUS
}
val GattTX: UUID = when {
legacyInterface -> Nordic.LegacyTX
omllboInterface -> Nordic.OmllboTX
else -> Nordic.TX
}
val GattRX: UUID = when {
legacyInterface -> Nordic.LegacyRX
omllboInterface -> Nordic.OmllboRX
else -> Nordic.RX
}
}
}

0 comments on commit 0a42afc

Please sign in to comment.