To connect with Bluetooth Low Energy sensors we build an app. We use the Companion Device Manager for paring the sensor with the app. In most cases this works fine. But in a few cases, when CompanionDeviceManger.associate(…) function is called, we don’t get a callback. And we don’t know why.
Our App is compatible with API version 26-31
So far, we’ve found out that the error occurred on the following devices:
• moto g 30 --> Api level 27
• Samsung Galaxy S8 --> Api level 27
(Companion Device Manager: https://developer.android.com/guide/topics/connectivity/companion-device-pairing)
Our Manifest is configured as follows:
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" android:required="true"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-feature android:name="android.software.companion_device_setup" android:required="true"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-feature android:name="android.hardware.camera" android:required="true"/>
This is how the function within our code (Kotlin) looks like (fact: The CompanionDeviceManger.associate(…) function is called):
val deviceFilter: BluetoothLeDeviceFilter = BluetoothLeDeviceFilter
.Builder()
.setNamePattern(
Pattern.compile(
sensorItem.hardwareName
)
)
.build()
val pairingRequest: AssociationRequest =
AssociationRequest
.Builder()
.setSingleDevice(true)
.addDeviceFilter(deviceFilter)
.build()
deviceManager.associate(
pairingRequest,
object :
CompanionDeviceManager.Callback() {
override fun onDeviceFound(
chooserLauncher: IntentSender?
) {
if (chooserLauncher != null) {
try {
bleChooseLauncher.launch(
IntentSenderRequest
.Builder(
chooserLauncher
)
.build()
)
} catch (e: Exception) {
Log.w(
TAG,
e.toString()
)
errorToast.show()
sensorItem.setPairingOff()
}
} else {
errorToast.show()
sensorItem.setPairingOff()
}
}
override fun onFailure(error: CharSequence?) {
errorToast.show()
sensorItem.setPairingOff()
}
},
null
)