val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
val pairedDevice = bluetoothAdapter.bondedDevices
if (pairedDevice.isNotEmpty()) {
val device = pairedDevice.first()
val mmSocket: BluetoothSocket = device.javaClass.getMethod(
"createRfcommSocket", Int::class.javaPrimitiveType
).invoke(device, 1) as BluetoothSocket // I had problem with connection with my device and this is only way to connect via Bluetooth
try {
if (mmSocket.isConnected) mmSocket.close() else mmSocket.connect()
readData(mmSocket)
} catch (ex: Exception) {
mmSocket.close()
}
}
和读取数据功能:
while (mmSocket.isConnected) {
val inputStream = mmSocket.inputStream
val available = inputStream.available()
val bytes = ByteArray(available)
inputStream.read(bytes, 0, available)
val barcode = String(bytes)
if (barcode.isNotBlank()) {
withContext(Dispatchers.Main) {
textView?.text = barcode //display result on UI
}
}
}
2条答案
按热度按时间1l5u6lss1#
您可以覆盖
onKeyDown
在里面Activity
并检查按下了哪些键在这里你可以检查所有的代码,除此之外还有
onKeyUp
方法,可能有用。波沃泽尼亚7nbnzgx92#
好的,可以从其他设备获取数据而不使用活动。以下是我的工作:
和读取数据功能: