Android蓝牙功能权限问题
我试图建立蓝牙连接Android应用。有一个按钮单击“蓝牙搜索BTN”事件处理程序。我想实现蓝牙搜索功能。 但是,许可有一个问题,并向我展示了代码下的红线。
问题发生了:
1。 bluetoothsearchbtn.setonClickListener(new View.OnClickListener()){}
2。私有最终broadcastreceiver reveriver = new broadcastreceiver(){}
- 变量声明
Set<BluetoothDevice> pairedDevices;
ArrayAdapter<String> btArrayAdapter;
ArrayList<String> deviceAddressArray;
BluetoothAdapter btAdapter;
btAdapter = BluetoothAdapter.getDefaultAdapter();
btArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
deviceAddressArray = new ArrayList<>();
这是我的代码,显示了红线。
- bluetoothsearchbtn.setOnClickListener(new View.OnClickListener()
BluetoothSearchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Check if the device is already discovering
if(btAdapter.isDiscovering()){
btAdapter.cancelDiscovery();
} else {
if (btAdapter.isEnabled()) {
btAdapter.startDiscovery();
btArrayAdapter.clear();
if (deviceAddressArray != null && !deviceAddressArray.isEmpty()) {
deviceAddressArray.clear();
}
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, filter);
} else {
Toast.makeText(getApplicationContext(), "bluetooth not on", Toast.LENGTH_SHORT).show();
}
}
}
});
红色下划线位置:btadpater.isdiscovering()
,btadapter.canceldster.canceldiscovering(cancanceldiscovering()
btadatper.startdiscovering()
问题: 呼叫需要用户可能拒绝的权限:代码应明确检查以查看是否可用(使用'Checkperssmerion)或明确处理潜在的“ SecurityException” 。。
- 私有最终broadcastreceiver接收器=新的broadcastreceiver()
private final BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
btArrayAdapter.add(deviceName);
deviceAddressArray.add(deviceHardwareAddress);
btArrayAdapter.notifyDataSetChanged();
}
}
};
红色下划线位置:device.getName()
问题: 呼叫需要用户可能拒绝的权限:代码应明确检查以查看是否可用(使用'Checkperssmerion)或明确处理潜在的“ SecurityException” 。。
我已经在androidmanifest.xml上添加了权限
<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" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
,但我仍然找不到权限出了什么问题... 对不起,我的英语不好..但是我想知道这个问题发生以及如何解决的原因。
I'am trying to make bluetooth connection android App. There is a button click 'BluetoothSearchBtn' event handler. And I wanted to implement bluetooth searching function.
But there is a problem with permission, and shows me redlines under the codes.
The problems occured methods:
1. BluetoothSearchBtn.setOnClickListener(new View.OnClickListener()){}
2. private final BroadcastReceiver receiver = new BroadcastReceiver(){}
- variable declaration
Set<BluetoothDevice> pairedDevices;
ArrayAdapter<String> btArrayAdapter;
ArrayList<String> deviceAddressArray;
BluetoothAdapter btAdapter;
btAdapter = BluetoothAdapter.getDefaultAdapter();
btArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
deviceAddressArray = new ArrayList<>();
This is my code which shows the redlines.
- BluetoothSearchBtn.setOnClickListener(new View.OnClickListener()
BluetoothSearchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Check if the device is already discovering
if(btAdapter.isDiscovering()){
btAdapter.cancelDiscovery();
} else {
if (btAdapter.isEnabled()) {
btAdapter.startDiscovery();
btArrayAdapter.clear();
if (deviceAddressArray != null && !deviceAddressArray.isEmpty()) {
deviceAddressArray.clear();
}
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, filter);
} else {
Toast.makeText(getApplicationContext(), "bluetooth not on", Toast.LENGTH_SHORT).show();
}
}
}
});
Red underline locations : btAdpater.isDiscovering()
, btAdapter.cancelDiscovering()
,btAdatper.startDiscovering()
Problem : Call requires permission which may be rejected by user:code should explicitly check to see if permission is available (with 'checkPersmission) or explicitly handle a potential 'SecurityException'.
- private final BroadcastReceiver receiver = new BroadcastReceiver()
private final BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
btArrayAdapter.add(deviceName);
deviceAddressArray.add(deviceHardwareAddress);
btArrayAdapter.notifyDataSetChanged();
}
}
};
Red underline locations : device.getName()
Problem : Call requires permission which may be rejected by user:code should explicitly check to see if permission is available (with 'checkPersmission) or explicitly handle a potential 'SecurityException'.
I have already added permissions on AndroidManifest.xml
<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" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
But I can't still find out what is wrong with permissions...
I'm sorry my english bad.. but I want to know the reason why this problem occurs and how to solve.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您应该检查是否需要许可,只需单击此按钮添加权限检查:
或按Alt+Enter,然后Android Studio将为您生成所需的代码
First of all, you should check whether you have required permission or not, just add permission check by clicking on this button:
or press alt+enter and then Android Studio will generate the needed code for you