【问题标题】:how to search unpaired bluetooth devices?如何搜索未配对的蓝牙设备?
【发布时间】:2015-05-05 23:45:20
【问题描述】:

我正在做一个使用蓝牙通信的应用程序。

首先,我试图获取设备可检测到的所有蓝牙设备的列表。

我已经设法在列表视图中加载所有配对的设备,但我无法列出未配对的设备,我找到了一些教程,但我有点困惑,因为我有点新安卓开发和蓝牙通信。

所以我需要找到范围内的每个蓝牙设备,我不知道该怎么做(懒惰)

这是(编辑的)代码:

com.voice.benz.instaurentremote 包;

import android.bluetooth.*;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.widget.ArrayAdapter;
import java.util.Set;
import android.content.Context;
import android.content.BroadcastReceiver;
import android.view.Window;
import android.app.Activity;
import android.content.IntentFilter;
import android.util.Log;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;
import java.util.ArrayList;
public class bluetooth extends ActionBarActivity {

    private TextView bluetoothPaired;
    private TextView txt_status;
    private BluetoothAdapter btAdapter;

    private ListView newdevices_listview;

    private Set<BluetoothDevice>pairedDevices;
    private ArrayAdapter<String> adapter = null;
    private static final int BLUETOOTH_ON = 1000;
    private static final int REQUEST_ENABLE_BT = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_bluetooth);

        // Initialize the button to perform device discovery

        txt_status          = (TextView) findViewById(R.id.txt_status);


        newdevices_listview = (ListView)findViewById(R.id.newdevices_listview);
        adapter=new             ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
        newdevices_listview.setAdapter(adapter);


        // Initialize adapter
        btAdapter =  BluetoothAdapter.getDefaultAdapter();


// Register for broadcasts when a device is discovered
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        this.registerReceiver(mReceiver, filter);

        filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        this.registerReceiver(mReceiver, filter);

    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
                //discovery starts, we can show progress dialog or perform other tasks
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                //discovery finishes, dismis progress dialog
            } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                //bluetooth device found
                BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                txt_status.setText("Found device " + device.getName());
                adapter.add(device.getName() + "\n" + device.getAddress());
            }
        }
    };



    public void attivaBluetooth (View view) {

        if (!btAdapter.isEnabled()) {
            Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(turnOn, 1);

        }
    }

        public void cercaDispositivi (View view)
    {
        IntentFilter filter = new IntentFilter();

        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

        registerReceiver(mReceiver, filter);
        btAdapter.startDiscovery();

    }


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

    public void disattivaBluetooth (View view)
    {
        if (btAdapter.isEnabled())
        {
            btAdapter.disable();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_bluetooth, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }



}

【问题讨论】:

    标签: android search bluetooth device


    【解决方案1】:

    您似乎没有在任何地方注册您的 BroadcastReceiver myBluetoothReceiver。如果您在onResume()onCreate 中这样做,您可能会有更多的运气。 :)

    【讨论】:

    • 我已经编辑了代码,现在它可以部分工作了。当我在我的应用程序上按下“查找设备”按钮时,它什么也不做,但是当我将设备与运行应用程序的设备相关联时,一旦配对完成,应用程序就会在列表视图中显示配对的设备。顺便说一句,这不是我需要的,因为当我按下“查找设备”按钮时,什么也没有发生,看起来我错过了什么
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 2018-08-15
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多