Android BLE计数器未通过手动BLE开/关按钮单击而停止

时间:2019-03-01 07:03:32

标签: android bluetooth-lowenergy beacon

我为BLE扫描创建模块。当我扫描信标时,它开始计数。当信标离开或我在手机上关闭BLE时,计数停止。当执行时,计数器在我关闭时仍会计数。你能告诉我:

  1. 是否建议最终用户每次第一次扫描我的信标BLE?

  2. 要强制启用和禁用手机的BLE,必须实施广播接收器吗?

这是我的代码:

 private var handler = Handler()
    private var  runnable =  Runnable()
    {
        updateCount()
    }

imageView.setOnClickListener {
            if(m_bluetoothAdapter!!.isEnabled){
                stopCount()
                handler.removeCallbacksAndMessages(null)
                checkStatus = "" 
                m_bluetoothAdapter!!.disable()
                imageView.setColorFilter(ContextCompat.getColor( this, R.color.white), android.graphics.PorterDuff.Mode.MULTIPLY);
            }else{

                imageView.setColorFilter(ContextCompat.getColor( this, R.color.white), android.graphics.PorterDuff.Mode.MULTIPLY);
                val enableBLE = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
                startActivityForResult(enableBLE , REQUEST_ENABLE_BLE)

            }
        }



    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
            if(requestCode == REQUEST_ENABLE_BLE) {
                if(resultCode == Activity.RESULT_OK) {
                    if(m_bluetoothAdapter!!.isEnabled) {
                        toast("BLE has been enabled").show()
                        imageView.setColorFilter(ContextCompat.getColor( this, R.color.linkedin_blue), android.graphics.PorterDuff.Mode.MULTIPLY);
                        pairedDeviceList()
                    }else{
                        toast("BLE has been disabled").show()
                        imageView.setColorFilter(ContextCompat.getColor( this, R.color.white), android.graphics.PorterDuff.Mode.MULTIPLY);
                    }
                }else if(resultCode == Activity.RESULT_CANCELED) {
                    toast("BLE enabling has been canceled").show()
                }
            }

0 个答案:

没有答案