2017-12-21 11 views
1

Ich habe den folgenden Code aus der Android-Dokumentation kopiert, aber kein Gerät entdeckt. Wer weiß warum?Scan-Geräte mit Bluetooth

public class MainActivity AppCompatActivity erstreckt {

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

Was funktioniert: - Liste der beschränkten Geräte - Ausstrahlung für "starten Entdeckung" und "Ende Entdeckung"

Was nicht funktioniert: - Discovery in der Nähe Geräte

Danke

Antwort

0

Sie müssen gefundenen Geräte eine der folgenden Erlaubnis hinzuzufügen:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

oder

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

Wenn Sie auf API 23 und höher arbeiten müssen Sie diese Berechtigung gewährleisten wird gewährt, da es sich um eine gefährliche Levelberechtigung handelt.

Ab Android 6.0 (API-Ebene 23), Benutzer-Berechtigungen Apps gewähren, während die App ausgeführt wird, nicht, wenn sie die App

installieren this guide Siehe Erlaubnis zu beantragen.