How to access external usb camera on unrooted android devices - android

I want to access extern usb cameras via v4l on android.
I tried SimpleWebCam. After some slight modifications of the original source codes, i achieved to make it work on a rooted android device. However, on unrooted devices, it keeps complaining about "not have permission to access "/dev/video*". I checked the permission of /dev/video* with "ls -l /dev/video*", and got
crw-rw---- system camera 81, 0 2015-08-18 18:31 video0
I understand that it means /dev/video* are owned by system, and are readable/writable to users in group "camera". So I think if i add
<uses-permission android:name="android.permission.CAMERA" />
in the manifest of my app, the user id of my app will be added to the group "camera", then my app will be allowed to read data from /dev/video*.
But, it still complains about "not have permission to access /dev/video*" now.
i also tried
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
, but still not working.
Do i miss somthing or misunderstand somthing. Any help or discussion will be appreciated.
The codes i used to open device are
int opendevice(int i)
{
struct stat st;
sprintf(dev_name,"/dev/video%d",i);
if (-1 == stat (dev_name, &st)) {
LOGE("Cannot identify '%s': %d, %s", dev_name, errno, strerror (errno));
return ERROR_LOCAL;
}
if (!S_ISCHR (st.st_mode)) {
LOGE("%s is no device", dev_name);
return ERROR_LOCAL;
}
fd = open (dev_name, O_RDWR);// | O_NONBLOCK, 0);
if (-1 == fd) {
LOGE("Cannot open '%s': %d, %s", dev_name, errno, strerror (errno));
return ERROR_LOCAL;
}
return SUCCESS_LOCAL;
}
The return value of open is always -1, with logcat:
Cannot open '/dev/video3': 13, Permission denied

I finally achieve to read images from the external usb camera on unrooted android devices using an opensource project named uvccamera.
Here is the link, https://github.com/saki4510t/UVCCamera

Try to add also
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
in your manifest file:
http://developer.android.com/reference/android/hardware/Camera.html

Related

Not seeing always allow in android location permission

I have been trying to use an react native ble librairie (react-native-ble-manager). In order to use this the user have to allow location permission (as explain in the documentation android.permission.ACCESS_COARSE_LOCATION and android.permission.ACCESS_FINE_LOCATION if android API >= 29.
I request the autorisation as their example with:
(PermissionsAndroid is from the react-native librairie) :
if (Platform.OS === 'android' && Platform.Version >= 23) {
PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION).then((result) => {
if (result) {
console.log("Permission is OK");
} else {
PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION).then((result) => {
if (result) {
console.log("User accept");
} else {
console.log("User refuse");
}
});
}
});
}
On my device the pop-up ask me for the autorisation with only two choices :
Only when the application is running
Refuse
But with their example I have 3 choices the two others and "always authorise"
And for some reason I'm not able to scan peripheral if I don't always approve (I have been able to change to always for my application by going in the settings of it).
Part of my android manifest (android/app/src/main) (as you can see I have background_location)
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
I'm sure I'm missing an obvious thing but I can't see what..
In advance thank you :)
For those having the same issue : my phone has android 11 so it can't be directly requested in the pop-up.
You have to indicate to your user how to change the permission
Source : https://developer.android.com/training/location/permissions

Checking for permissions always returns "denied". React Native

During the debugging process of React Native application on a physical device (Android) when I check for the location permission it's always blocked, even though I granted the permission in the settings. I have to note that I haven't been able to request the "ask for permission" window previously, so I couldn't block it in any way. Also, I tried to delete and let the app to be installed again.
Here's the code where I check for location permission (I tried others too). There I use react-native-permissions however, the behaviour is the same if I use PermissionsAndroid from react-native.
import {check, PERMISSIONS, request, RESULTS, openSettings} from "react-native-permissions";
async function checkPermissions() {
let response = await check(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION); // <-- always blocked
let isPermissionsGranted = false;
if (response === RESULTS.GRANTED) {
isPermissionsGranted = true;
} else if (response === RESULTS.DENIED) {
response = request(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION, {
title: "FreeLine requires permission",
message: "FreeLine needs access to your location so you can see your position",
buttonPositive: "Ok",
buttonNegative: "Don't show my position",
});
if (response === RESULTS.GRANTED) {
isPermissionsGranted = true;
} else if (response === RESULTS.DENIED) {
await openSettings();
}
}
return isPermissionsGranted;
}
I haven't found any information that'd explain that. I thought that it's possible that during debugging I can't request for permission.
In the end, I found the root of the problem. The funniest thing it isn't connected with my code in any way. The problem was caused by the manifest, to be more precise by rules I included.
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-feature android:name="android.permission.BLUETOOTH" android:required="true"/>
<!-- Only foreground ble access -->
<uses-feature android:name="android.permission.ACCESS_FINE_LOCATION" android:required="true"/>
As you can see from the snippet above, I used use-feature. Here what the docs say about it:
Google Play uses the elements declared in your app manifest to filter your app from devices that do not meet its hardware and software feature requirements.
In addition to the rules above, I have to add uses-permission, like that:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

How can I get GPS Location of my device in Xamarin Forms?

I want to get the Longitude and Latitude of my device when the entry box named entLocation is focused. I am using Xamarin.Essential Geolocation to get the GPS location of my device I followed the documentation and tutorials and still I am unable to get the GPS Location. I have added permission below to my android manifest still nothing. I am getting an display alert on "Error3", Error3 is the exception when Unable to get location.
The error is "Java.Lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Activity.requestPermissions(java.lang.String[],int)' on a null object reference"
My questions:
1. What is my error in my code?
2. Can I get my gps location offline? If yes, how can I achieve that?
uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
uses-feature android:name="android.hardware.location" android:required="false"
uses-feature android:name="android.hardware.location.gps" android:required="false"
uses-feature android:name="android.hardware.location.network" android:required="false"
private async void entLocation_FocusedAsync(object sender, FocusEventArgs e)
{
try
{
var request = new GeolocationRequest(GeolocationAccuracy.Medium);
var location = await Geolocation.GetLocationAsync(request);
if (location != null)
{
entLocation.Text = location.Longitude.ToString() + "," + location.Latitude.ToString();
}
}
catch (FeatureNotSupportedException fnsEx)
{
await DisplayAlert("Error1", fnsEx.ToString(), "ok");
}
catch (PermissionException pEx)
{
await DisplayAlert("Error2", pEx.ToString(), "ok");
}
catch (Exception ex)
{
await DisplayAlert("Error3", ex.ToString(), "ok");
}
}
I am also working on my first Xamarin project that requires location permissions. A couple of things you should try that may help you get GPS location.
My suggestions
1) You need to grant additional permissions in AndroidManifest file. These are
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
2) Since Android 6.0 or above, you need to have runtime permissions. To acchieve that, you need to override OnStart() method in your MainActivity.cs. I referenced to Location permission for Android above 6.0 with Xamarin.Forms.Maps. Here is what I did
protected override void OnStart()
{
base.OnStart();
if(CheckSelfPermission(Manifest.Permission.AccessCoarseLocation) != (int)Permission.Granted)
{
RequestPermissions(new string[] { Manifest.Permission.AccessCoarseLocation, Manifest.Permission.AccessFineLocation }, 0);
}
}
Important: My solution here is just temporarily handle granting permissions. Visual Studio would show an exception when you build and run the project first time. After you choose "allow" from "Allow access location?" prompt on the device, you would not get the exception when run the project again. I have not handled this issue yet.
My limits
As I said, the problem isn't completely solved. My answer is to help you temporarily solve location permissions issue and move on. And I don't know how to get location offline.

Android - Camera permissions denied without prompting

I was running into issues with the manual process behind requesting permissions (just kept falling into the 'denied' code), so I switched over to using Dexter to simplify. I implemented the following code in onCreate(), and I did a fresh install of the app:
Dexter.withActivity(this)
.withPermission(Manifest.permission.CAMERA)
.withListener(new PermissionListener() {
#Override public void onPermissionGranted(PermissionGrantedResponse response) {
Log.d(TAG, "GRANTED!");
initCamera();
}
#Override public void onPermissionDenied(PermissionDeniedResponse response) {
Log.d(TAG, "DENIED!");
}
#Override public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
Log.d(TAG, "PERMISSION RATIONAL SHOULD BE SHOWN!");
}
}).check();
It immediately falls into the "DENIED!" log, and it never even prompts me. I tried this particular code to attempt multiple permissions (which is ultimately what I need to do):
Dexter.withActivity(activity)
.withPermissions(Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
.withListener(new MultiplePermissionsListener() {
#Override
public void onPermissionsChecked(MultiplePermissionsReport report) {
Log.d(TAG, "Accepted: " + report.getGrantedPermissionResponses().size() + " | Denied: " + report.getDeniedPermissionResponses().get(0).getPermissionName());
}
#Override
public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
Log.d(TAG, "continuing permissions request..");
token.continuePermissionRequest();
}
})
.check();
It prompts for permissions to Record Audio, then it asks about Access to photos/media/files on the device (it never asks about Camera). Then once that's done, it prints the log: "Accepted 3 | Denied: android.permission.CAMERA". It denies it without even prompting me again.
My Manifest is set properly to have CAMERA in the proper place (outside of the 'application' tag). See below for reference:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.app">
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<permission
android:name="${applicationId}.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
etc..
Odd thing is that when I go into Settings > Applications > MyApp, the Camera option is not even displayed in there.
I don't think it's an issue with Dexter, since it's doing basically the same thing when I set it up manually (and I confirmed that it's definitely setup properly in that case after looking at a few top S.O. posts).
Any thoughts on what the issue could be here? FYI - I'm using a Galaxy S6, OS 6.0.2. The other users experiencing this seem to be other devices with 6.0+ OS. Thanks in advance!
EDIT:
Testing various devices, it works on some and does not work on some:
Moto X (OS 5.0) - Broken
Nexus 5 (OS 7.0) - Works
Samsung S6 (OS 6.0.1) - Broken
Broken Moto X (OS 6.0) - Works
Doesn't seem to be a solid pattern.. Definitely strange. I also started a brand new project and ran the same code - worked fine and allowed access to my camera. So it doesn't appear to be fully device-specific..
The issue with this turned out to be a third-party library, which had this line in their Manifest, overriding our own permission:
<uses-permission android:name="android.permission.CAMERA" tools:node="remove" />
The solution was either to manually import their project as a module (rather than use gradle), and then comment out that line, OR more simple - you can add "tools:node="replace"" to the end of the main project's CAMERA permission line, and it works fine after that; no need to import the project with the latter approach.
What you need is native runtime permissions not dexter, Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. This approach streamlines the app install process, since the user does not need to grant permissions when they install or update the app. It also gives the user more control over the app's functionality; for example, a user could choose to give a camera app access to the camera but not to the device location. The user can revoke the permissions at any time, by going to the app's Settings screen.
// Assume thisActivity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.YOUR_PERMISSION);
then what you need is to request a certain permission if that check is false,
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.YOUR_PERMISSION},
MY_PERMISSION_CODE);
Bare in ind that you need to as well declare them also in the manifest, based on what you have shown still that was already done. For more information.

WebRTC error inside Chromium WebView: "CheckMediaAccessPermission: Not supported"

I'm trying to show WebRTC chat in WebView.
According to this documentation, WebView v36 supports WebRTC. For my test I'm using a device with Chrome/39.0.0.0 and I have added permissions to the AndroidManifest.xml file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<user-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
but when I enter into the chat, I see a Chromium error log (device doesn't show \ translate anything, only 'loading' progress bar):
W/AudioManagerAndroid: Requires MODIFY_AUDIO_SETTINGS and RECORD_AUDIO
W/AudioManagerAndroid: No audio device will be available for recording
E/chromium: [ERROR:web_contents_delegate.cc(178)] WebContentsDelegate::CheckMediaAccessPermission: Not supported.
E/chromium: [ERROR:web_contents_delegate.cc(178)] WebContentsDelegate::CheckMediaAccessPermission: Not supported.
W/AudioManagerAndroid: Requires MODIFY_AUDIO_SETTINGS and RECORD_AUDIO
W/AudioManagerAndroid: No audio device will be available for recording
D/ChromiumCameraInfo: Camera enumerated: front
Tested on a real device, Android 5.1.1.
additional request for permissions is needed
webView.setWebChromeClient(new WebChromeClient(){
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
public void onPermissionRequest(final PermissionRequest request) {
request.grant(request.getResources());
}
});
update but it not working for audio capture
UPDATE found working google-sample code here
You need these permissions to access Camera and Microphone
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" android:required="true"/>
<uses-feature android:name="android.hardware.camera.autofocus"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
// don't miss this one
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
Next you need to grant permissions to your webview, check this link for more details:
webView.setWebChromeClient(new WebChromeClient(){
#Override
public void onPermissionRequest(PermissionRequest request) {
runOnUiThread(() -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
String[] PERMISSIONS = {
PermissionRequest.RESOURCE_AUDIO_CAPTURE,
PermissionRequest.RESOURCE_VIDEO_CAPTURE
};
request.grant(PERMISSIONS);
}
});
}
});
If audio playback is not working, use this:
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
My experience with this in 2022:
CAMERA and RECORD_AUDIO permissions need to be declared in Manifest
setWebChromeClient.onPermissionRequest should check if those permissions have already been granted. If not, use registerForActivityResult(new RequestMultiplePermissions()) to ask the user to grant them.
its mostly error in webview reload becuase when we will request for audio , camera permission on webview , after accept permission , we need to refresh the webpage.
if (permission.equals("android.webkit.resource.AUDIO_CAPTURE")) {
demandForPermission(request.getOrigin().toString(), Manifest.permission.RECORD_AUDIO, MY_PERMISSIONS_REQUEST_RECORD_AUDIO);
} else {
myRequest.grant(request.getResources());
}
I also stuck this problem for many days but after in below link code , 100% working code Android Webview

Categories

Resources