I am trying to build a Bluetoothscanner.
But the startDiscovery() is not starting. I already include the permissions.
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
My Code is:
final BluetoothManager mBluetoothManager =
(BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = mBluetoothManager.getAdapter();
mBluetoothAdapter.disable();
onoff.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.enable();
onoff.setChecked(true);
}
else {
mBluetoothAdapter.disable();
onoff.setChecked(false);
}
if(!mBluetoothAdapter.isDiscovering()){
Log.e("Searching?: ", "is not searching");
mBluetoothAdapter.startDiscovery();
Log.d("SearchingStatus: ", "startDiscovery() started!" );
Log.e("Searching?", String.valueOf(mBluetoothAdapter.isDiscovering()));
}
}
});
My last log looks like this:
Searching?: false
BluetoothAdapter.enable is an asynchronous call: it will return immediately and clients should listen for ACTION_STATE_CHANGED to be notified of subsequent adapter state change, see more at https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#enable()
If you comment out all calls to BluetoothAdapter.enable / BluetoothAdapter.disable and make sure that Bluetooth is enabled (from Settings -> Bluetooth) before you start the app does it work then?
I would check the return value of mBluetoothAdapter.startDiscovery() and have a Log.e if it returns false.
Related
I have had push notifications configured in my Xamarin Forms app for the last year and this week decided to update to the new SDK, on iOS this was smooth sailing but on Android I am having problems.
I have tried to copy the documentation here: https://learn.microsoft.com/en-us/azure/notification-hubs/xamarin-notification-hubs-push-notifications-android-gcm and believe I am 90% of the way there as my app will happily receive push notifications when it is in the foreground and background, but my app cannot receive push notifications when it has been swiped closed (not force shut). I know this is 100% not a device issue as I tested the old configuration using the FirebaseService class and it works fine. I am also using 'Data' notifications.
I was able to find the following error message in the device log when a push notification is sent "Error (6621) / AndroidRuntime: java.lang.NullPointerException: Attempt to invoke interface method 'void com.microsoft.windowsazure.messaging.notificationhubs.NotificationListener.onPushNotificationReceived(android.content.Context, com.microsoft.windowsazure.messaging.notificationhubs.NotificationMessage)' on a null object reference"
Please see the code for my MainActivity class below.
[Activity(Label = "AndroidNotificationTest", Icon = "#mipmap/icon", Theme = "#style/MainTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
internal static readonly string CHANNEL_ID = "my_notification_channel";
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
// Listen for push notifications
NotificationHub.SetListener(new AzureListener());
// Start the SDK
NotificationHub.Start(this.Application, Constants.NotificationHubName, Constants.ListenConnectionString);
NotificationHub.AddTag("Developer");
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
CreateNotificationChannel();
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
void CreateNotificationChannel()
{
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
return;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "FCM Notifications", NotificationImportance.Default)
{
Description = "Firebase Cloud Messages appear in this channel"
};
NotificationManager notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
}
}
internal class AzureListener : Java.Lang.Object, INotificationListener
{
public void OnPushNotificationReceived(Context context, INotificationMessage message)
{
Console.WriteLine($"Message received with title {message.Title} and body {message.Body}");
}
}
And my Android Manifest File.
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.package.appname">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="29" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<application android:label="AndroidNotificationTest.Android" android:theme="#style/MainTheme"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
I have spent days on this but I am stumped at this stage on what the problem is.
Maybe my working code helps you.
I wrote this code before the new documentation with AzureListener class but it's still valid.
Xamarin forms: Push notification is not working on Android 7.1.2
//if i want more than one notification ,different unique value in every call
Random u = new Random ();
if (Build.VERSION.SdkInt >= BuildVersionCodes.O) {
string channelName = Resources.GetString (Resource.String.channel_name);
NotificationCompat.Builder notificationBuilder;
notificationBuilder = new NotificationCompat.Builder (this, channelName)
.SetContentTitle (title)
.SetSmallIcon (Resource.Drawable.ic_stat_g)
.SetContentText (messageBody)
.SetAutoCancel (true)
.SetContentIntent (pendingIntent);
var notificationManager = GetSystemService (Context.NotificationService) as NotificationManager;
NotificationChannel channel = new NotificationChannel (channelName, "notification channel", NotificationImportance.Default);
notificationManager.CreateNotificationChannel (channel);
notificationManager.Notify (u.Next(), notificationBuilder.Build ());
}
else
{
NotificationCompat.Builder notificationBuilder;
notificationBuilder = new NotificationCompat.Builder (this)
.SetContentTitle (title)
.SetSmallIcon (Resource.Drawable.ic_stat_g)
.SetContentText (messageBody)
.SetAutoCancel (true)
.SetContentIntent (pendingIntent);
var notificationManager = GetSystemService (Context.NotificationService) as NotificationManager;
notificationManager.Notify (u.Next(), notificationBuilder.Build ());
}
In my launch options, I'm launching a specific activity in this case it's called "DailyActivity.java".
In that class, I have an OnClicklistener for a button that should open up a camera intent to take a picture and then perform text extraction on it.
fab_camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DISPLAY_NAME, "myimage.jpg");
values.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg");
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES);
imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
Intent camintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
camintent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(camintent, 101); }});
And here is the onActivityResult that should run after startActivityForResult executes
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri image2 = data.getData();
InputImage image = null;
try {
image = InputImage.fromFilePath(this, image2);
TextRecognizer recognizer = TextRecognition.getClient();
When I click my button, my app crashes. I'm currently in the process of debugging and was wondering if it could be because I've launched from a specific activity initially? Would this affect future testing if I want the camera button to later redirect me to another activity(3rd one from the text extraction)?
This is the error I see in logcat when I click the button
2020-12-04 23:19:54.986 25037-25064/com.example.smartcalendar E/eglCodecCommon: GoldfishAddressSpaceHostMemoryAllocator: ioctl_ping failed for device_type=5, ret=-1
2020-12-04 23:19:59.633 25037-25037/com.example.smartcalendar E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.smartcalendar, PID: 25037
java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=25037, uid=10087 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()
at android.os.Parcel.createException(Parcel.java:1950)
at android.os.Parcel.readException(Parcel.java:1918)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476)
at android.content.ContentResolver.insert(ContentResolver.java:1587)
at com.example.smartcalendar.DailyActivity$2.onClick(DailyActivity.java:128)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Here are permissions from my Android Manifest file.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
On android 6 or above you have to check for permission in the runtime before opening the camera intent. Here's how you do it:
fab_camera.setOnClickListener{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.CAMERA)
== PackageManager.PERMISSION_DENIED ||
checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_DENIED
) {
// Permission was not granted
// Ask for runtime permission
val permissions = arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)
requestPermissions(
permissions,
PERMISSION_CODE
)
} else {
// Permission already granted
openCamera()
}
} else {
// System OS < Marshmallow
openCamera()
}
}
I've added <uses-permission android:name="android.permission.BLUETOOTH" /> to my manifest but the error
Missing permission required by BluetoothAdapter.isEnabled: android.permission.BLUETOOTH.
is still there.
Also, in ContextCompat.checkSelfPermission(...) what is the first parameter CONTEXT? The documentation https://developer.android.com/training/permissions/requesting does not say.
And am I correct that I need to disconnect and reconnect bluetooth whenever the app is not being used?
class MainActivity : AppCompatActivity() {
var bt: BluetoothAdapter? = null
var bts: BluetoothSocket? = null
val REQUEST_BLUETOOTH_PERMISSION: Int = 1
val REQUEST_BLUETOOTH_ENABLE: Int = 2
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if(!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE))
{
Toast.makeText(
getApplicationContext(),
"Device does not support Bluetooth therefore this application cannot run.",
Toast.LENGTH_SHORT
).show();
return;
}
bt = BluetoothAdapter.getDefaultAdapter()
if (bt == null) {
// This device does not have Bluetooth.
Toast.makeText(
getApplicationContext(),
"Device does not have a Bluetooth adapter therefore this application cannot run.",
Toast.LENGTH_SHORT
).show();
return;
}
bluetoothConnect();
}
fun bluetoothConnect() {
if (ContextCompat.checkSelfPermission(
CONTEXT, // What is this? It's not explained at https://developer.android.com/training/permissions/requesting
Manifest.permission.BLUETOOTH
) == PackageManager.PERMISSION_GRANTED
) {
if (bt.isEnabled == false) { // Error: Missing permission required by BluetoothAdapter.isEnabled: android.permission.BLUETOOTH.
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBtIntent, REQUEST_BLUETOOTH_ENABLE)
} else {
val pairedDevices: Set<BluetoothDevice>? = bt.bondedDevices
pairedDevices?.forEach { device ->
val deviceName = device.name
val deviceHardwareAddress = device.address // MAC address
}
}
}
else {
// Request permission. That will call back to onActivityResult which in the case of success will call this method again.
// Ask for permission.
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.BLUETOOTH),
REQUEST_BLUETOOTH_PERMISSION
)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_BLUETOOTH_PERMISSION) {
if (resultCode == RESULT_OK) {
bluetoothConnect();
} else {
Toast.makeText(
getApplicationContext(),
"This application cannot run because it does not have Bluetooth permission.",
Toast.LENGTH_SHORT
).show();
// Do we need to quit? How?
}
}
else if( requestCode == REQUEST_BLUETOOTH_ENABLE)
{
if(resultCode == RESULT_OK)
{
// try again
bluetoothConnect();
}
else {
Toast.makeText(
getApplicationContext(),
"This application cannot run because Bluetooth is not enabled and could not be enabled.",
Toast.LENGTH_SHORT
).show();
// Do we need to quit? How?
}
}
}
override fun onPause() {
super.onPause()
// Release Bluetooth
}
override fun onResume() {
super.onResume()
// Connect Bluetooth
}
override fun onStop() {
super.onStop()
// Release Bluetooth
}
override fun onStart() {
super.onStart()
// Connect Bluetooth
}
}
Edit:
added additional BT check to code,
adding manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rwb.btconnectortest">
<uses-permission android:name="android.permission.BLUETOOTH" />
<!--<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />-->
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/btconnectortestTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Just because comments are not good for this, let me give you a list of things you ought to do before you can act with Bluetooth. (apologies this is in Java because that's what I have right now, but very easy to translate to Kotlin if needed)
I'm doing this for BT LE (low energy) which is the preferred way for.. obvious reasons.
Did you add the permission(s) to the Manifest? You need something like
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
Make sure Bluetooth exists and is turned on...
// Does BLE exist?
if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)){
final BluetoothManager manager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
Now that you have a manager, you need to get the BluetoothAdapter:
BluetoothAdapter bluetoothAdapter = manager.getAdapter();
All this is fine in onCreate, but keep in mind that you have to check if BT is enabled every time the user resumes the activity (For it could have been turned off/disabled/revoked/etc).
Likely in onResume:
// obviously, you need to check that Bt adapter isn't null and all that,
// otherwise you ought to go back and "construct" it again, check permissions, etc.
adapter = getBTAdapter(); // do all the checks in there...
boolean bluetoothEnabled = adapter != null && adapter.isEnabled();
If the BT radio is off (user turning it off), you can programmatically enable it, if you have the corresponding permission (which I think is BT admin or similar, you're gonna have to search on that one, because it's been a while).
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> I believe it was.
Since BT is a radio that needs power, it will take a while (seconds) to turn on and be available. For this you need to "listen" with Yet Another broadcast receiver...
In other words, the activity will fire an intent (startActivityForResult(...)) telling Android to enable BT, you will subscribe to that broadcast to listen to the callback. Once android informs you that BT is on, you can go back to step 1 and start checking if it's enabled, you have permission, etc.
The callback is if I have not forgotten too much... looked like
public void onReceive(Context context, Intent intent) {
In there you ought to check for various BluetoothAdapter states... among them:
BluetoothAdapter.ACTION_STATE_CHANGED
This signals that the state changed, but another nested if is needed to determine to what state...
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE);
switch (state) {
case BluetoothAdapter.STATE_OFF:
case BluetoothAdapter.STATE_TURNING_OFF:
case BluetoothAdapter.STATE_TURNING_ON:
case BluetoothAdapter.STATE_ON:
}
Those are all the ones you care (check the BluetoothAdapter enum for more info).
In the ON you know BT is on... so..
Now you can tell the adapter that you want to scan...
adapter.startLeScan(callback);
(remember to call stopLeScan(callback) when you're done).
As each device is found, the callback will be called with the info you need to attempt to connect and pair (if needed).
The signature of the callback (LeScanCallback) is something like:
public void onScan(final BluetoothDevice device, int rssi, byte[] record);
(I'm typing by memory, so it may be a different name but you get the idea)
This is, as far as I can remember the old API.
API 21 has a ScanSettings.Builder() where you can specify how you want to scan, but it's essentially a similar method. Initiate scan, pass a callback and wait for results to show up.
You have various modes too:
SCAN_MODE_BALANCED: Balance battery efficiency and scan speed
SCAN_MODE_LOW_LATENCY: Prefer scan speed over battery
SCAN_MODE_LOW_POWER: Prefer battery efficiency over scan speed
SCAN_MODE_OPPORTUNISTIC: can't remember :) I think it was to use other scanner results 'around' you. Never used it.
Once you have identified the device you were looking for the BluetoothDevice has everything you need to tell BT to "connect" to it.
public void onScanResult(int callbackType, ScanResult scanResult) {
^ this is the signature of the "new" Scanner.
From that ScanResult, you can do:
int rssi = result.getRssi();
BluetoothDevice device = result.getDevice();
String advertiseName = device.getName();
String macAddress = device.getAddress();
If the scan fails for any reason, you get a callback on onScanFailed(int errorCode).
And again, there are various "reasons" (check the errorCode) why the scan failed.
Remember I may be mixing API 18 or API 21 "apis" here, but the concept is very similar in both.
Once you have finally grabbed a Device's MAC address... you can ask the adapter to try to connect to it:
BluetoothDevice device = adapter.getRemoteDevice(macAddress);
device.connectGatt(context, false, anotherCallback);
The callback is of BluetoothGattCallback and again, it has a bunch of methods among them onConnectionStateChange...
At this point you ought to read more about how Bluetooth works (and how it works on Android) because there are various modes (Gatt being one way) of operating with BT. It's impossible to know each and how/what you want to do once connected.
The rule of thumb will be: make sure you're prepared to having to re-pair or re-request permissions, because it's ultimately the user's choice to disable, turn off, walk-away, revoke permission, etc. at any point during this.
Good luck!
in manifest file add these two permission
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH"
#########################################################
*NOTE:i attached my bluetooth kotlin code , and its work with me. I enter code herehope this helpful
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private var myBluetooth:BluetoothAdapter? = null
lateinit var mypairedDevices:Set<BluetoothDevice>
val Request_Enable_Blutooth=1
companion object {
val EXTRA_ADDRESS :String= "Device_Address"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
myBluetooth= BluetoothAdapter.getDefaultAdapter()
if (myBluetooth == null)
{
Toast.makeText(applicationContext, "Bluetooth Device Not Available", Toast.LENGTH_LONG).show()
}
if (!myBluetooth!!.isEnabled)
{
val enableBlutoothIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBlutoothIntent, Request_Enable_Blutooth)
}
binding.BTNPairedDevices.setOnClickListener {
pairedDeviceList()
}
}
private fun pairedDeviceList (){
mypairedDevices = myBluetooth!!.bondedDevices
val list : ArrayList<BluetoothDevice> = ArrayList()
if (!mypairedDevices.isEmpty())
{
for ( device:BluetoothDevice in mypairedDevices)
list.add(device)
//list.add(device.name() + "\n" + device.address())
Log.i("Device", "This is messeage")
}
else {
Toast.makeText(applicationContext, " NO PAIRED DEVICES FOUND", Toast.LENGTH_LONG).show()
}
val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, list)
binding.DeviceListView.adapter = adapter
binding.DeviceListView.onItemClickListener = AdapterView.OnItemClickListener{ _, _, position, _ ->
val device: BluetoothDevice = list[position]
val address: String = device.address
val intent = Intent(this, LedController::class.java)
intent.putExtra(EXTRA_ADDRESS, address)
startActivity(intent)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == Request_Enable_Blutooth)
{
if(resultCode ==Activity.RESULT_OK)
{
if (myBluetooth!!.isEnabled)
{ Toast.makeText(applicationContext, "Bluetooth Enabled", Toast.LENGTH_LONG).show()
}
else ( Toast.makeText(applicationContext, "Bluetooth Disabled", Toast.LENGTH_LONG).show()
)
}
} else if(resultCode == Activity.RESULT_CANCELED)
Toast.makeText(applicationContext, "Bluetooth has been canceled", Toast.LENGTH_LONG).show()
}
}
I restarted AndroidStudio and now the error has disappeared. What a complete piece of rubbish.
But now the layout is broken...
Scan Bluetooth Devices not working on Android Things DP3. Any idea/workaround? Not getting action BluetoothDevice.ACTION_FOUND.
I've added ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permisssion due changes in Android 6.0 but still not working.
https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html
Also android.permission.BLUETOOTH_ADMIN, android.permission.BLUETOOTH
I have checked, ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION over Android >=6.0 needs runtime permission but it give an error due Android Things limitation.
private void checkPermission() {
List<String> permissionsList = new ArrayList<String>();
List<String> permissionsNeeded = new ArrayList<String>();
// app
permissionsList.add(Manifest.permission.ACCESS_FINE_LOCATION);
permissionsList.add(Manifest.permission.ACCESS_COARSE_LOCATION);
permissionsList.add(Manifest.permission.ACCESS_WIFI_STATE);
permissionsList.add(Manifest.permission.SET_TIME_ZONE);
// AndroidThingsLib
permissionsList.add(Manifest.permission.BLUETOOTH_ADMIN);
permissionsList.add(Manifest.permission.BLUETOOTH);
permissionsList.add(Manifest.permission.CHANGE_WIFI_STATE);
permissionsList.add(Manifest.permission.INTERNET);
permissionsList.add(Manifest.permission.ACCESS_NETWORK_STATE);
for (int i = 0; i < permissionsList.size(); i++){
if (checkSelfPermission(permissionsList.get(i)) == PackageManager.PERMISSION_GRANTED) {
permissionsNeeded.add(permissionsList.get(i));
updateDataLogs(SourceEnum.System, "GRANTED: " + permissionsList.get(i));
} else {
permissionsNeeded.add(permissionsList.get(i));
updateDataLogs(SourceEnum.Error, "NOT GRANTED: " + permissionsList.get(i));
}
}
if (permissionsList.size()>0){
requestPermissions(permissionsNeeded.toArray(new String[permissionsNeeded.size()]),5678);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == 5678){
for (int i = 0; i < permissions.length; i++){
updateDataLogs(SourceEnum.Error, permissions[i] + " result " + grantResults[i]);
}
}
}
private void enableBluetoothScan() {
updateDataLogs(SourceEnum.System, "enableBluetoothScan");
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter.isEnabled()){
bluetoothAdapter.startDiscovery();
} else {
updateDataLogs(SourceEnum.Error, "Bluetooth Adapter not enabled");
}
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
updateDataLogs(SourceEnum.System, "DISCOVERY STARTED");
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
updateDataLogs(SourceEnum.System, "DISCOVERY FINISHED");
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
updateDataLogs(SourceEnum.System, "NEW DEVICE " + device.getName());
}
}
}
};
As noted in the Platform Overview, runtime permissions granting is not supported on Android Things because the device may not have an interactive display to show the dialog. Instead, Android Things falls back on the install-time permissions model for all permissions requested in the manifest. This means calls to methods like requestPermissions() aren't necessary and won't help you.
Regarding issues encountered during the current preview, the following excerpt is from the Release Notes:
Dangerous permissions requested by apps are not granted until the next device reboot. This includes new app installs and new elements in existing apps.
For the time being, when your app requests dangerous permissions (like location permissions), you need to reboot the device after install (one-time) to have those permissions granted during boot.
Thanks to your explanation what you did already, I was able to conquer this issue. Because you did not mark the question as solved yet:
build.gradle (app; thanks to 1 and 2):
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
<!-- update this to your version of the Android SDK Tools -->
buildToolsVersion "25.0.3"
defaultConfig {
minSdkVersion 24
}
}
dependencies {
<!-- update this to your version of Android Things -->
provided 'com.google.android.things:androidthings:0.4-devpreview'
}
AndroidManifest.xml (thanks to 1 and 2):
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package=" . . . ">
<!-- required for getState in startDiscovery -->
<uses-permission android:name="android.permission.BLUETOOTH"/>
<!-- required for startDiscovery -->
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<!-- required for the IntentFilter ACTION_FOUND -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<!-- change to your label -->
<application android:label="Intel Edison">
<uses-library android:name="com.google.android.things"/>
<!-- change to the name of your Main Activity -->
<activity android:name=".HomeActivity">
<!-- Launch activity as default from Android Studio -->
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<!-- Launch activity automatically on boot -->
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.IOT_LAUNCHER"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>
HomeActivity.java:
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothDevice.ACTION_NAME_CHANGED);
registerReceiver(mReceiverBT, filter);
BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();
boolean started = mBtAdapter.startDiscovery();
if (false == started) {
<!-- in that case, catch BluetoothAdapter.STATE_ON in the receiver and start the Bluetooth Discovery then -->
mBtAdapter.enable();
}
With this code, my app started right away after booting the Intel Edison board and was able to discover nearby Bluetooth devices. Notes:
On this board, Android Things requires 69 seconds (Developer Preview 4) or 82 seconds (Developer Preview 3) to start my app.
In logcat, I saw "Not granting permission android.permission.ACCESS_COARSE_LOCATION to package … because it was previously installed without". I found no way around that; reported in Issue 62514750.
Actually, you might not need any ‘dangerous’ permission at all – for example, I do nothing on ACTION_FOUND – because ACTION_NANE_CHANGED is fired always here in my scenarios; reported in Issue 62524160.
I'm trying to create a single purpose app.
So I have create an BaseActivity that all my activities inherit from it.
it's look like
public class LockDeviceActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private void startLock() {
if(mDevicePolicyManager.isLockTaskPermitted(getPackageName())) {
/**
* If lock task is permitted, we can lock the task. We can use an external DPM like
* TestDPC provided by Google to manage lock task list.
*
* If the lock is obtained using TestDPC, features like status bar, home button, recent
* apps, etc is disabled.
*
* To unlock we can programatically call stopLockTask() when users taps a button. But
* in practice this should be done using a separate admin console or Confirm Credential.
*
* For API 23+ you can check if the lock is active by checking if
* activityManager.getLockTaskModeState() == ActivityManager.LOCK_TASK_MODE_NONE
*/
Log.d(TAG, "startLock() called");
this.startLockTask();
} else {
/**
* The device is not whitelisted.
*/
Toast.makeText(this, "The app is not whitelisted for lock", Toast.LENGTH_SHORT).show();
// Timber.d("The app is not whitelisted for lock task");
/**
* We can still pin the app but it will not be locked.
*
* We can simply unlock by pressing recent and back button together.
*
* Unlocking by calling stopLockTask() on button click can be achieved as well.
*/
// Timber.d("just pinning the app");
this.startLockTask();
}
}
so when I first Enter the app, I can see some Pinned message and it's okay by me.
The problem is, when I'm doing an Intent from one activity to other Activity which contain Fragment, I'm getting the following image :
plus i'm getting a some system toast message :
" the app is not whitelisted for lock "
how can I avoid this kind of behavior ?
thank you all .
1) Reset device to factory default
2) Skip Account registration
3) Install the app then close it
4) Run adb shell dpm set-device-owner <app-package-name>/.<AppAdminReceiver>
5) Make sure you get a successful result.
6) Re-launch the app.
The following must be satisfied:
AndroidManifest.xml inside application tag
<activity
android:name=".AppActivity"
android:label="Locked Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<receiver
android:name=".AppAdminReceiver"
android:description="#string/app_name"
android:label="#string/app_name"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="#xml/device_admin_receiver" />
<intent-filter>
<action android:name="android.intent.action.DEVICE_ADMIN_ENABLED"/>
<action android:name="android.intent.action.PROFILE_PROVISIONING_COMPLETE"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Create a new Java Class named AppAdminReceiver.java (it's fine to leave it blank)
public class AppAdminReceiver extends android.app.admin.DeviceAdminReceiver {
}
Add device_admin_receiver.xml in /res/xml directory
<?xml version="1.0" encoding="utf-8"?>
<device-admin>
<uses-policies>
<disable-keyguard-features/>
</uses-policies>
</device-admin>
In AppActivity.java calling startLockTask() and stopLockTask()
private ComponentName mAdminComponentName;
private DevicePolicyManager mDevicePolicyManager;
...
... onCreate(){
// Retrieve Device Policy Manager so that we can check whether we can
// lock to screen later
mAdminComponentName = new ComponentName(this,AppAdminReceiver.class);
mDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
if(mDevicePolicyManager.isDeviceOwnerApp(getPackageName())){
// App is whitelisted
setDefaultCosuPolicies(true);
}
else {
// did you provision the app using <adb shell dpm set-device-owner ...> ?
}
}
... onStart(){
// Consider locking your app here or by some other mechanism
// Active Manager is supported on Android M
if(mDevicePolicyManager.isLockTaskPermitted(this.getPackageName())){
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
if (am.getLockTaskModeState() == ActivityManager.LOCK_TASK_MODE_NONE) {
setDefaultCosuPolicies(true);
startLockTask();
}
}
}
... unlockApp(){
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
if (am.getLockTaskModeState() == ActivityManager.LOCK_TASK_MODE_LOCKED) {
stopLockTask();
}
setDefaultCosuPolicies(false);
}
private void setDefaultCosuPolicies(boolean active){
// Set user restrictions
setUserRestriction(UserManager.DISALLOW_SAFE_BOOT, active);
setUserRestriction(UserManager.DISALLOW_FACTORY_RESET, active);
setUserRestriction(UserManager.DISALLOW_ADD_USER, active);
setUserRestriction(UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA, active);
setUserRestriction(UserManager.DISALLOW_ADJUST_VOLUME, active);
// Disable keyguard and status bar
mDevicePolicyManager.setKeyguardDisabled(mAdminComponentName, active);
mDevicePolicyManager.setStatusBarDisabled(mAdminComponentName, active);
// Enable STAY_ON_WHILE_PLUGGED_IN
enableStayOnWhilePluggedIn(active);
// Set system update policy
if (active){
mDevicePolicyManager.setSystemUpdatePolicy(mAdminComponentName,SystemUpdatePolicy.createWindowedInstallPolicy(60, 120));
} else {
mDevicePolicyManager.setSystemUpdatePolicy(mAdminComponentName,null);
}
// set this Activity as a lock task package
mDevicePolicyManager.setLockTaskPackages(mAdminComponentName,active ? new String[]{getPackageName()} : new String[]{});
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MAIN);
intentFilter.addCategory(Intent.CATEGORY_HOME);
intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
if (active) {
// set Cosu activity as home intent receiver so that it is started
// on reboot
mDevicePolicyManager.addPersistentPreferredActivity(mAdminComponentName, intentFilter, new ComponentName(getPackageName(), AppActivity.class.getName()));
} else {
mDevicePolicyManager.clearPackagePersistentPreferredActivities(mAdminComponentName, getPackageName());
}
}
private void setUserRestriction(String restriction, boolean disallow){
if (disallow) {
mDevicePolicyManager.addUserRestriction(mAdminComponentName,restriction);
} else {
mDevicePolicyManager.clearUserRestriction(mAdminComponentName,restriction);
}
}
private void enableStayOnWhilePluggedIn(boolean enabled){
if (enabled) {
mDevicePolicyManager.setGlobalSetting(mAdminComponentName,Settings.Global.STAY_ON_WHILE_PLUGGED_IN,Integer.toString(BatteryManager.BATTERY_PLUGGED_AC| BatteryManager.BATTERY_PLUGGED_USB| BatteryManager.BATTERY_PLUGGED_WIRELESS));
} else {
mDevicePolicyManager.setGlobalSetting(mAdminComponentName,Settings.Global.STAY_ON_WHILE_PLUGGED_IN,"0");
}
}
}
Similar tutorial on setting up kiosk mode
android:lockTaskMode="if_whitelisted"
add this line on your activity in AndroidManifest file
<activity android:name=".MainActivity"
android:exported="true"
android:lockTaskMode="if_whitelisted">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>