Skip to content

Instantly share code, notes, and snippets.

@tkubasik-luna
tkubasik-luna / scanDevice.kt
Last active September 14, 2022 18:15
scan device
val callback = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult?) {
super.onScanResult(callbackType, result)
result?.device?.connectGatt(context, false, gattCallBack)
}
}
adapter = BluetoothAdapter.getDefaultAdapter()
val builder = ScanFilter.Builder()
builder.setServiceUuid(ParcelUuid(SERVICE_UUID))
@tkubasik-luna
tkubasik-luna / gattCallbackClient.kt
Last active October 20, 2021 10:29
gatt callback
val gattCallBack = object : BluetoothGattCallback() {
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
super.onConnectionStateChange(gatt, status, newState)
val isSuccess = status == BluetoothGatt.GATT_SUCCESS
val isConnected = newState == BluetoothProfile.STATE_CONNECTED
if (isSuccess && isConnected) {
// Discover the service on the server on are connected on
gatt.discoverServices()
}
}