I am building a bluetooth low energy advertiser just to explore and learn more about BLE technology and I have this error when I launch the app “FATAL EXCEPTION: main
Process: com.example.bleapp, java.lang.RuntimeException: Unable to start activity: java.lang.IllegalArgumentException: bad primaryPhy 2”.
I am pretty sure this is the section that is causing problems
val advertisingSetParametersUltraLowPower = AdvertisingSetParameters.Builder().apply {
setInterval(AdvertisingSetParameters.INTERVAL_HIGH)
setTxPowerLevel(AdvertisingSetParameters.TX_POWER_ULTRA_LOW)
setConnectable(false)
setScannable(true)
setIncludeTxPower(true)
setLegacyMode(false)
if (bluetoothAdapter?.isLe2MPhySupported() == true) {
setPrimaryPhy(PHY_LE_2M)
} else {
setPrimaryPhy(PHY_LE_1M) }
}
.build()
if (bluetoothAdapter!!.isLe2MPhySupported()) {
Text(text = "BLueTooth version = 5")
Button(onClick = {
if (bluetoothAdapter?.bluetoothLeAdvertiser != null) {
bluetoothAdapter!!.bluetoothLeAdvertiser!!.startAdvertisingSet(
advertisingSetParametersUltraLowPower,
advertiseExtendedData,
advertiseScanResponseData,
periodicAdvertisingParameters,
periodicData,
advertisingSetCallback)
}
}) {
Text(text = "Ultra Low Pow")
}
I have been told that maybe my phone doesn’t have all the Le2MPhy capabilities but I have checked and from my understanding i shouldn’t have a problem because I have if statement to check the different Le2MPHY characteristics of my phone and they are all supported. Here is the code that I use to check for Le2MPHY
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val isLe2MPhySupported = bluetoothAdapter!!.isLe2MPhySupported()
val isLeCodedPhySupported = bluetoothAdapter!!.isLeCodedPhySupported()
val isLeExtendedAdvertisingSupported = bluetoothAdapter!!.isLeExtendedAdvertisingSupported()
val isLePeriodicAdvertisingSupported = bluetoothAdapter!!.isLePeriodicAdvertisingSupported()
if (isLe2MPhySupported) {
Log.i("tag", "LE 2M PHY is supported.")
} else {
Log.i("tag", "LE 2M PHY is not supported on this device.")
}
if (isLeCodedPhySupported) {
Log.i("tag", "Le Coded Phy is supported.")
} else {
Log.i("tag", "Le Coded Phy is not supported on this device.")
}
if (isLeExtendedAdvertisingSupported) {
Log.i("tag", "Le Extended Advertising is supported.")
} else {
Log.i("tag", "Le Extended Advertising is not supported on this device.")
}
if (isLePeriodicAdvertisingSupported) {
Log.i("tag", "Le Periodic Advertising is supported.")
} else {
Log.i("tag", "Le Periodic Advertising is not supported on this device.")
}
} else {
Log.i("tag", "LE 2M PHY is not available on this device (API level below 26).")
}```
And here are the results of my Log statement:
LE 2M PHY is supported.
Le Coded Phy is supported.
Le Extended Advertising is supported.
Le Periodic Advertising is supported.
I'm simply expecting that the app starts advertising because the advertisement works fine with this code
Button(onClick = {
if (bluetoothAdapter?.bluetoothLeAdvertiser != null) {
bluetoothAdapter!!.bluetoothLeAdvertiser!!.startAdvertising(
advertiserSettingsLowPower,
advertiseData,
bleAdvertiserCallback)
}
}) {
Text(text = “Low Power”)
}