@@ -30,8 +30,8 @@ import androidx.appcompat.app.AlertDialog
30
30
import androidx.fragment.app.Fragment
31
31
import androidx.lifecycle.lifecycleScope
32
32
import chip.devicecontroller.AttestationInfo
33
- import chip.devicecontroller.DeviceAttestationDelegate.DeviceAttestationCompletionCallback
34
- import chip.devicecontroller.DeviceAttestationDelegate.DeviceAttestationFailureCallback
33
+ import chip.devicecontroller.ChipDeviceController
34
+ import chip.devicecontroller.DeviceAttestationDelegate
35
35
import chip.devicecontroller.NetworkCredentials
36
36
import com.google.chip.chiptool.NetworkCredentialsParcelable
37
37
import com.google.chip.chiptool.ChipClient
@@ -57,15 +57,23 @@ class DeviceProvisioningFragment : Fragment() {
57
57
private val networkCredentialsParcelable: NetworkCredentialsParcelable ?
58
58
get() = arguments?.getParcelable(ARG_NETWORK_CREDENTIALS )
59
59
60
+ private lateinit var deviceController: ChipDeviceController
61
+
60
62
private lateinit var scope: CoroutineScope
61
63
64
+ override fun onCreate (savedInstanceState : Bundle ? ) {
65
+ super .onCreate(savedInstanceState)
66
+ deviceController = ChipClient .getDeviceController(requireContext())
67
+ }
68
+
62
69
override fun onCreateView (
63
70
inflater : LayoutInflater ,
64
71
container : ViewGroup ? ,
65
72
savedInstanceState : Bundle ?
66
73
): View {
67
74
scope = viewLifecycleOwner.lifecycleScope
68
75
deviceInfo = checkNotNull(requireArguments().getParcelable(ARG_DEVICE_INFO ))
76
+
69
77
return inflater.inflate(R .layout.single_fragment_container, container, false ).apply {
70
78
if (savedInstanceState == null ) {
71
79
if (deviceInfo.ipAddress != null ) {
@@ -82,13 +90,65 @@ class DeviceProvisioningFragment : Fragment() {
82
90
gatt = null
83
91
}
84
92
93
+ override fun onDestroy () {
94
+ super .onDestroy()
95
+ deviceController.close()
96
+ deviceController.setDeviceAttestationDelegate(0 , EmptyAttestationDelegate ())
97
+ }
98
+
99
+ private class EmptyAttestationDelegate : DeviceAttestationDelegate {
100
+ override fun onDeviceAttestationCompleted (
101
+ devicePtr : Long ,
102
+ attestationInfo : AttestationInfo ,
103
+ errorCode : Int ) {}
104
+ }
105
+
106
+ private fun setAttestationDelegate () {
107
+ deviceController.setDeviceAttestationDelegate(DEVICE_ATTESTATION_FAILED_TIMEOUT
108
+ ) { devicePtr, attestationInfo, errorCode ->
109
+ Log .i(TAG , " Device attestation errorCode: $errorCode , " +
110
+ " Look at 'src/credentials/attestation_verifier/DeviceAttestationVerifier.h' " +
111
+ " AttestationVerificationResult enum to understand the errors" )
112
+
113
+ val activity = requireActivity()
114
+
115
+ if (errorCode == STATUS_PAIRING_SUCCESS ) {
116
+ activity.runOnUiThread(Runnable {
117
+ deviceController.continueCommissioning(devicePtr, true )
118
+ })
119
+
120
+ return @setDeviceAttestationDelegate
121
+ }
122
+
123
+ activity.runOnUiThread(Runnable {
124
+ val dialog = AlertDialog .Builder (activity)
125
+ .setPositiveButton(" Continue" ,
126
+ DialogInterface .OnClickListener { dialog, id ->
127
+ deviceController.continueCommissioning(devicePtr, true )
128
+ })
129
+ .setNegativeButton(" No" ,
130
+ DialogInterface .OnClickListener { dialog, id ->
131
+ deviceController.continueCommissioning(devicePtr, false )
132
+ })
133
+ .setTitle(" Device Attestation" )
134
+ .setMessage(" Device Attestation failed for device under commissioning. Do you wish to continue pairing?" )
135
+ .create()
136
+
137
+ dialog.show()
138
+ })
139
+ }
140
+ }
141
+
85
142
private fun pairDeviceWithAddress () {
86
143
// IANA CHIP port
87
144
val port = 5540
88
145
val id = DeviceIdUtil .getNextAvailableId(requireContext())
89
- val deviceController = ChipClient .getDeviceController(requireContext())
146
+
90
147
DeviceIdUtil .setNextAvailableId(requireContext(), id + 1 )
91
148
deviceController.setCompletionListener(ConnectionCallback ())
149
+
150
+ setAttestationDelegate()
151
+
92
152
deviceController.pairDeviceWithAddress(
93
153
id,
94
154
deviceInfo.ipAddress,
@@ -103,9 +163,7 @@ class DeviceProvisioningFragment : Fragment() {
103
163
if (gatt != null ) {
104
164
return
105
165
}
106
-
107
166
scope.launch {
108
- val deviceController = ChipClient .getDeviceController(requireContext())
109
167
val bluetoothManager = BluetoothManager ()
110
168
111
169
showMessage(
@@ -135,36 +193,14 @@ class DeviceProvisioningFragment : Fragment() {
135
193
if (wifi != null ) {
136
194
network = NetworkCredentials .forWiFi(NetworkCredentials .WiFiCredentials (wifi.ssid, wifi.password))
137
195
}
196
+
138
197
val thread = networkParcelable.threadCredentials
139
198
if (thread != null ) {
140
199
network = NetworkCredentials .forThread(NetworkCredentials .ThreadCredentials (thread.operationalDataset))
141
200
}
142
- deviceController.setDeviceAttestationFailureCallback(DEVICE_ATTESTATION_FAILED_TIMEOUT
143
- ) { devicePtr, errorCode ->
144
- Log .i(TAG , " Device attestation errorCode: $errorCode , " +
145
- " Look at 'src/credentials/attestation_verifier/DeviceAttestationVerifier.h' " +
146
- " AttestationVerificationResult enum to understand the errors" )
147
- requireActivity().runOnUiThread(Runnable {
148
- val alertDialog: AlertDialog ? = activity?.let {
149
- val builder = AlertDialog .Builder (it)
150
- builder.apply {
151
- setPositiveButton(" Continue" ,
152
- DialogInterface .OnClickListener { dialog, id ->
153
- deviceController.continueCommissioning(devicePtr, true )
154
- })
155
- setNegativeButton(" No" ,
156
- DialogInterface .OnClickListener { dialog, id ->
157
- deviceController.continueCommissioning(devicePtr, false )
158
- })
159
- }
160
- builder.setTitle(" Device Attestation" )
161
- builder.setMessage(" Device Attestation failed for device under commissioning. Do you wish to continue pairing?" )
162
- // Create the AlertDialog
163
- builder.create()
164
- }
165
- alertDialog?.show()
166
- })
167
- }
201
+
202
+ setAttestationDelegate()
203
+
168
204
deviceController.pairDevice(gatt, connId, deviceId, deviceInfo.setupPinCode, network)
169
205
DeviceIdUtil .setNextAvailableId(requireContext(), deviceId + 1 )
170
206
}
0 commit comments