[英]BluetoothAdapter.getDefaultAdapter().startDiscovery() not working
public void startScan() {
final List<MyBluetoothDevice> arrayOfFoundBTDevices = new ArrayList<>();
// start looking for bluetooth devices
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
boolean scanStatus = mBluetoothAdapter.startDiscovery();
Timber.d("SCANNING_STATUS : " + scanStatus);
// Discover new devices
// Create a BroadcastReceiver for ACTION_FOUND
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Timber.d("onReceiveSignal");
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the bluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
Timber.d("Discovery Finished ");
AppUtils.showToast(context, "Scanning restart");
mBluetoothAdapter.startDiscovery();
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
context.registerReceiver(mReceiver, filter);
}
I am try to scan nearby bluetooth devices.我正在尝试扫描附近的蓝牙设备。 But, BluetoothAdapter.getDefaultAdapter().startDiscovery() method returns false in API Level 29 but working in API Level 26
但是,BluetoothAdapter.getDefaultAdapter().startDiscovery() 方法在 API 级别 29 中返回 false,但在 API 级别 26 中工作
-------------Permission defined in AndroidManifest.xml----------- Can't find solution for Android 10 -------------在AndroidManifest.xml 中定义的权限----------- 找不到Android 10 的解决方案
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluetoothexample">
<uses-feature
android:name="android.hardware.bluetooth"
android:required="true" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FoundBTDevices" />
</application>
</manifest>
您需要在 Android 10 中开启位置信息才能扫描附近的设备。
not sure if you are still looking for the answer.不知道你是否还在寻找答案。 From API 29, Google requires LOCATION group permission to use some Bluetooth function.
从 API 29 开始,Google 需要 LOCATION 组权限才能使用某些蓝牙功能。 So you will need to add some <uses-permission> tag to Android Manifest, and DON'T forget to request these permissions at RUNTIME.
因此,您需要在 Android Manifest 中添加一些 <uses-permission> 标记,并且不要忘记在 RUNTIME 请求这些权限。
I don't remember exactly which location is needed, so you can add all 3 following:我不记得确切需要哪个位置,因此您可以添加以下所有 3 个:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.