Java android studio-未授予蓝牙发现访问\u粗略\u位置
我正在android studio上训练编码,我正在编写一个使用蓝牙的活动。我可以启用/禁用蓝牙。但我在尝试发现设备时遇到了麻烦。蓝牙找不到任何设备,似乎找不到的动作从未触发。。 代码没有错误,应用程序运行良好。 我发现没有授予访问位置的权限。这可能会导致错误吗 有人知道如何授予许可吗 我遵循了以下教程: 这是我的密码Java android studio-未授予蓝牙发现访问\u粗略\u位置,java,android,android-studio,android-bluetooth,Java,Android,Android Studio,Android Bluetooth,我正在android studio上训练编码,我正在编写一个使用蓝牙的活动。我可以启用/禁用蓝牙。但我在尝试发现设备时遇到了麻烦。蓝牙找不到任何设备,似乎找不到的动作从未触发。。 代码没有错误,应用程序运行良好。 我发现没有授予访问位置的权限。这可能会导致错误吗 有人知道如何授予许可吗 我遵循了以下教程: 这是我的密码 package com.example.kartouche; import android.bluetooth.BluetoothAdapter; import androi
package com.example.kartouche;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class BluetoothActivity extends AppCompatActivity {
/** Declaration des variables privés
* - p_bluetoothAdapter : représente le bluetooth de l'appareil
* - p_acces_btn_bluetooth : représente le bouton logique d'activation et désactivation du bluetooth de l'appareil
* -
* -- */
private BluetoothAdapter p_bluetoothAdapter;
private Button p_access_btn_bluetooth;
private Button p_btn_discover_device;
private ListView p_list_device;
int MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION = 1;
private static final int REQUEST_ENABLE_BT = 0;
private static final int REQUEST_DISCOVER_BT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth);
p_access_btn_bluetooth = (Button) findViewById(R.id.btn_access_bluetooth);
p_btn_discover_device = (Button) findViewById(R.id.btn_discover_device);
p_list_device = (ListView) findViewById(R.id.lv_list_devices);
/** ETAPE 1
* Implementation Bluetooth qui objective le bluetooth de l'appareil. -- */
p_bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (p_bluetoothAdapter == null)
{Toast.makeText(getApplicationContext(), "Bluetooth non disponible sur cette appareil",Toast.LENGTH_SHORT).show();}
if (getApplicationContext().checkSelfPermission("android.permission.ACCESS_COARSE_LOCATION") == PackageManager.PERMISSION_GRANTED)
{
Log.e("[MESSAGE]","ACCES_COARSE_LOCATION GRANTED");
}
else
{
Log.e("[MESSAGE]","ACCES_COARSE_LOCATION NOT GRANTED");
}
/** Textualisation du bouton p_acces_btn_bluetooth*/
if(!p_bluetoothAdapter.isEnabled())
{p_access_btn_bluetooth.setText("ACTIVER BLUETOOTH");}
else
{p_access_btn_bluetooth.setText("DESACTIVER BLUETOOTH");}
/** ETAPE 2
* Vérification de l'etat d'activation de l'appareil, Si le bluetooth n'est pas activé alors nous l'activons (resp) et changeons le texte du bouton -- */
p_access_btn_bluetooth.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Button btn_logique_text = (Button) p_access_btn_bluetooth;
String buttonText = btn_logique_text.getText().toString();
if (!p_bluetoothAdapter.isEnabled())
{
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
p_access_btn_bluetooth.setText("DESACTIVER BLUETOOTH");
}
else
{
p_bluetoothAdapter.disable();
p_access_btn_bluetooth.setText("ACTIVER BLUETOOTH");
}
}
});
/**
* ETAPE 3
* Analyse des appareils à proximité. -- */
p_btn_discover_device.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!p_bluetoothAdapter.isEnabled())
{Toast.makeText(getApplicationContext(), "Le bluetooth doit être activé pour effectuer l'analyse", Toast.LENGTH_SHORT).show();}
else
{
p_bluetoothAdapter.startDiscovery();
Toast.makeText(getApplicationContext(),"début d'analyse ...", Toast.LENGTH_SHORT).show();
}
}
});
// Register for broadcasts when a device is discovered.
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(receiver, filter);
}//OnCreate
// Create a BroadcastReceiver for ACTION_FOUND.
private final BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
List<String> p_device_bluetooth = new ArrayList<String>();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(BluetoothActivity.this,android.R.layout.simple_list_item_1,p_device_bluetooth );
p_list_device.setAdapter(arrayAdapter);
if (BluetoothDevice.ACTION_FOUND.equals(action))
{
Log.e("[MESSAGE]","Device enfin trouvé..");
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
p_device_bluetooth.add(deviceName);
arrayAdapter.notifyDataSetChanged();
}
}
};// BroadcastReceiver
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}//onDestroy
@Override
public void onBackPressed(){
startActivity(new Intent(getApplicationContext(), MainActivity.class));
finish();
}//OnBackPressed
}
package com.example.kartuche;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.bluetooth设备;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.content.IntentFilter;
导入android.content.pm.PackageManager;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.ListView;
导入android.widget.Toast;
导入androidx.appcompat.app.appcompat活动;
导入java.util.ArrayList;
导入java.util.List;
公共类BluetoothActivity扩展了AppCompatActivity{
/**私有变量声明
*-p_bluetooth适配器:蓝牙设备的代表
*-蓝牙接入:蓝牙设备的激活和激活报告
* -
* -- */
私人蓝牙适配器p_蓝牙适配器;
私人按钮p_访问\u btn\u蓝牙;
专用按钮p_btn_discover_设备;
专用ListView p_列表_设备;
int MY_PERMISSIONS_REQUEST_ACCESS_rough_LOCATION=1;
私有静态最终整数请求\u启用\u BT=0;
私有静态最终整数请求\u DISCOVER\u BT=1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u蓝牙);
p\u访问\u btn\u蓝牙=(按钮)findViewById(R.id.btn\u访问\u蓝牙);
p_btn_discover_device=(按钮)findviewbyd(R.id.btn_discover_device);
p_list_device=(ListView)findViewById(R.id.lv_list_devices);
/**第1版
*实现蓝牙设备的快速蓝牙目标。-*/
p_bluetoothAdapter=bluetoothAdapter.getDefaultAdapter();
if(p_bluetoothAdapter==null)
{Toast.makeText(getApplicationContext(),“蓝牙不可争议设备”,Toast.LENGTH_SHORT).show();}
if(getApplicationContext().checkSelfPermission(“android.permission.ACCESS\u粗略位置”)==PackageManager.permission\u已授予)
{
Log.e(“[消息]”,“已授予访问位置”);
}
其他的
{
Log.e(“[消息]”,“未授予访问位置”);
}
/**bouton p_接入蓝牙的文本化*/
如果(!p_bluetoothAdapter.isEnabled())
{p_access_btn_bluetooth.setText(“ACTIVER bluetooth”);}
其他的
{p_access_btn_bluetooth.setText(“解除激活蓝牙”);}
/**ETAPE 2
*设备激活的验证,是蓝牙技术的一个重要组成部分,它的作用是激活所有的设备(响应)并改变设备的文本--*/
p_access_btn_bluetooth.setOnClickListener(新视图.OnClickListener()){
@凌驾
公共void onClick(视图){
按钮btn\U logique\U text=(按钮)p\U访问\U btn\U蓝牙;
字符串buttonText=btn_logique_text.getText().toString();
如果(!p_bluetoothAdapter.isEnabled())
{
Intent enablebintent=新意图(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(启用BTIntent、请求\启用\ BT);
p_access_btn_bluetooth.setText(“解除激活蓝牙”);
}
其他的
{
p_bluetoothAdapter.disable();
p_access_btn_bluetooth.setText(“主动蓝牙”);
}
}
});
/**
*第三版
*分析设备的性能*/
p_btn_discover_device.setOnClickListener(新视图.OnClickListener()){
@凌驾
公共void onClick(视图){
如果(!p_bluetoothAdapter.isEnabled())
{Toast.makeText(getApplicationContext(),“Le bluetooth doitêtre activépour effectuer l'analysis”,Toast.LENGTH_SHORT.show();}
其他的
{
p_bluetoothAdapter.startDiscovery();
Toast.makeText(getApplicationContext(),“début d'analysis…”,Toast.LENGTH_SHORT.show();
}
}
});
//在发现设备时注册广播。
IntentFilter筛选器=新的IntentFilter(BluetoothDevice.ACTION\u已找到);
filter.addAction(BluetoothAdapter.ACTION\u DISCOVERY\u已启动);
filter.addAction(BluetoothAdapter.ACTION\u DISCOVERY\u FINISHED);
寄存器接收器(接收器、过滤器);
}//一次创建
//为找到的操作创建广播接收器。
专用最终BroadcastReceiver=新的BroadcastReceiver(){
公共void onReceive(上下文、意图){
String action=intent.getAction();
列出p_设备_bluetooth=新阵列列表();
ArrayAdapter ArrayAdapter=新的ArrayAdapter(BluetoothActivity.this,android.R.layout.simple\u list\u item\u 1,p\u device\u bluetooth);
p_list_device.setAdapter(arrayAdapter);
if(BluetoothDevice.ACTION_FOUND.equals(ACTION))
{
Log.e(“[MESSAGE]”,即“Device enfin trouvé.”);
BluetoothDevice=intent.getParcelableExtra(Bluet
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BluetoothActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@+id/btn_access_bluetooth"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="0dp" />
<Button
android:id="@+id/btn_access_bluetooth"
android:layout_width="345dp"
android:layout_height="40dp"
android:text=""
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_discover_device"
android:layout_width="345dp"
android:layout_height="40dp"
android:text="Analyse bluetooth a proximite"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.492"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.584" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kartouche">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
if (ContextCompat.checkSelfPermission(
CONTEXT, Manifest.permission.ACCESS_COARSE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
Log.e("[MESSAGE]","ACCES_COARSE_LOCATION GRANTED");
else {
// You can directly ask for the permission.
// The registered ActivityResultCallback gets the result of this request.
requestPermissionLauncher.launch(
Manifest.permission.ACCESS_COARSE_LOCATION);
Log.e("[MESSAGE]","ACCES_COARSE_LOCATION NOT GRANTED");
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission is granted. Continue the action or workflow
// in your app.
} else {
// Explain to the user that the feature is unavailable because
// the features requires a permission that the user has denied.
// At the same time, respect the user's decision. Don't link to
// system settings in an effort to convince the user to change
// their decision.
}
return;
}
}
}