【发布时间】:2018-08-06 01:51:23
【问题描述】:
我想在我的项目中使用蓝牙打印机,但是我收到了这个错误
lateinit property mmDevice has been not initialized
这是我的代码
lateinit var device:String
lateinit var mBluetoothAdapter: BluetoothAdapter
lateinit var mmSocket: BluetoothSocket
lateinit var mmDevice: BluetoothDevice
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_penjualan_cetak)
device = Function().getShared("printer","",this)
try {
findBT()
} catch (e: Exception) {
}
}
fun findBT(){
try{
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
val paireddevice = mBluetoothAdapter.bondedDevices
if(paireddevice.size > 0){
ePrinter.setText("Printer Belum Dipilih")
for (device:BluetoothDevice in paireddevice) {
if (device.name == this.device) {
// this is the error come from
mmDevice = device
break
}
}
}
}catch (e:Exception){
e.printStackTrace()
}
}
如何在 koltin 中初始化蓝牙设备?,我尝试了一些解决方案,但它不起作用
【问题讨论】:
-
您确定错误来自这一行吗?您没有在代码中的任何其他位置使用
mmDevice? -
同意@Christopher。无论如何,您可以将 mmDevice 更改为可空类型并摆脱 lateinit: var mmDevice: BluetoothDevice? = null
标签: android bluetooth kotlin kotlin-lateinit