0

我有 2 个单独的服务,一个用于启动bluetoothLeAdvertiser,另一个用于bluetoothLeScanner.

使用的服务类bluetoothLeScanner如下

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    super.onStartCommand(intent, flags, startId)
    startScanning()
    return START_STICKY
}
private fun startScanning() {
    bluetoothAdapter?.bluetoothLeScanner?.startScan(scanCallBack)
    Toast.makeText(this.applicationContext, "Scanning Started", Toast.LENGTH_SHORT).show()
}

和服务类bluetoothLeAdvertiser看起来如下

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    super.onStartCommand(intent, flags, startId)
    FileManagement.getInstance(applicationContext).writeToFile(TAG, "onStartCommand() called")
    startAdvertising()
    return START_STICKY
}
private fun startAdvertising() {
    bluetoothAdapter?.bluetoothLeAdvertiser?.let {
        val setting = AdvertiseSettings.Builder()
            .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED)
            .setConnectable(false)
            .setTimeout(0)
            .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
            .build()

        val data = AdvertiseData.Builder()
            .setIncludeDeviceName(true)
            .setIncludeTxPowerLevel(true)
            .addServiceUuid(ParcelUuid.fromString("00001827-0000-1000-8000-00805F9B34FB"))
            .build()

        it.startAdvertising(setting, data, advertiseCallback)
        Toast.makeText(this.applicationContext, "Advert Started", Toast.LENGTH_SHORT).show()
    }
}

问题介于两者之间bluetoothLeScanner,并且bluetoothLeAdvertiser只有一个回调被触发。

4

0 回答 0