I am working on One application that needs to Advertise itself and start a Gatt server at the same time. But somehow when I tried to do that device's Bluetooth state gets changed by itself.
I am unsing device Samsung m32 Andriod 12
private val mBluetoothAdapterPowerStateReceiver: BroadcastReceiver =
object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action
if (action == BluetoothAdapter.ACTION_STATE_CHANGED) {
val state = intent.getIntExtra(
BluetoothAdapter.EXTRA_STATE,
BluetoothAdapter.ERROR
)
when (state) {
BluetoothAdapter.STATE_OFF -> Log.d(TAG, "GantnerBLEAdapter: STATE_OFF")
BluetoothAdapter.STATE_TURNING_OFF -> Log.d(TAG, "GantnerBLEAdapter: STATE_TURNING_OFF")
BluetoothAdapter.STATE_ON -> Log.d(TAG, "GantnerBLEAdapter: STATE_ON")
BluetoothAdapter.STATE_TURNING_ON -> Log.d(TAG, "GantnerBLEAdapter: STATE_TURNING_ON")
}
}
}
}
And here is did register the callback for the same change
val filter = IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)
activity.registerReceiver(mBluetoothAdapterPowerStateReceiver, filter)
Here is my methods for advertising and Gatt server which are well palce in my onw Gattserver class.
fun startAdvertisementAndServer(){
gantnerGattServer.startAdvertising()
gantnerGattServer.startServer()
}
fun stopAdvertisementAndServer(){
gantnerGattServer.stopServer()
gantnerGattServer.stopAdvertising()
}
I call startAdvertisementAndServer on a click of the button and also stop that stopAdvertisementAndServer if it is enbled.
If i do this start and stop multiple times more than twice Bluetooth state automatically started changing itself.
Android system automatically restarts the Bluetooth So it called STATE_OFF and than STATE_ON.
Is this normal behaviour, i did check this in other apps it is not behaving same for other apps. do we need to do any extra code to prevent that?
Please help me in this. I am using
minSdk 21
targetSdk 33
Also gave all permission nessesary for BLUETOOTH
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="com.google.android.gms.permission.AD_ID" />
<uses-permission
android:name="android.permission.BLUETOOTH_SCAN"
android:usesPermissionFlags="neverForLocation" />
I did have findings for the same this is happening due to my advertisement data.
Which i pass as a scan response
val manufacturerDataKotlin = ByteArray(1)
manufacturerDataKotlin[0] = 0x08
manufacturerDataKotlin.plus(advInfo.toByteArray()).let {
Log.d(TAG, "initializeAdvertiser: $it")
mScanResponseData = AdvertiseData.Builder()
.addManufacturerData(0x0403, it)
.build()
}
Passing as a scan response
mBluetoothLeAdvertiser!!.startAdvertising(
mAdvertiseSettings,
mAdvertiseData, mScanResponseData, mAdvertisingCallback
Am I doing something wrong here can the scan response affect the Bluetooth State? )