PC SOFT

FORUMS PROFESSIONNELS
WINDEVWEBDEV et WINDEV Mobile

Accueil → WINDEV Mobile 2024 → bluetooth permissions bluetooth_scan bluetooth_connect android 12
bluetooth permissions bluetooth_scan bluetooth_connect android 12
Débuté par Jonathan, 29 nov. 2022 00:20 - 3 réponses
Posté le 29 novembre 2022 - 00:20
New permissions for android 12 and on
Java code for asking those permissions

import android.content.pm.PackageManager;
import androidx.annotation.NonNull;;
import androidx.core.app.ActivityCompat;;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Build;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;

public static int BTPermissions()
{

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
int REQUEST_CODE = 1;
Activity currentActivity = getCurrentActivity();

if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(currentActivity,new String[] {android.Manifest.permission.BLUETOOTH_CONNECT,android.Manifest.permission.BLUETOOTH_SCAN,android.Manifest.permission.BLUETOOTH_ADVERTISE}, REQUEST_CODE);
return 0;
}
}
return 1;

}
Posté le 29 novembre 2022 - 19:08
Quicker way to get paired devices, especially android 12 with new permissions bluetoo

import android.content.pm.PackageManager;
import androidx.annotation.NonNull;;
import androidx.core.app.ActivityCompat;;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Build;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;

public static String BTgetPairedDevices()
{
String BTList = "";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
int REQUEST_CODE = 1;
Activity currentActivity = getCurrentActivity();

if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(currentActivity,new String[] {android.Manifest.permission.BLUETOOTH_CONNECT,android.Manifest.permission.BLUETOOTH_SCAN,android.Manifest.permission.BLUETOOTH_ADVERTISE}, REQUEST_CODE);
}
if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
return "";
}
}

BluetoothAdapter mBluetoothAdapter;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set pairedDevices = mBluetoothAdapter.getBondedDevices();
// mBluetoothAdapter.startDiscovery();

if (pairedDevices.size() > 0) {
// There are paired devices. Get the name and address of each paired device.
for (BluetoothDevice device : pairedDevices) {
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
if (BTList != "") {
BTList = BTList + "\r\n";
}
BTList = BTList + "1\t" + deviceHardwareAddress + "\t" + deviceName;
}
}

return BTList;

}
Membre enregistré
8 messages
Posté le 13 janvier 2023 - 19:47
Hello. Great tool missing from Windev. I don't know Java and I cannot add it in. I made global procedure. Turned into Java. Now I need to find the external dependencies and I have a really hard time searching and searching which jar file I need to add. Can you tell us a bit more of instructions? Where do we locate all the jar or aar files to make this code work?
Membre enregistré
8 messages
Posté le 05 mars 2023 - 14:58
Auto translation to French
Après avoir lutté, débogué et lu, j'ai trouvé une solution qui a fonctionné pour moi. Pour toutes les versions Android, 10, 11, 12, 13
Android 11 n'a besoin de rien de différent concernant la dent bleue. Android 12 et versions ultérieures doivent analyser et se connecter au moment de l'exécution. Mais le système prend un certain temps jusqu'à ce que la boîte de dialogue s'affiche. Et dans Android 12, l'appareil doit être couplé une fois l'autorisation accordée. J'ai donc ajouté une boîte de dialogue si la connexion ne parvient pas à inviter l'utilisateur à réparer l'appareil. J'ai créé cette routine GrantPermissions en utilisant plusieurs if pour chaque version d'API différente. Concernant le stockage public, j'ai arrêté de l'utiliser et stocké les fichiers dans le stockage interne. Je le partage ici pour aider ceux qui pourraient en avoir besoin.

English
After struggling and debugging and reading I 've come up to a solution that worked for me. For all android versions, 10, 11, 12, 13
Android 11 needs nothing different concerning blue tooth. Android 12 and above need scan and connect in runtime. But the system takes some time until the dialog is presented. And in Android 12 the device needs to be paired after the permission is granted. So I added a dialog if connection fails to prompt the user repair the device. I created this routine GrantPermissions using multiple if for every different API version. Concerning the public storage I quit from using it and store the files in internal storage. I share it here to help anyone else who might need it.

//All variables are global, I check if permissions are allready granted at initialization so when running 2nd time this does nothing
//The code is not very beautiful but it works for me
Procedure GrantPermissions()

ProgressBar1=0

IF ApiVersion<=30 THEN RETURN True //Android 10 needs nothing
IF ApiVersion=30 THEN
//android 11 needs nothing. Files are in internal storage from gspath creation
ELSE IF ApiVersion>=31 THEN //Android 12 and 13 need both scan and connect.
// ChangePlane(PlanePermissions) //This is for my application. I noticed that from splash screen my app was minimized. I made a plane to show a progress bar and a text
WHILE Grantedconnect=False OR Grantedscan = False
IF Grantedscan=False THEN
PermissionRequest(Permission_BT_Scan,ProcRequestScan)
END
IF Grantedconnect=False THEN
PermissionRequest(Permission_BT_Connect,ProcRequestConnect)
END
ProgressBar1=ProgressBar1+4
Multitask(50)
END
ChangePlane(MainPlane)
RETURN Grantedconnect AND Grantedscan
END

INTERNAL Procedure ProcRequestScan(Perm1 is Permission)
IF Perm1.Granted THEN
Grantedscan=True
END
END


INTERNAL Procedure ProcRequestConnect(Perm is Permission)
IF Perm.Granted THEN
HourGlass(True)
sListDevices=""
sListDevices = BTListDevice(2000)
HourGlass(False)
Grantedconnect=True
END
END