2017-10-05 5 views
1

Je fais une application bluetooth en utilisant android studio et le problème est que l'application ne scanne qu'une seule fois les périphériques. Après avoir trouvé un appareil, il ne recherche plus d'autres appareils. Je souhaite rechercher tous les appareils disponibles autour de mon appareil. Le code d'activité est:Seul un périphérique est détecté en bluetooth

package com.example.yubrajsharma.my_application; 

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.util.Log; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.ProgressBar; 

import java.util.ArrayList; 

import static android.view.View.INVISIBLE; 

public class MainActivity extends AppCompatActivity{ 
    BluetoothAdapter mBluetoothAdapter; 
    int count; 
    Button lister; 
    public ArrayList<BluetoothDevice> mBTDevices = new ArrayList<>(); 
    String[] mBTDevice; 
    ArrayAdapter<String> adapter; 
    private static final String TAG = "MainActivity"; 

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 
      if (action.equals(mBluetoothAdapter.ACTION_STATE_CHANGED)) { 
       final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, mBluetoothAdapter.ERROR); 
      } 
     } 
    }; 
    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     unregisterReceiver(mReceiver); 
     unregisterReceiver(mReciever); 
    } 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 
     ProgressBar pg = (ProgressBar) findViewById(R.id.progressBar); 
     pg.setVisibility(View.INVISIBLE); 
     Button searchbtn = (Button) findViewById(R.id.searchbtn); 
     Button lister = (Button) findViewById(R.id.lists); 
     mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     if(mBluetoothAdapter.isEnabled()){ 
      lister.setVisibility(View.VISIBLE); 
     } 
     lister.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       listdata(); 
      } 
     }); 
    } 
    private BroadcastReceiver mReciever = new BroadcastReceiver(){ 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      final String action = intent.getAction(); 
      ProgressBar pg = (ProgressBar) findViewById(R.id.progressBar); 
      if (action.equals(BluetoothDevice.ACTION_FOUND)) { 
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
        mBTDevices.add(device); 

      } 
      count = mBTDevices.size(); 
      int j = 0; 
      mBTDevice = new String[count]; 
      if(count>0) { 
       for (BluetoothDevice device : mBTDevices) { 
        mBTDevice[j] = device.getName(); 
        j++; 
        } 
       ListView pairing = (ListView) findViewById(R.id.paired); 
       adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,mBTDevice); 
       pairing.setAdapter(adapter); 
       } 
      else{ 
       mBTDevice[0] = "no devices found"; 
       ListView pairing = (ListView) findViewById(R.id.paired); 
       adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,mBTDevice); 
       pairing.setAdapter(adapter); 
      } 
      mBluetoothAdapter.cancelDiscovery(); 
      pg.setVisibility(View.INVISIBLE); 
      Log.d(TAG, "Disabled"); 
     } 
    }; 
    private void listdata() { 
     if(mBluetoothAdapter.isDiscovering()) { 
      mBluetoothAdapter.cancelDiscovery(); 
     } 
     mBluetoothAdapter.startDiscovery(); 

     ProgressBar pg = (ProgressBar) findViewById(R.id.progressBar); 
     pg.setVisibility(View.VISIBLE); 
     IntentFilter infill = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
     registerReceiver(mReciever, infill); 
    } 
    public void enableDisableBT(View view) { 
     Button lister = (Button) findViewById(R.id.lists); 
     if(!mBluetoothAdapter.isEnabled()){ 
      Intent enableBTintent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivity(enableBTintent); 

      IntentFilter btIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); 
      registerReceiver(mReceiver, btIntent); 
     } 
     if(mBluetoothAdapter.isEnabled()){ 
      mBluetoothAdapter.disable(); 
      IntentFilter btIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); 
      registerReceiver(mReceiver, btIntent); 
     } 
    } 
} 

Le code pour le fichier xml est:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.yubrajsharma.my_application.MainActivity" 
    tools:layout_editor_absoluteY="81dp" 
    tools:layout_editor_absoluteX="0dp"> 

    <Button 
     android:id="@+id/lists" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="List Devices" 
     android:visibility="invisible" 
     app:layout_constraintLeft_toRightOf="@+id/searchbtn" 
     app:layout_constraintBottom_toBottomOf="@+id/searchbtn" 
     android:layout_marginRight="28dp" 
     android:layout_marginEnd="28dp" 
     android:layout_alignParentTop="true" 
     android:layout_alignRight="@+id/paired" 
     android:layout_alignEnd="@+id/paired" /> 

    <Button 
     android:id="@+id/searchbtn" 
     android:onClick="enableDisableBT" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="ON/OFF" 
     android:visibility="visible" 
     tools:layout_editor_absoluteX="0dp" 
     tools:layout_editor_absoluteY="3dp" /> 

    <ListView 
     android:id="@+id/paired" 
     android:layout_width="368dp" 
     android:layout_height="422dp" 
     app:layout_constraintTop_toBottomOf="@+id/lists" 
     app:layout_constraintLeft_toLeftOf="@+id/searchbtn" 
     android:layout_marginBottom="13dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <ProgressBar 
     android:id="@+id/progressBar" 
     style="?android:attr/progressBarStyle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_marginEnd="29dp" 
     android:layout_marginRight="29dp" 
     android:layout_toLeftOf="@+id/lists" 
     android:layout_toStartOf="@+id/lists" 
     tools:visibility="invisible" /> 


</RelativeLayout> 

Comment numériser tous les périphériques disponibles?

Répondre

2

bien dans votre méthode BroadcastReceiver.onReceive une fois qu'il est appelé que vous appelez:

mBluetoothAdapter.cancelDiscovery(); 

Donc, ce arrête le processus de découverte. Essayez d'enlever cette ligne et voir ce qui se passe.

Cependant, gardez à l'esprit les éléments suivants de la official documentation:

Parce que la découverte est une procédure des poids lourds pour l'adaptateur Bluetooth , cette méthode devrait toujours être appelée avant de tenter de connecter à un périphérique distant avec connexion (). La découverte n'est pas gérée par l'activité , mais elle est exécutée en tant que service système. Par conséquent, une application doit toujours appeler la découverte d'annulation, même si elle n'a pas directement demandé une découverte , pour être sûr.