我从 Android 文档中复制了以下代码,但没有发现任何设备。有谁知道为什么吗?
公共类 MainActivity 扩展 AppCompatActivity {
private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private TextView tvBoundedDevices;
private TextView tvDiscoveredDevices;
private IntentFilter intentFilter = new IntentFilter();
private BroadcastReceiver broadcastReceiver;
private ProgressDialog progressDialog;
private ArrayList<BluetoothDevice> bluetoothDevicesFound = new ArrayList<>();
private Set<BluetoothDevice> bluetoothDevicesBounded;
@Override
protected void onDestroy() {
unregisterReceiver(broadcastReceiver);
bluetoothAdapter.cancelDiscovery();
super.onDestroy();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvBoundedDevices = findViewById(R.id.tvBoundedDevices);
tvDiscoveredDevices = findViewById(R.id.tvDiscoveredDevices);
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action){
case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
progressDialog = ProgressDialog.show(MainActivity.this,"Attendere","Scan in corso");
break;
case BluetoothDevice.ACTION_FOUND:
bluetoothDevicesFound.add((BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE));
break;
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
if(progressDialog.isShowing())
progressDialog.dismiss();
break;
default:
Toast.makeText(context, "Action=" + action, Toast.LENGTH_LONG).show();
}
}
};
registerReceiver(broadcastReceiver,intentFilter);
}
public void turnBluetoothOn(View view){
if(!bluetoothAdapter.isEnabled()){
bluetoothAdapter.enable();
Toast.makeText(this,"Bluetooth attivato",Toast.LENGTH_SHORT).show();
}
}
public void turnBluetoothOff(View view){
if(bluetoothAdapter.isEnabled()){
bluetoothAdapter.disable();
Toast.makeText(this,"Bluetooth disattivato",Toast.LENGTH_SHORT).show();
}
}
public void scanDevices(View view){
turnBluetoothOn(null);
if(bluetoothAdapter.isDiscovering())
bluetoothAdapter.cancelDiscovery();
Toast.makeText(this, "Scansione " + (bluetoothAdapter.startDiscovery()?"":"non") + " avviata.", Toast.LENGTH_SHORT).show();
printDevices();
}
String toStamp = "";
private void printDevices(){
bluetoothDevicesBounded = bluetoothAdapter.getBondedDevices();
if(!bluetoothDevicesBounded.isEmpty()){
toStamp = "Dispositivi associati:\n";
for(BluetoothDevice b : bluetoothDevicesBounded){
toStamp += b.getName() + " | " + b.getAddress() + "\n";
}
tvBoundedDevices.setText(toStamp + "");
}
if(!bluetoothDevicesFound.isEmpty()){
toStamp = "Dispositivi trovati:\n";
for(BluetoothDevice b : bluetoothDevicesFound){
toStamp += b.getName() + " | " + b.getAddress() + "\n";
}
tvDiscoveredDevices.setText(toStamp + "");
}
}
Pastebin:https://pastebin.com/CnEBAtwt
什么有效: - 绑定设备列表 - 广播“开始发现”和“结束发现”
什么不起作用: - 发现附近的设备
谢谢你
您需要为找到的设备添加以下权限之一:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
或
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
如果您正在使用 API 23 及更高版本,则必须确保授予此权限,因为这是危险级别的权限。
从 Android 6.0(API 级别 23)开始,用户在应用程序运行时向应用程序授予权限,而不是在安装应用程序时
请参阅本指南以请求许可。