Binding the ARxAPI
4 min
Make sure your Main Activity implements the ARxAPI:
class MainActivity : ArxActivity() {Add the following functions overrides:
override fun setOnPreviewFrameListener(cameraInput: ByteArray) {
}
override fun onButtonClick(button: ArxButton) {
}
override fun onButtonLongClick(button: ArxButton) {
}
override fun onDeviceAttach() {
super.onDeviceAttach()
}
override fun onDeviceDetach() {
super.onDeviceDetach()
}
In order for the ArxApi to be initialized, you must reference a UVCCameraTextureView in your layout. This is the component you must add to your layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.serenegiant.usb.widget.UVCCameraTextureView
android:id="@+id/camera_view"
android:layout_width="wrap_content"
android:layout_height="400dp"
android:visibility="visible"
app:layout_anchorGravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
In order to initialize the API you must call the following function, passing the UVCCameraTextureView:
arxApi.init(findViewById<UVCCameraTextureView>(R.id.camera_view))UVCCameraTextureView is a widget that must be present in your main activity's layout. Here is an example of it integrated into the layout file:
In order to access the headset, you must make sure that the user has granted permissions.
Here's an example of how to do so:
private val permissionArray = arrayOf(
Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO
)
private val activityResultLauncher =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
// Handle Permission granted/rejected
if (permissions.entries.filter { it.value == true }.size == permissionArray.size) {
// Initialize the arx api after the Camera And RECORD_AUDIO permission is given
arxApi.init(findViewById<UVCCameraTextureView>(R.id.camera_view))
} else {
Snackbar.make(
findViewById<UVCCameraTextureView>(R.id.camera_view),
"Please provide all the required Permission",
Snackbar.LENGTH_LONG
).setAction("Setting") {
val intent = Intent(
Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.fromParts("package", packageName, null)
)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}.show()
}
}
Then in order to request permissions and start the headset, call:
activityResultLauncher.launch(permissionArray)