2016-03-22 30 views
7

Bluetooth in 6.01 sembra non funzionare come previsto con il seguente codice e le autorizzazioni in base all'aggiornamento + appCompat per Kitkat 4.4.4.Android Marshmallow 6.0.1 Scansione Bluetooth non restituisce risultati

Nessun risultato viene restituito e ho diversi dispositivi rilevabili nelle vicinanze.

Qualcuno ha qualche idea del perché? Sono in esecuzione su un Nexus 5.

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

package bluetoothscanneractivity; 

import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.Toast; 

import java.util.ArrayList; 

public class BluetoothScannerActivity extends Activity { 
    //private final BroadcastReceiver FoundReceiver = null; 
    protected ArrayList<BluetoothDevice> foundDevices = new ArrayList<BluetoothDevice>(); 
    private ListView foundDevicesListView; 
    private ArrayAdapter<BluetoothDevice> btArrayAdapter; 

    /** 
    * Called when the activity is first created. 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_bluetooth_scanner); 
     final BluetoothAdapter myBlueToothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     final Button scanb = (Button) findViewById(R.id.button); 
     final ListView foundDevicesListView = (ListView) findViewById(R.id.listView1); 

     btArrayAdapter = new ArrayAdapter<BluetoothDevice>(this, 
       android.R.layout.simple_list_item_1, foundDevices); 
     foundDevicesListView.setAdapter(btArrayAdapter); 

     //Turn on Bluetooth 
     if (myBlueToothAdapter == null) 
      Toast.makeText(BluetoothScannerActivity.this, "Your device doesnot support Bluetooth", Toast.LENGTH_LONG).show(); 
     else if (!myBlueToothAdapter.isEnabled()) { 
      Intent BtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(BtIntent, 0); 
      Toast.makeText(BluetoothScannerActivity.this, "Turning on Bluetooth", Toast.LENGTH_LONG).show(); 
     } 
     //scan 
     scanb.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       btArrayAdapter.clear(); 
       myBlueToothAdapter.startDiscovery(); 
       Toast.makeText(BluetoothScannerActivity.this, "Scanning Devices", Toast.LENGTH_LONG).show(); 

      } 
     }); 

     registerReceiver(FoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); 
     IntentFilter filter = new IntentFilter(
       BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
     this.registerReceiver(FoundReceiver, filter); 

    } 

    @Override 
    protected void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     unregisterReceiver(FoundReceiver); 
    } 

    private final BroadcastReceiver FoundReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 

      String action = intent.getAction(); 

      // When discovery finds a new device 
      if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
       // Get the BluetoothDevice object from the Intent 
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
       if (!foundDevices.contains(device)) { 
        foundDevices.add(device); 
        btArrayAdapter.notifyDataSetChanged(); 
       } 
      } 

      // When discovery cycle finished 
      if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { 
       if (foundDevices == null || foundDevices.isEmpty()) { 
        Toast.makeText(BluetoothScannerActivity.this, "No Devices", Toast.LENGTH_LONG).show(); 
       } 
      } 

     } 
    }; 


} 
+0

Si prega di incollare il codice del metodo startDiscovery()? –

+0

Non è necessario: è la chiamata standard BL di Android. – mcdoomington

+0

È possibile visualizzare il brindisi - Toast.makeText (BluetoothScannerActivity.questo "Dispositivi di scansione", Toast.LENGTH_LONG) .show(); –

risposta

4

Con i permessi aggiunto, cambiando a min versione SDK per 23 - funziona con il seguente:

package bluetoothscanneractivity; 

import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.Toast; 
import android.Manifest; 

import java.util.ArrayList; 

public class BluetoothScannerActivity extends AppCompatActivity { 
    //private final BroadcastReceiver FoundReceiver = null; 
    protected ArrayList<BluetoothDevice> foundDevices = new ArrayList<BluetoothDevice>(); 
    private ListView foundDevicesListView; 
    private ArrayAdapter<BluetoothDevice> btArrayAdapter; 

    /** 
    * Called when the activity is first created. 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_bluetooth_scanner); 
     final BluetoothAdapter myBlueToothAdapter = BluetoothAdapter.getDefaultAdapter(); 

     final Button scanb = (Button) findViewById(R.id.button); 
     final ListView foundDevicesListView = (ListView) findViewById(R.id.listView1); 

     btArrayAdapter = new ArrayAdapter<BluetoothDevice>(this, 
       android.R.layout.simple_list_item_1, foundDevices); 

     foundDevicesListView.setAdapter(btArrayAdapter); 

     //Turn on Bluetooth 
     if (myBlueToothAdapter == null) 
      Toast.makeText(BluetoothScannerActivity.this, "Your device doesnt support Bluetooth", Toast.LENGTH_LONG).show(); 
     else if (!myBlueToothAdapter.isEnabled()) { 
      Intent BtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(BtIntent, 0); 
      Toast.makeText(BluetoothScannerActivity.this, "Turning on Bluetooth", Toast.LENGTH_LONG).show(); 
     } 

     // Quick permission check 
     int permissionCheck = this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION"); 
     permissionCheck += this.checkSelfPermission("Manifest.permission.ACCESS_COARSE_LOCATION"); 
     if (permissionCheck != 0) { 

      this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001); //Any number 
     } 


     scanb.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       btArrayAdapter.clear(); 

       myBlueToothAdapter.startDiscovery(); 

       Toast.makeText(BluetoothScannerActivity.this, "Scanning Devices", Toast.LENGTH_LONG).show(); 

      } 
     }); 

     registerReceiver(FoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); 
     IntentFilter filter = new IntentFilter(
       BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
     this.registerReceiver(FoundReceiver, filter); 

    } 

    @Override 
    protected void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     unregisterReceiver(FoundReceiver); 
    } 


    private final BroadcastReceiver FoundReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 

      String action = intent.getAction(); 

      // When discovery finds a new device 
      if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
       // Get the BluetoothDevice object from the Intent 
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
       if (!foundDevices.contains(device)) { 
        foundDevices.add(device); 
        Toast.makeText(BluetoothScannerActivity.this, "name: " + device.getName() + " " + device.getAddress(), Toast.LENGTH_LONG).show(); 
        btArrayAdapter.notifyDataSetChanged(); 
       } 

      } 

      // When discovery cycle finished 
      if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { 
       if (foundDevices == null || foundDevices.isEmpty()) { 
        Toast.makeText(BluetoothScannerActivity.this, "No Devices", Toast.LENGTH_LONG).show(); 
       } 
      } 

     } 
    }; 


} 
+0

Non sono uno sviluppatore di app, ma ho notato che la mia app "blueterm" non si collega più al mio dispositivo incorporato da quando il mio telefono è passato ad Android 6.0.1. Si tratta di un problema che si applicherebbe a un'applicazione del genere? –

+0

Sospetto di sì - è possibile modificare la gestione delle autorizzazioni in quanto si è trattato di una corsa rapida e sporca. – mcdoomington

+0

grazie, presumo che sia necessario avere accesso al codice sorgente "blueterm", giusto? –

13

Voi sapete che da quando Marshmallow, avete bisogno di queste autorizzazioni per il vostro compito -

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

Anche perché Marshmallow, è necessario richiedere a livello di codice per le autorizzazioni anche se si li hai dichiarati nel tuo file manifest.

Quindi, si deve richiedere le autorizzazioni di localizzazione prima di startDiscovery()

ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001); //Any number 

E dopo che l'utente accetta queste autorizzazioni, è possibile startDiscovery(). E se lui/lei nega, non è possibile scoprire i dispositivi. È possibile controllare l'azione dell'utente in

onRequestPermissionsResult() richiamata.

+0

Ah, non sapevo di dover richiedere l'autorizzazione per la richiesta programmatica, lo controllerò. – mcdoomington

+0

Ahh non sapevo di dover richiedere le autorizzazioni in modo programmatico, ma ancora non funziona. Ho persino abilitato l'autorizzazione tramite la pagina di autorizzazione informativa dell'app. – mcdoomington

+0

Posso confermare che questo ha risolto il problema per me, lo faccio sulla mia attività principale. – Dayan

2

Il metodo seguito eseguirà solo se la versione SDK è> LOLLIPOP. Quando ho provato a usarlo senza questa restrizione, la mia app si è bloccata. Basta chiamare questo metodo appena prima di utilizzare .startDiscovery();

public void checkBTPermissions(){ 
    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){ 
      int permissionCheck = this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION"); 
     permissionCheck += this.checkSelfPermission("Manifest.permission.ACCESS_COARSE_LOCATION"); 
     if (permissionCheck != 0) { 

      this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001); //Any number 
     } 
    }else{ 
     Log.d(TAG, "checkBTPermissions: No need to check permissions. SDK version < LOLLIPOP."); 
    } 
} 
Problemi correlati