Skip to content

Instantly share code, notes, and snippets.

View justtwago's full-sized avatar
📱

Artyom Vlasov justtwago

📱
View GitHub Profile
class Adapter : ListAdapter<Item, ItemViewHolder>(DiffUtilCallback) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SpotViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false)
return ItemViewHolder(view)
}
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
holder.bind(currentList[position])
}
fun renderItems(newItems: List){
val diffResult: DiffUtil.DiffResult = DiffUtil.calculateDiff(DiffUtilCallback(oldItems, newItems))
diffResult.dispatchUpdatesTo(this) // this : RecyclerView.Adapter
}
class DiffUtilCallback(
private val oldItems: List,
private val newItems: List
) : DiffUtil.Callback() {
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return oldItems[oldItemPosition].id == newItems[newItemPosition].id
}
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
// It works properly if Item is a data class
dependencies {
implementation 'com.google.android.gms:play-services-location:17.0.0'
}
val deviceNameUUID = UUID.fromString("00002a00-0000-1000-8000-00805f9b34fb")
val appearanceUUID = UUID.fromString("00002a01-0000-1000-8000-00805f9b34fb")
val batteryLevelUUID = UUID.fromString("00002a19-0000-1000-8000-00805f9b34fb")
val modelNumberUUID = UUID.fromString("00002A24-0000-1000-8000-00805f9b34fb")
val serialNumberUUID = UUID.fromString("00002A25-0000-1000-8000-00805f9b34fb")
val firmwareRevisionUUID = UUID.fromString("00002A26-0000-1000-8000-00805f9b34fb")
val hardwareRevisionUUID = UUID.fromString("00002A27-0000-1000-8000-00805f9b34fb")
val softwareRevisionUUID = UUID.fromString("00002A28-0000-1000-8000-00805f9b34fb")
val manufacturerNameUUID = UUID.fromString("00002A29-0000-1000-8000-00805f9b34fb")
fun BluetoothSocket.setSocketAndStartConnector() {
val socketThread: Thread = object : Thread() {
override fun run() {
try {
BluetoothAdapter.getDefaultAdapter().cancelDiscovery()
if (!isConnected) connect()
inputStream
// read the stream here
private fun BluetoothDevice.createSocket(uuid: UUID): BluetoothSocket {
return createInsecureRfcommSocketToServiceRecord(uuid)
}
private val BluetoothDevice.isDeviceBonded get() = bondState == BluetoothDevice.BOND_BONDED
private var leScanCallback: ScanCallback? = null
private fun startLEScan(doOnDeviceFound: (BluetoothDevice?) -> Unit) {
leScanCallback = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult) {
doOnDeviceFound(result.device)
}
}
BluetoothAdapter.getDefaultAdapter().bluetoothLeScanner.startScan(leScanCallback)
}
private fun scanDevices(
context: Context,
doOnDeviceFound: (BluetoothDevice?) -> Unit,
doOnDiscoveryFinished: () -> Unit
) {
val scanningBroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
BluetoothDevice.ACTION_FOUND -> {
val device = intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)