在蓝牙中只检测到一个设备

时间:2017-10-05 13:22:23

标签: java android bluetooth android-bluetooth

我正在使用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>

如何扫描所有可用的设备?

1 个答案:

答案 0 :(得分:2)

一旦被调用,你的BroadcastReceiver.onReceive方法就会调用:

mBluetoothAdapter.cancelDiscovery();

所以这会阻止发现过程。尝试删除此行,看看会发生什么。

但请注意official documentation中的以下内容:

  

因为发现是蓝牙的重量级程序   适配器,在尝试之前应始终调用此方法   使用connect()连接到远程设备。发现不受管理   Activity,但作为系统服务运行,因此应用程序应该   即使没有直接请求,也总是调用取消发现   发现,只是为了确定。

相关问题