问题描述
我正在尝试在附近发现蓝牙设备,但是startDiscovery()总是返回false,就好像它不起作用一样.因此,它无法找到设备.
I am trying to discover bluetooth devices nearby, but startDiscovery() always returns false, as if it is not working. Therefore it is not able to find devices.
我看到除了Bluetooth和Bluetooth_Admin之外,我还必须包括Coarse_Location权限,但是无论如何,它不起作用.
I saw that i have to include Coarse_Location permission apart from Bluetooth and Bluetooth_Admin, but anyway, it doesn´t work.
这是我现在正在尝试的代码,主要包含一些痕迹以查看其工作方式:
Here is the code I am trying right now, where there are mainly traces to see how it works:
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清单中的权限:
And here the permissions in android manifest:
<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带下划线并表示无法找到方法值":
UPDATE: when I clicked to see the description of the startDiscovery method, I get this, with RequiresPermission and startDiscovery in red, saying "Cannot resolve method" and Manifest.permission.BLUETOOTH_ADMIN underlined and saying "Cannot find method value" :
@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;
}
推荐答案
您是否检查过该位置已在设备上激活?
Have you checked that the location is activated on your device?
即使授予了位置权限,当禁用该权限时,蓝牙API也无法正常工作.
Even if the location permission is granted, when it is disabled, the bluetooth API doesn't work.
因此,我建议您添加以下代码以确保许可有效:
So I suggest you to add the following code to be sure that the permission is effective:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!