This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun renderItems(newItems: List){ | |
val diffResult: DiffUtil.DiffResult = DiffUtil.calculateDiff(DiffUtilCallback(oldItems, newItems)) | |
diffResult.dispatchUpdatesTo(this) // this : RecyclerView.Adapter | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
implementation 'com.google.android.gms:play-services-location:17.0.0' | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun BluetoothSocket.setSocketAndStartConnector() { | |
val socketThread: Thread = object : Thread() { | |
override fun run() { | |
try { | |
BluetoothAdapter.getDefaultAdapter().cancelDiscovery() | |
if (!isConnected) connect() | |
inputStream | |
// read the stream here |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun BluetoothDevice.createSocket(uuid: UUID): BluetoothSocket { | |
return createInsecureRfcommSocketToServiceRecord(uuid) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private val BluetoothDevice.isDeviceBonded get() = bondState == BluetoothDevice.BOND_BONDED |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |