我试图发现附近的蓝牙设备,但 startDiscovery() 总是返回 false,就好像它不工作一样。因此它无法找到设备。
我看到除了蓝牙和 Bluetooth_Admin 之外,我必须包含 Coarse_Location 权限,但无论如何,它不起作用。
这是我现在正在尝试的代码,其中主要有痕迹可以看到它是如何工作的:
public class BluetoothActivity extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<BluetoothDevice> dispositivos;
BTAdapter adaptador;
BluetoothAdapter mBluetoothAdapter;
Button buscar;
final int REQUEST_ACCESS_COARSE_LOCATION = 16;
@Override
public void onCreate(Bundle savedInsanceState){
super.onCreate(savedInsanceState);
setContentView(R.layout.bluetooth_activity);
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter != null) {
Toast.makeText(this, "No es nulo", Toast.LENGTH_SHORT).show();
if (!mBluetoothAdapter.isEnabled()) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, RESULT_OK);
}
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);
}
buscar = findViewById(R.id.buscar);
buscar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(BluetoothActivity.this, "Empezar a buscar", Toast.LENGTH_SHORT).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
switch (ContextCompat.checkSelfPermission(BluetoothActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
case PackageManager.PERMISSION_DENIED:
ActivityCompat.requestPermissions(BluetoothActivity.this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_ACCESS_COARSE_LOCATION);
break;
case PackageManager.PERMISSION_GRANTED:
boolean a = mBluetoothAdapter.startDiscovery();
Toast.makeText(BluetoothActivity.this, "Start discovery es "+a, Toast.LENGTH_SHORT).show();
break;
}
}
}
});
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)){
Toast.makeText(BluetoothActivity.this, "Encontrado", Toast.LENGTH_SHORT).show();
}
}
};
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_ACCESS_COARSE_LOCATION: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
boolean a = mBluetoothAdapter.startDiscovery();
Toast.makeText(BluetoothActivity.this, "Start discovery es "+a, Toast.LENGTH_SHORT).show();
}
else {
//exit application or do the needful
}
return;
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
}
}
这里是android list 中的权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-feature android:name="android.hardware.bluetooth" />
更新:当我点击查看 startDiscovery 方法的描述时,我得到了这个,RequiresPermission 和 startDiscovery 是红色的,说“无法解析方法”和 Manifest.permission.BLUETOOTH_ADMIN 下划线并说“找不到方法值”:
@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
public boolean startDiscovery() {
if (getState() != STATE_ON) {
return false;
}
try {
mServiceLock.readLock().lock();
if (mService != null) {
return mService.startDiscovery(getOpPackageName());
}
} catch (RemoteException e) {
Log.e(TAG, "", e);
} finally {
mServiceLock.readLock().unlock();
}
return false;
}
最佳答案
您是否检查过该位置已在您的设备上激活?
即使授予位置权限,当它被禁用时,蓝牙 API 也不起作用。
所以我建议你添加以下代码以确保权限有效:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!isGpsEnabled) {
startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), MY_REQUEST_CODE);
}
关于Android 蓝牙 StartDiscovery() 总是返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61984202/