用蓝牙扫描设备

时间:2017-12-21 15:13:29

标签: android bluetooth discovery

我从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

什么有效:   - 有界设备列表   - 广播“开始发现”和“结束发现”

什么行不通: - 发现附近的设备

谢谢

1 个答案:

答案 0 :(得分:0)

您需要为找到的设备添加以下权限之一:

<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)开始,用户在应用运行时向应用授予权限,而非在安装应用时授予

请参阅this guide以获取许可。