package Ġcom . app . near by ble . ui . ble Client Ċ Ċ import Ġandroid . os . Bundle Ċ import Ġandroid . util . Log Ċ import Ġandroid . view . LayoutInflater Ċ import Ġandroid . view . View Ċ import Ġandroid . view . ViewGroup Ċ import Ġandroid . widget . Toast Ċ Ċ import Ġandroidx . fragment . app . Fragment Ċ import Ġandroidx . databinding . Data Binding Util Ċ import Ġandroidx . lifecycle . ViewModel Provider Ċ import Ġandroidx . recyclerview . widget . Linear LayoutManager Ċ import Ġandroidx . recyclerview . widget . RecyclerView Ċ Ċ import Ġcom . app . near by ble .* Ċ import Ġcom . app . near by ble . database . B le Device Database Ċ import Ġcom . app . near by ble . databinding . B le Client Frag Binding Ċ import Ġcom . app . near by ble . util . Permissions Helper Ċ Ċ /** Ċ Ġ* ĠA Ġfragment Ġcontaining Ġthe Ġlist Ġof Ġdevices Ġdiscovered Ċ Ġ*/ Ċ class ĠB le Client Fragment Ġ: ĠFragment () Ġ{ ĊĊĠĠĠ Ġprivate Ġval ĠTAG Ġ= ĠB le Client Fragment :: class . java . simple Name ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġble Client ViewModel Ġ: ĠB le Client ViewModel ĊĊĠĠĠ Ġoverride Ġfun ĠonCreate View ( ĊĠĠĠĠĠĠĠ Ġinflater : ĠLayoutInflater , Ġcontainer : ĠViewGroup ?, ĊĠĠĠĠĠĠĠ ĠsavedInstanceState : ĠBundle ? ĊĠĠĠ Ġ): ĠView Ġ{ ĊĊĠĠĠĠĠĠĠ Ġval Ġbinding Ġ= ĠData Binding Util . inflate < B le Client Frag Binding >( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġinflater , ĊĠĠĠĠĠĠĠĠĠĠĠ ĠR . layout . ble _ client _ frag , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcontainer , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġfalse ĊĠĠĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠĠĠ Ġ// application Ġneeded Ġto Ġconstruct Ġandroid Ġview Ġmodel ĊĠĠĠĠĠĠĠ Ġval Ġapplication Ġ= Ġrequire NotNull ( this . activity ). application ĊĠĠĠĠĠĠĠ Ġ// database Ġaccess Ġpoint Ġfor Ġview Ġmodel ĊĠĠĠĠĠĠĠ Ġval Ġdatabase Ġ= ĠB le Device Database . getInstance ( application ). ble Device Dao ĊĊĠĠĠĠĠĠĠ Ġ// permission Ġhelper Ġto Ġask Ġpermissions Ġand Ġenable Ġbluetooth ĊĠĠĠĠĠĠĠ Ġval Ġpermission Helper Ġ= ĠPermissions Helper ( this . require Activity ()) ĊĊĠĠĠĠĠĠĠ Ġval Ġble Client ViewModel Factory Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠ ĠB le Client ViewModel Factory ( database , Ġapplication , Ġpermission Helper ) ĊĠĠĠĠĠĠĠ Ġble Client ViewModel Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠ ĠViewModel Provider ( this , Ġble Client ViewModel Factory ). get ( B le Client ViewModel :: class . java ) ĊĊĠĠĠĠĠĠĠ Ġbinding . ble Client ViewModel Ġ= Ġble Client ViewModel ĊĠĠĠĠĠĠĠ Ġbinding . lifecycle Owner Ġ= Ġthis ĊĊĠĠĠĠĠĠĠ Ġ// set Ġup Ġre cycler Ġview Ġwith Ġcustom Ġadapter ĊĠĠĠĠĠĠĠ Ġval Ġmanager Ġ= ĠLinearLayoutManager ( this . context , ĠRecyclerView . VERTICAL , Ġfalse ) ĊĠĠĠĠĠĠĠ Ġbinding . devices List . layout Manager Ġ= Ġmanager ĊĊĠĠĠĠĠĠĠ Ġval Ġadapter Ġ= ĠB le Devices Adapter ( B le Devices Listener Ġ{ Ġble Device Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠ ĠToast . makeText ( application , Ġ" device Ġid : Ġ${ ble Device . device ID }", ĠToast . LENGTH _ SHORT ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. show () ĊĠĠĠĠĠĠĠ Ġ}) ĊĠĠĠĠĠĠĠ Ġbinding . devices List . adapter Ġ= Ġadapter ĊĊĠĠĠĠĠĠĠ Ġ// update Ġthe Ġlist Ġfrom Ġdatabase ĊĠĠĠĠĠĠĠ Ġble Client ViewModel . devices . observe ( view Lifecycle Owner , Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġit ?. let Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġadapter . submit L ew List ( it ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ}) ĊĊĠĠĠĠĠĠĠ Ġble Client ViewModel . show Toast . observe ( view Lifecycle Owner , Ġ{ Ġmessage Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠ Ġmessage ?. let Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠToast . makeText ( application , Ġmessage , ĠToast . LENGTH _ LONG ). show () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġble Client ViewModel . show M Toast Done () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ}) ĊĊĠĠĠĠĠĠĠ Ġreturn Ġbinding . root ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon Stop () Ġ{ ĊĠĠĠĠĠĠĠ Ġsuper . on Stop () ĊĠĠĠĠĠĠĠ Ġble Client ViewModel . stop () ĊĠĠĠ Ġ} Ċ }