Kotlin中分配的变量无法正确解析

时间:2019-11-27 12:25:46

标签: android kotlin java.util.scanner ibeacon

我遇到一个问题,其中“ {不要将Android上下文类放在静态字段中”突出显示var instance: IbeaconTooth? = null。我尽力解决该问题,但似乎无济于事。每当我运行该应用程序时,我收到的错误是“ ibeantooth为null,请使用init()方法”。感谢您的协助。

IbeaconTooth.kt

class IbeaconTooth constructor(mContext: Context) {

        private val cxt: Context = mContext
        private val manager: BluetoothManager = cxt.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
        private val adapter: BluetoothAdapter = manager.adapter
        private var scanner: BluetoothLeScanner? = null
        private var isScan: Boolean = false
        private val TAG = "beacon"
        // scan callback
        private var bleCallback: BeaconBleCallback? = null
        private var leCallback: BeaconLeCallback? = null

        init {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                scanner = adapter.bluetoothLeScanner
            }
        }

        companion object {

            var instance: IbeaconTooth? = null

            fun init(context: Context) {
                if (instance == null) {
                    synchronized(IbeaconTooth::class.java) {
                        if (instance == null) {
                            instance = IbeaconTooth(context)
                        }
                    }
                }
            }

            fun getIbeacon(): IbeaconTooth {
                if (instance == null) {
                    throw IllegalArgumentException("ibeantooth is null , please use init() method")
                }
                return instance as IbeaconTooth
            }

        }

        fun startBeacon(onBeaconScanListener: OnBeaconScanListener) {
            if (isScan) {
                Log.d(TAG, "already start scan")
                onBeaconScanListener.onScanErrorMsg("start scan already")
            } else {
                if (scanner == null) {
                    bleCallback = BeaconBleCallback(onBeaconScanListener)
                    adapter.startLeScan(bleCallback)
                } else {
                    // android 5.0
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        leCallback = BeaconLeCallback(onBeaconScanListener)
                        scanner?.startScan(leCallback)
                    }
                }
            }
        }

        fun stopBeacon() {
            if (isScan) {
                // stop scan
                if (scanner == null) {
                    if (bleCallback != null) {
                        adapter.stopLeScan(bleCallback)
                    }
                } else {
                    if (leCallback != null) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            scanner?.stopScan(leCallback)
                        }
                    }
                }
            } else {
                Log.d(TAG, "already stop scan")
            }
        }
    }

ActivateBeacon.kt

class ActivateBeacon : AppCompatActivity(), OnBeaconScanListener {
    private lateinit var rippleView: RippleView

    override fun OnScanResult(beacon: Beacon) {
        Log.v("beacon", "result > $beacon")
        rippleView.newRipple()

    }

    override fun onScanErrorMsg(msg: String) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
    }

    override fun getScanFilter(): BeaconFilter = BeaconFilter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_ibeacon)
        rippleView = findViewById(R.id.circle_ripple)
        rippleView.newRipple()
    }
    override fun onResume() {
        super.onResume()
        IbeaconTooth.getIbeacon().startBeacon(this)

    }

    override fun onDestroy() {
        super.onDestroy()
        IbeaconTooth.getIbeacon().stopBeacon()
    }

    override fun onBackPressed() {
        finish()
    }
}

1 个答案:

答案 0 :(得分:0)

    companion object {

        var instance: IbeaconTooth? = null

        fun init(context: Context) {
            if (instance == null) {
                synchronized(IbeaconTooth::class.java) {
                    if (instance == null) {
                        instance = IbeaconTooth(context)
                    }
                }
            }
        }

        fun getIbeacon(context:Context): IbeaconTooth {
            if (instance == null) {
                instance = init(context)
            }
            return instance as IbeaconTooth
        }

    }

这将消除在获取之前调用init的开销。