Android Bluetooth API找不到任何设备
我正在尝试在我的应用程序中实现蓝牙设备发现功能。我已经用Android Bluetooth API找不到任何设备,android,bluetooth,android-bluetooth,Android,Bluetooth,Android Bluetooth,我正在尝试在我的应用程序中实现蓝牙设备发现功能。我已经用BluetoothAdapter和BroadcastReceiver实现了建议的方法,如下所示: My Activity (discover() is called in onCreate): public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback { BluetoothAdapter mBluetoothA
BluetoothAdapter
和BroadcastReceiver
实现了建议的方法,如下所示:
My Activity (discover() is called in onCreate):
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {
BluetoothAdapter mBluetoothAdapter;
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//Finding devices
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
//discovery starts, we can show progress dialog or perform other tasks
testDevice("Start discover");
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//discovery finishes, dismis progress dialog
testDevice("Discovery finished");
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
//bluetooth device found
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
testDevice("Found device " + device.getName());
}
}
};
public void discover() {
this.mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);
mBluetoothAdapter.startDiscovery();
}
public void testDevice(String msg) {
Toast.makeText(this, msg,
Toast.LENGTH_LONG).show();
}
}
在我的清单中,我拥有以下权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
结果我得到了两个祝酒词:“开始探索”和10秒后的“探索完成”。找不到任何设备。我已经在三星S9+、三星Tab A和摩托罗拉某物(蓝牙开启)上进行了测试
有什么我没有做,或者有什么我不知道的已知问题吗?这纯粹是猜测,但如果您运行的是API级别23+(很可能是),您需要动态请求权限,而不仅仅是在清单中声明权限。要么使用
rxp权限
要么使用ContextCompat.checkSelfPermission
不是它,我试过了。实际上,当我从清单和代码中删除权限时,当我调用BluetoothAdapter时,应用程序崩溃。感谢您的尝试。您是否尝试动态检查访问\u位置权限?