2017-10-05 119 views
1

我正在使用android studio製作藍牙應用程序,問題是應用程序只掃描一次設備。找到設備後,它會停止查找其他設備。我想搜索設備周圍的所有可用設備。活動代碼:只有一個設備被檢測到藍牙

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); 
     } 
    } 
} 

的代碼,XML文件是:

<?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> 

如何掃描所有可用的設備?

回答

2

那麼在你BroadcastReceiver.onReceive方法一旦被調用你撥打:

mBluetoothAdapter.cancelDiscovery(); 

所以這個停止情迷過程。嘗試刪除這條線,看看會發生什麼。

記住但是保持從official documentation如下:

因爲發現是藍牙 適配器一個重量級的過程中,這種方法應該總是試圖 之前調用connect連接到遠程設備( )。發現不由 活動管理,但作爲系統服務運行,因此應用程序應該始終調用取消發現,即使它沒有直接請求 發現,只是可以肯定。

相關問題