onAdvertisingSetStopped
не вызывается, когда я пытаюсь позвонить
advertiser.stopAdvertisingSet(mAdvertisingSetCallback)
Вот мой исходный код. РекламаSetCallback
Код: Выделить всё
private val mAdvertisingSetCallback = @RequiresApi(Build.VERSION_CODES.O)
object : AdvertisingSetCallback() {
override fun onAdvertisingSetStarted(
advertisingSet: AdvertisingSet?,
txPower: Int,
status: Int
) {
super.onAdvertisingSetStarted(advertisingSet, txPower, status)
Log.d(TAG, "onAdvertisingSetStarted called, txPower: $txPower,status: $status")
if (status == ADVERTISE_SUCCESS) {
gattServerCallback?.onStartSuccess(this@GattServer, status)
currentAdvertiseSet = advertisingSet
currentAdvertiseSet?.setAdvertisingData(buildAdvertiseData())
} else {
gattServerCallback?.onStartFailure(this@GattServer.id,status)
}
}
override fun onAdvertisingDataSet(advertisingSet: AdvertisingSet?, status: Int) {
super.onAdvertisingDataSet(advertisingSet, status)
Log.d(TAG,"onAdvertisingDataSet, status: $status")
currentAdvertiseSet?.setScanResponseData(buildScanResponseData())
}
override fun onScanResponseDataSet(advertisingSet: AdvertisingSet?, status: Int) {
super.onScanResponseDataSet(advertisingSet, status)
Log.d(TAG,"onScanResponseDataSet, status: $status")
}
override fun onAdvertisingEnabled(
advertisingSet: AdvertisingSet?,
enable: Boolean,
status: Int
) {
super.onAdvertisingEnabled(advertisingSet, enable, status)
gattServerCallback?.onStartSuccess(this@GattServer, status)
Log.d(TAG, "onAdvertisingEnabled called, enable: $enable,status: $status")
}
override fun onAdvertisingSetStopped(advertisingSet: AdvertisingSet?) {
super.onAdvertisingSetStopped(advertisingSet)
Log.d(TAG, "onAdvertisingSetStopped called")
}
}
Код: Выделить всё
fun startBleGattServer(
ctx: Context,
bluetoothManager: BluetoothManager,
gattConfiguration: GattConfigurationWithServices?,
callback: GattServerCallBack
) {
try {
gattServerCallback = callback
if (!bluetoothManager.adapter.isEnabled) {
gattServerCallback?.onStartFailure(this@GattServer.id,BluetoothAdapter.STATE_OFF)
Log.w(TAG, "BluetoothAdapter not enabled,$gattServerCallback")
return
}
initBleAdvertiser(bluetoothManager.adapter)
val advSetting = buildAdvSetting()
val advData = buildAdvertiseData()
val sResponseData = buildScanResponseData()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
logDebug("Starting adv set...")
val advertisingSetParameters = buildAdvertisingSetParameters()
bluetoothLeAdvertiser!!.startAdvertisingSet(
advertisingSetParameters,
AdvertiseData.Builder().setIncludeDeviceName(true).build(),
null,
null,
null,
mAdvertisingSetCallback,
)
} else {
// bluetoothLeAdvertiser?.startAdvertising(
// advSetting,
// advData,
// sResponseData,
// mAdvertiseCallback
// )
Log.v(TAG, "Starting ble gatt server, version: ${Build.VERSION.SDK_INT}")
}
startGattServer(ctx, bluetoothManager, gattConfiguration)
} catch (ex: Exception) {
Log.e(TAG, "Error while starting ble gatt server")
gattServerCallback?.onStartFailure(id, 11) // TODO
ex.printStackTrace()
}
}
Код: Выделить всё
fun stopBleGattServer() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
logDebug("Stopping adv set...")
bluetoothLeAdvertiser?.stopAdvertisingSet(mAdvertisingSetCallback)
} else {
bluetoothLeAdvertiser?.stopAdvertising(mAdvertiseCallback)
}
//bluetoothLeAdvertiser = null
bluetoothGattServer?.close()
bluetoothGattServer = null
gattServerCallback = null
currentAdvertiseSet = null
Log.d(TAG,"Ble Gatt server stopped, name: $name,id: $id")
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... ing-called