I have an issue where var instance: IbeaconTooth? = null
var instance: IbeaconTooth? = null
is highlighted with "Do not place Android context classes in static fields". I tried everything I can think of to resolve this issue but nothing seems to work. Whenever I run the app, the error I receive is "ibeantooth is null , please use init() method". Any assistance is appreciated.
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()
}
}
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
}
}
This will eliminate the overhead of calling init before get.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.