我的代码给出了一个错误,覆盖了这个注释,修复了错误。这是一个健康的解决方案吗?给出错误的类与蓝牙相关。
"Add permissions check" It gives an error when I do the suggestion.
错误:调用需要用户可能拒绝的权限:代码应该显式检查权限是否可用(使用checkPermission)或显式处理潜在的SecurityException
谢谢
我使用了@SuppressLint("MissingPermission"),但我不能相信
public synchronized void connected(BluetoothSocket socket, BluetoothDevice
device, final String socketType) {
// Cancel the thread that completed the connection
if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
// Cancel any thread currently running a connection
if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
// Cancel the accept thread because we only want to connect to one device
if (mSecureAcceptThread != null) {
mSecureAcceptThread.cancel();
mSecureAcceptThread = null;
}
// Start the thread to manage the connection and perform transmissions
mConnectedThread = new ConnectedThread(socket, socketType);
mConnectedThread.start();
// Send the name of the connected device back to the UI Activity
Message msg = mHandler.obtainMessage(BluetoothState.MESSAGE_DEVICE_NAME);
Bundle bundle = new Bundle();
bundle.putString(BluetoothState.DEVICE_NAME, device.getName());
bundle.putString(BluetoothState.DEVICE_ADDRESS, device.getAddress());
msg.setData(bundle);
mHandler.sendMessage(msg);
setState(BluetoothState.STATE_CONNECTED);
}
发布于 2022-03-30 06:36:12
AFAIK,为了避免出现MissingPermission
错误,您必须检查函数中的权限。
现在,如果您完全确定该权限在调用此函数之前被检查过,那么使用@SuppressLint("MissingPermission")
是可以的。另外,我要做的是,评论一下为什么你会抑制错误。行中的东西:@SuppressLint("MissingPermission") // permission must be checked before the call of the function!
。
https://stackoverflow.com/questions/71672424
复制相似问题