package Ġno . n ord ic semi . android . ble . k tx Ċ Ċ import Ġandroid . bluetooth . Bluetooth Device Ċ import Ġkotlinx . coroutines . suspend C ancellable Coroutine Ċ import Ġno . n ord ic semi . android . ble .* Ċ import Ġno . n ord ic semi . android . ble . callback . Fail Callback Ċ import Ġno . n ord ic semi . android . ble . callback . profile . Profile Read Response Ċ import Ġno . n ord ic semi . android . ble . data . Data Ċ import Ġno . n ord ic semi . android . ble . exception .* Ċ import Ġno . n ord ic semi . android . ble . response . Read Response Ċ import Ġno . n ord ic semi . android . ble . response . Write Response Ċ import Ġkotlin . coroutines . resume Ċ import Ġkotlin . coroutines . resume With Exception Ċ Ċ /** Ċ Ġ* ĠS usp ends Ġthe Ġcoroutine Ġuntil Ġthe Ġrequest Ġis Ġcompleted . Ċ Ġ*/ Ċ @ Throws ( Ċ ĉ Bluetooth Disabled Exception :: class , Ċ ĉ Device Disconnected Exception :: class , Ċ ĉ Request FailedException :: class , Ċ ĉ Invalid RequestException :: class Ċ ) Ċ suspend Ġfun ĠRequest . suspend () Ġ= Ġsuspend C ancellable () Ċ Ċ /** Ċ Ġ* ĠS usp ends Ġthe Ġcoroutine Ġuntil Ġthe Ġdata Ġhave Ġbeen Ġwritten . Ċ Ġ* Ġ@ return ĠThe Ġdata Ġwritten . Ċ Ġ*/ Ċ @ Throws ( Ċ ĉ Bluetooth Disabled Exception :: class , Ċ ĉ Device Disconnected Exception :: class , Ċ ĉ Request FailedException :: class , Ċ ĉ Invalid RequestException :: class Ċ ) Ċ suspend Ġfun ĠWrite Request . suspend (): ĠData Ġ{ Ċ ĉ var Ġresult : ĠData ? Ġ= Ġnull Ċ ĉ with Ġ{ Ġ_, Ġdata Ġ-> Ġresult Ġ= Ġdata Ġ}. suspend C ancellable () Ċ ĉ return Ġresult !! Ċ } Ċ Ċ /** Ċ Ġ* ĠS usp ends Ġthe Ġcoroutine Ġuntil Ġthe Ġdata Ġhave Ġbeen Ġwritten . Ċ Ġ* ĠThe Ġdata Ġsent Ġis Ġparsed Ġto Ġthe Ġgiven Ġtype . Ċ Ġ* Ċ Ġ* ĠUsage : Ċ Ġ* Ċ Ġ* ĠĠĠĠ Ġ// ĠAss uming ĠAlert Level Request Ġis Ġa Ġclass Ġsimilar Ġto ĠAlert Data Response Ġfrom Ġble - common Ċ Ġ* ĠĠĠĠ Ġ// Ġmodule , Ġbut Ġextends ĠWrite Response Ġinterface Ġinstead Ġof ĠProfile Read Response . Ċ Ġ* ĠĠĠĠ Ġval Ġresult : ĠAlert Level Request Ġ= Ġwrite Characteristic ( Ċ Ġ* ĠĠĠĠĠĠĠĠĠĠĠĠ Ġalert Level Characteristic , Ċ Ġ* ĠĠĠĠĠĠĠĠĠĠĠĠ ĠAlert Level Data . high Alert (), Ċ Ġ* ĠĠĠĠĠĠĠĠĠĠĠĠ ĠBluetooth GattCharacteristic . WRITE _ TYPE _ NO _ RESPONSE Ċ Ġ* ĠĠĠĠĠĠĠĠ Ġ). suspend For Response () Ċ Ġ* Ċ Ġ* Ġ@ return ĠThe Ġdata Ġwritten Ġparsed Ġto Ġrequired Ġtype . Ċ Ġ*/ Ċ @ Throws ( Ċ ĉ Bluetooth Disabled Exception :: class , Ċ ĉ Device Disconnected Exception :: class , Ċ ĉ Request FailedException :: class , Ċ ĉ Invalid RequestException :: class Ċ ) Ċ suspend Ġinline Ġfun Ġ< re ified ĠT : ĠWrite Response > ĠWrite Request . suspend For Response (): ĠT Ġ{ Ċ ĉ var Ġdevice : ĠBluetooth Device ? Ġ= Ġnull Ċ ĉ then Ġ{ Ġd Ġ-> Ġdevice Ġ= Ġd Ġ}. suspend (). let Ġ{ Ċĉ ĉ return ĠT :: class . java . newInstance (). apply Ġ{ Ġon Data Sent ( device !! , Ġit ) Ġ} Ċ ĉ } Ċ } Ċ Ċ /** Ċ Ġ* ĠS usp ends Ġthe Ġcoroutine Ġuntil Ġthe Ġdata Ġhave Ġbeen Ġread . Ċ Ġ* Ġ@ return ĠThe Ġdata Ġread . Ċ Ġ*/ Ċ @ Throws ( Ċ ĉ Bluetooth Disabled Exception :: class , Ċ ĉ Device Disconnected Exception :: class , Ċ ĉ Request FailedException :: class , Ċ ĉ Invalid RequestException :: class Ċ ) Ċ suspend Ġfun ĠRead Request . suspend (): ĠData Ġ{ Ċ ĉ var Ġresult : ĠData ? Ġ= Ġnull Ċ ĉ with Ġ{ Ġ_, Ġdata Ġ-> Ġresult Ġ= Ġdata Ġ}. suspend C ancellable () Ċ ĉ return Ġresult !! Ċ } Ċ Ċ /** Ċ Ġ* ĠS usp ends Ġthe Ġcoroutine Ġuntil Ġthe Ġdata Ġhave Ġbeen Ġread . Ċ Ġ* ĠThe Ġdata Ġread Ġis Ġparsed Ġto Ġthe Ġgiven Ġtype . Ċ Ġ* Ċ Ġ* ĠUsage : Ċ Ġ* Ċ Ġ* ĠĠĠĠ Ġval Ġresult : ĠAlert Level Response Ġ= Ġread Characteristic ( alert Level Characteristic ) Ċ Ġ* ĠĠĠĠĠĠĠĠ Ġ. suspend For Response () Ċ Ġ* Ċ Ġ* Ġ@ return ĠThe Ġdata Ġread Ġparsed Ġto Ġrequired Ġtype . Ċ Ġ*/ Ċ @ Throws ( Ċ ĉ Bluetooth Disabled Exception :: class , Ċ ĉ Device Disconnected Exception :: class , Ċ ĉ Request FailedException :: class , Ċ ĉ Invalid RequestException :: class Ċ ) Ċ suspend Ġinline Ġfun Ġ< re ified ĠT : ĠRead Response > ĠRead Request . suspend For Response (): ĠT Ġ{ Ċ ĉ var Ġdevice : ĠBluetooth Device ? Ġ= Ġnull Ċ ĉ then Ġ{ Ġd Ġ-> Ġdevice Ġ= Ġd Ġ}. suspend (). let Ġ{ Ċĉ ĉ return ĠT :: class . java . newInstance (). apply Ġ{ Ġon Data Received ( device !! , Ġit ) Ġ} Ċ ĉ } Ċ } Ċ Ċ /** Ċ Ġ* ĠS usp ends Ġthe Ġcoroutine Ġuntil Ġthe Ġdata Ġhave Ġbeen Ġread . Ċ Ġ* ĠThe Ġdata Ġread Ġis Ġparsed Ġto Ġthe Ġgiven Ġtype . Ċ Ġ* ĠIf Ġthe Ġreceived Ġdata Ġare Ġnot Ġvalid , Ġan Ġ[ Invalid DataException ] Ġis Ġthrown . Ċ Ġ* Ċ Ġ* ĠUsage : Ċ Ġ* Ċ Ġ* ĠĠĠĠ Ġval Ġresult : ĠAlert Level Response Ġ= Ġread Characteristic ( alert Level Characteristic ) Ċ Ġ* ĠĠĠĠĠĠĠĠ Ġ. suspend For Valid Response () Ċ Ġ* Ċ Ġ* Ġ@ return ĠThe Ġdata Ġread Ġparsed Ġto Ġrequired Ġtype . Ċ Ġ*/ Ċ @ Throws ( Ċ ĉ Bluetooth Disabled Exception :: class , Ċ ĉ Device Disconnected Exception :: class , Ċ ĉ Request FailedException :: class , Ċ ĉ Invalid RequestException :: class , Ċ ĉ Invalid DataException :: class Ċ ) Ċ suspend Ġinline Ġfun Ġ< re ified ĠT : ĠProfile Read Response > ĠRead Request . suspend For Valid Response (): ĠT Ġ{ Ċ ĉ val Ġresponse Ġ= Ġsuspend For Response < T >() Ċ ĉ return Ġresponse . take If Ġ{ Ġit . isValid Ġ} Ġ?: Ġthrow ĠInvalid DataException ( response ) Ċ } Ċ Ċ /** Ċ Ġ* ĠS usp ends Ġthe Ġcoroutine Ġuntil Ġthe ĠRSS I Ġvalue Ġis Ġreceived . Ċ Ġ* Ġ@ return ĠThe Ġcurrent ĠRSS I Ġvalue . Ċ Ġ*/ Ċ @ Throws ( Ċ ĉ Bluetooth Disabled Exception :: class , Ċ ĉ Device Disconnected Exception :: class , Ċ ĉ Request FailedException :: class , Ċ ĉ Invalid RequestException :: class Ċ ) Ċ suspend Ġfun ĠRead R ssi Request . suspend (): ĠInt Ġ{ Ċ ĉ var Ġresult : ĠInt ? Ġ= Ġnull Ċ ĉ with Ġ{ Ġ_, Ġr ssi Ġ-> Ġresult Ġ= Ġr ssi Ġ}. suspend C ancellable () Ċ ĉ return Ġresult !! Ċ } Ċ Ċ /** Ċ Ġ* ĠS usp ends Ġthe Ġcoroutine Ġuntil Ġthe ĠMT U Ġvalue Ġis Ġreceived . Ċ Ġ* Ġ@ return ĠThe Ġcurrent ĠMT U Ġvalue . Ċ Ġ*/ Ċ @ Throws ( Ċ ĉ Bluetooth Disabled Exception :: class , Ċ ĉ Device Disconnected Exception :: class , Ċ ĉ Request FailedException :: class , Ċ ĉ Invalid RequestException :: class Ċ ) Ċ suspend Ġfun ĠM tu Request . suspend (): ĠInt Ġ{ Ċ ĉ var Ġresult : ĠInt ? Ġ= Ġnull Ċ ĉ with Ġ{ Ġ_, Ġmt u Ġ-> Ġresult Ġ= Ġmt u Ġ}. suspend C ancellable () Ċ ĉ return Ġresult !! Ċ } Ċ Ċ /** Ċ Ġ* ĠS usp ends Ġthe Ġcoroutine Ġuntil Ġthe ĠTX Ġand ĠRX ĠPHY Ġvalues Ġare Ġreceived . Ċ Ġ* Ġ@ return ĠA Ġpair Ġof ĠTX Ġand ĠRX ĠPHY s . Ċ Ġ*/ Ċ @ Throws ( Ċ ĉ Bluetooth Disabled Exception :: class , Ċ ĉ Device Disconnected Exception :: class , Ċ ĉ Request FailedException :: class , Ċ ĉ Invalid RequestException :: class Ċ ) Ċ suspend Ġfun ĠP hy Request . suspend (): ĠPair < Int , ĠInt > Ġ{ Ċ ĉ var Ġresult : ĠPair < Int , ĠInt >? Ġ= Ġnull Ċ ĉ with Ġ{ Ġ_, Ġtx Phy , Ġrx Phy Ġ-> Ġresult Ġ= Ġtx Phy Ġto Ġrx Phy Ġ}. suspend C ancellable () Ċ ĉ return Ġresult !! Ċ } Ċ Ċ /** Ċ Ġ* ĠS usp ends Ġthe Ġcoroutine Ġuntil Ġthe Ġvalue Ġof Ġthe Ġattribute Ġhas Ġchanged . Ċ Ġ* Ġ@ return ĠThe Ġnew Ġvalue Ġof Ġthe Ġattribute . Ċ Ġ*/ Ċ @ Throws ( Ċ ĉ Bluetooth Disabled Exception :: class , Ċ ĉ Device Disconnected Exception :: class , Ċ ĉ Request FailedException :: class , Ċ ĉ Invalid RequestException :: class Ċ ) Ċ suspend Ġfun ĠWaitFor ValueChanged Request . suspend (): ĠData Ġ Ġ= Ġsuspend C ancellable Coroutine Ġ{ Ġcontinuation Ġ-> ĉ this Ċ ĉ . with Ġ{ Ġ_, Ġdata Ġ-> Ġcontinuation . resume ( data ) Ġ} Ċ ĉ . invalid Ġ{ Ġcontinuation . resume With Exception ( Invalid RequestException ( this )) Ġ} Ċ ĉ . fail Ġ{ Ġ_, Ġstatus Ġ-> Ċĉ ĉ val Ġexception Ġ= Ġwhen Ġ( status ) Ġ{ Ċĉĉ ĉ Fail Callback . REASON _ BL U ETO OTH _ DISABLED Ġ-> ĠBluetooth Disabled Exception () Ċĉĉ ĉ Fail Callback . REASON _ DEVICE _ DIS CONNECTED Ġ-> ĠDevice Disconnected Exception () Ċĉĉ ĉ else Ġ-> ĠRequest FailedException ( this , Ġstatus ) Ċĉ ĉ } Ċĉ ĉ continu ation . resume With Exception ( exception ) Ċ ĉ } Ċ ĉ . enqueue () Ċ } Ċ Ċ /** Ċ Ġ* ĠS usp ends Ġthe Ġcoroutine Ġuntil Ġthe Ġvalue Ġof Ġthe Ġattribute Ġhas Ġchanged . Ċ Ġ* Ċ Ġ* ĠUsage : Ċ Ġ* Ċ Ġ* ĠĠĠĠ Ġval Ġresult : ĠHeart Rate Measurement Response Ġ= ĠwaitFor Notification ( h mr Characteristic ) Ċ Ġ* ĠĠĠĠĠĠĠĠ Ġ. suspend For Response () Ċ Ġ* Ċ Ġ* Ġ@ return ĠThe Ġnew Ġvalue Ġof Ġthe Ġattribute . Ċ Ġ*/ Ċ @ Throws ( Ċ ĉ Bluetooth Disabled Exception :: class , Ċ ĉ Device Disconnected Exception :: class , Ċ ĉ Request FailedException :: class , Ċ ĉ Invalid RequestException :: class Ċ ) Ċ suspend Ġinline Ġfun Ġ< re ified ĠT : ĠRead Response > ĠWaitFor ValueChanged Request . suspend For Response (): ĠT Ġ{ Ċ ĉ var Ġdevice : ĠBluetooth Device ? Ġ= Ġnull Ċ ĉ then Ġ{ Ġd Ġ-> Ġdevice Ġ= Ġd Ġ}. suspend (). let Ġ{ Ċĉ ĉ return ĠT :: class . java . newInstance (). apply Ġ{ Ġon Data Received ( device !! , Ġit ) Ġ} Ċ ĉ } Ċ } Ċ Ċ /** Ċ Ġ* ĠS usp ends Ġthe Ġcoroutine Ġuntil Ġthe Ġvalue Ġof Ġthe Ġattribute Ġhas Ġchanged Ġand Ġis Ġvalid . Ċ Ġ* ĠIf Ġthe Ġvalue Ġis Ġinvalid , Ġan Ġ[ Invalid DataException ] Ġis Ġthrown . Ċ Ġ* Ċ Ġ* ĠUsage : Ċ Ġ* Ċ Ġ* ĠĠĠĠ Ġval Ġresult : ĠHeart Rate Measurement Response Ġ= ĠwaitFor Notification ( h mr Characteristic ) Ċ Ġ* ĠĠĠĠĠĠĠĠ Ġ. suspend For Valid Response () Ċ Ġ* Ċ Ġ* Ġ@ return ĠThe Ġnew Ġvalue Ġof Ġthe Ġattribute . Ċ Ġ*/ Ċ @ Throws ( Ċ ĉ Bluetooth Disabled Exception :: class , Ċ ĉ Device Disconnected Exception :: class , Ċ ĉ Request FailedException :: class , Ċ ĉ Invalid RequestException :: class , Ċ ĉ Invalid DataException :: class Ċ ) Ċ suspend Ġinline Ġfun Ġ< re ified ĠT : ĠProfile Read Response > ĠWaitFor ValueChanged Request . suspend For Valid Response (): ĠT Ġ{ Ċ ĉ val Ġresponse Ġ= Ġsuspend For Response < T >() Ċ ĉ return Ġresponse . take If Ġ{ Ġit . isValid Ġ} Ġ?: Ġthrow ĠInvalid DataException ( response ) Ċ } Ċ Ċ /** Ċ Ġ* ĠS usp ends Ġthe Ġcoroutine Ġuntil Ġthe Ġvalue Ġof Ġthe Ġattribute Ġhas Ġchanged . Ċ Ġ* Ġ@ return ĠThe Ġnew Ġvalue Ġof Ġthe Ġattribute . Ċ Ġ*/ Ċ @ Throws ( Ċ ĉ Bluetooth Disabled Exception :: class , Ċ ĉ Device Disconnected Exception :: class , Ċ ĉ Request FailedException :: class , Ċ ĉ Invalid RequestException :: class Ċ ) Ċ suspend Ġfun ĠWaitFor Read Request . suspend (): ĠData Ġ Ġ= Ġsuspend C ancellable Coroutine Ġ{ Ġcontinuation Ġ-> ĉ this Ċ ĉ . with Ġ{ Ġ_, Ġdata Ġ-> Ġcontinuation . resume ( data ) Ġ} Ċ ĉ . invalid Ġ{ Ġcontinuation . resume With Exception ( Invalid RequestException ( this )) Ġ} Ċ ĉ . fail Ġ{ Ġ_, Ġstatus Ġ-> Ċĉ ĉ val Ġexception Ġ= Ġwhen Ġ( status ) Ġ{ Ċĉĉ ĉ Fail Callback . REASON _ BL U ETO OTH _ DISABLED Ġ-> ĠBluetooth Disabled Exception () Ċĉĉ ĉ Fail Callback . REASON _ DEVICE _ DIS CONNECTED Ġ-> ĠDevice Disconnected Exception () Ċĉĉ ĉ else Ġ-> ĠRequest FailedException ( this , Ġstatus ) Ċĉ ĉ } Ċĉ ĉ continu ation . resume With Exception ( exception ) Ċ ĉ } Ċ ĉ . enqueue () Ċ } Ċ Ċ /** Ċ Ġ* ĠS usp ends Ġthe Ġcoroutine Ġuntil Ġthe Ġremote Ġdevice Ġreads Ġthe Ġvalue Ġand Ġthe Ġresponse Ġis Ġsent . Ċ Ġ* Ċ Ġ* ĠUsage : Ċ Ġ* Ċ Ġ* ĠĠĠĠ Ġ// ĠAss uming ĠAlert Level Request Ġis Ġa Ġclass Ġsimilar Ġto ĠAlert Data Response Ġfrom Ġble - common Ċ Ġ* ĠĠĠĠ Ġ// Ġmodule , Ġbut Ġextends ĠWrite Response Ġinterface Ġinstead Ġof ĠProfile Read Response . Ċ Ġ* ĠĠĠĠ Ġval Ġresult : ĠAlert Level Request Ġ= ĠwaitFor Read ( alert Level Characteristic ) Ċ Ġ* ĠĠĠĠĠĠĠĠ Ġ. suspend For Response () Ċ Ġ* Ċ Ġ* Ġ@ return ĠThe Ġnew Ġvalue Ġof Ġthe Ġattribute . Ċ Ġ*/ Ċ @ Throws ( Ċ ĉ Bluetooth Disabled Exception :: class , Ċ ĉ Device Disconnected Exception :: class , Ċ ĉ Request FailedException :: class , Ċ ĉ Invalid RequestException :: class Ċ ) Ċ suspend Ġinline Ġfun Ġ< re ified ĠT : ĠWrite Response > ĠWaitFor Read Request . suspend For Response (): ĠT Ġ{ Ċ ĉ var Ġdevice : ĠBluetooth Device ? Ġ= Ġnull Ċ ĉ then Ġ{ Ġd Ġ-> Ġdevice Ġ= Ġd Ġ}. suspend (). let Ġ{ Ċĉ ĉ return ĠT :: class . java . newInstance (). apply Ġ{ Ġon Data Sent ( device !! , Ġit ) Ġ} Ċ ĉ } Ċ } Ċ Ċ private Ġsuspend Ġfun ĠRequest . suspend C ancellable (): ĠUnit Ġ= Ġsuspend C ancellable Coroutine Ġ{ Ġcontinuation Ġ-> Ġthis Ċ ĉ . done Ġ{ Ġcontinuation . resume ( Unit ) Ġ} Ċ ĉ . invalid Ġ{ Ġcontinuation . resume With Exception ( Invalid RequestException ( this )) Ġ} Ċ ĉ . fail Ġ{ Ġ_, Ġstatus Ġ-> Ċĉ ĉ val Ġexception Ġ= Ġwhen Ġ( status ) Ġ{ Ċĉĉ ĉ Fail Callback . REASON _ BL U ETO OTH _ DISABLED Ġ-> ĠBluetooth Disabled Exception () Ċĉĉ ĉ Fail Callback . REASON _ DEVICE _ DIS CONNECTED Ġ-> ĠDevice Disconnected Exception () Ċĉĉ ĉ else Ġ-> ĠRequest FailedException ( this , Ġstatus ) Ċĉ ĉ } Ċĉ ĉ continu ation . resume With Exception ( exception ) Ċ ĉ } Ċ ĉ . enqueue () Ċ }