在Android Studio的列表视图中接收扫描的蓝牙设备的重复条目

时间:2019-04-19 01:38:11

标签: java

显示重复的Mac地址

duplicate mac address displayed

我是Android开发的新手,正在为我的项目开发蓝牙应用程序,在该项目中,我需要扫描蓝牙设备并与其中之一配对。不幸的是,我可以在列表视图中看到多次显示相同蓝牙设备的mac地址。任何帮助解决该错误将不胜感激。谢谢。

package com.example.xyz;
import android.Manifest;
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.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private static final int REQUEST_ENABLE_BT = 1;

    BluetoothAdapter mBluetoothAdapter;
    TextView BluetoothStatus;
    ImageView BTIstatus;
    Button Discover;
    //public ArrayList<BluetoothDevice> mBTDevices = new ArrayList<>();
    //public DevicelistAdapter mDevicelistAdapter;
    ArrayAdapter<String> btArrayAdapter;
    ListView listDevicesFound;

    // Create a BroadcastReceiver for ACTION_FOUND
    private final BroadcastReceiver mBroadcastReceiver1 = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            //when discovery finds a device
            if (action.equals(mBluetoothAdapter.ACTION_STATE_CHANGED)){
                final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,mBluetoothAdapter.ERROR);
                switch(state){
                    case BluetoothAdapter.STATE_OFF: //State off = 10
                        Log.d(TAG, "OnReceive: STATE OFF");
                        break;
                    case BluetoothAdapter.STATE_TURNING_OFF: //State turning off = 13
                        Log.d(TAG,"mBroadcastReceiver1: STATE TURNING OFF");
                        break;
                    case BluetoothAdapter.STATE_ON: //State ON = 12
                        Log.d(TAG,"MBroadcastReceiver1: STATE ON");
                        break;
                    case BluetoothAdapter.STATE_TURNING_ON: //State on = 11
                        Log.d(TAG,"MBroadcastReceiver1: STATE ON");
                        break;
                }
            }
        }
    };

    //BroadcastReceiver2
    private final BroadcastReceiver mBroadcastReceiver2 = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (action.equals(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED)){
                int mode = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE,mBluetoothAdapter.ERROR);
                switch(mode){
                    case BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE:
                        Log.d(TAG, "OnBroadcastReceiver2: Discoverabilty Enabled");
                        break;
                    case BluetoothAdapter.SCAN_MODE_CONNECTABLE: //
                        Log.d(TAG,"mBroadcastReceiver2: Discoverabilty Enabled, Able to Receive connections");
                        break;
                    case BluetoothAdapter.SCAN_MODE_NONE: //
                        Log.d(TAG,"MBroadcastReceiver2: Discoverabilty disabled");
                        break;
                    case BluetoothAdapter.STATE_CONNECTING: //
                        Log.d(TAG,"MBroadcastReceiver2: Connecting...");
                        break;
                    case BluetoothAdapter.STATE_CONNECTED: //
                        Log.d(TAG,"MBroadcastReceiver2: Connected");
                        break;
                }
            }
        }
    };

    //BroadcastReceiver3
    private final BroadcastReceiver mBroadcastReceiver3 = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            Log.d(TAG,"OnReceive: Action_Found");
            if (action.equals(BluetoothDevice.ACTION_FOUND)){
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                //mBTDevices.add(device);
                Log.d(TAG,"onReceiver:" +  device.getName() + " : " + device.getAddress());
                //mDevicelistAdapter = new DevicelistAdapter(context, R.layout.device_adapter_view, mBTDevices);
                btArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                btArrayAdapter.notifyDataSetChanged();
                //lvNewDevices.setAdapter(mDevicelistAdapter);
            }
        }
    };

    @Override
    protected void onDestroy() {
        Log.d(TAG, "OnDestroy: Called");
        super.onDestroy();
        unregisterReceiver(mBroadcastReceiver1);
        unregisterReceiver(mBroadcastReceiver2);
        unregisterReceiver(mBroadcastReceiver3);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button monoffBtn = findViewById(R.id.onoffBtn);
        BluetoothStatus = findViewById(R.id.BluetoothStatusTV);
        BTIstatus = findViewById(R.id.BluetoothIv);
        Discover = findViewById(R.id.discoverBtn);
        Button discoverdevices = findViewById(R.id.FindunpairedDevices);
        //lvNewDevices = findViewById(R.id.lvNewDevices);
        //mBTDevices = new ArrayList<>();
        listDevicesFound = findViewById(R.id.lvNewDevices);
        btArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1 );
        listDevicesFound.setAdapter(btArrayAdapter);


        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        checkBTStatus();
        checkOnOffstatus();
        monoffBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "Enable/Disable Bluetooth");
                enableDisableBT();
            }
        });
        discoverdevices.setOnClickListener(discoverdevicesOnClickListener);
        registerReceiver(mBroadcastReceiver3,
                new IntentFilter(BluetoothDevice.ACTION_FOUND));

    }
    public void checkOnOffstatus(){
        if(mBluetoothAdapter.isEnabled()){
            BTIstatus.setImageResource(R.drawable.ic_action_on);
        }
        else{
            BTIstatus.setImageResource(R.drawable.ic_action_off);
        }

    }

    public void checkBTStatus(){
        if (mBluetoothAdapter == null) {
            BluetoothStatus.setText("Bluetooth is not Available");
            Log.d(TAG,"enableDisableBT: Bluetooth not available");
        }
        else{
            BluetoothStatus.setText("Bluetooth is Available");
            Log.d(TAG,"enableDisableBT: Bluetooth is Available");
        }
    }

    public void enableDisableBT() {
        if(!mBluetoothAdapter.isEnabled()){
            Log.d(TAG,"Enabling Bluetooth");
            showToast("Turning On Bluetooth");
            //Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            BTIstatus.setImageResource(R.drawable.ic_action_on);
            mBluetoothAdapter.enable();
            //startActivity(enableBTIntent);

            IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
            registerReceiver(mBroadcastReceiver1, BTIntent);

        }
        if(mBluetoothAdapter.isEnabled()){
            Log.d(TAG,"Disabling Bluetooth");
            showToast("Turning Off Bluetooth");
            mBluetoothAdapter.disable();
            btArrayAdapter.clear();
            BTIstatus.setImageResource(R.drawable.ic_action_off);

            IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
            registerReceiver(mBroadcastReceiver1, BTIntent);
        }
    }

    private Button.OnClickListener discoverdevicesOnClickListener = new Button.OnClickListener(){

        @Override
        public void onClick(View view){
            btArrayAdapter.clear();
            mBluetoothAdapter.startDiscovery();
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if(requestCode == REQUEST_ENABLE_BT){
            checkBTStatus();
        }
    }

    private void showToast(String msg) {
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }

    public void Discoverablebtn(View view) {
        Log.d(TAG,"Making Device Discoverable for 300 seconds");
        Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,300);
        startActivity(discoverableIntent);

        IntentFilter DisFilter = new IntentFilter(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
        registerReceiver(mBroadcastReceiver2, DisFilter);
    }

0 个答案:

没有答案