Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何搜索未配对的蓝牙设备?_Android_Search_Bluetooth_Device - Fatal编程技术网

Android 如何搜索未配对的蓝牙设备?

Android 如何搜索未配对的蓝牙设备?,android,search,bluetooth,device,Android,Search,Bluetooth,Device,我正在做一个应用程序,使用蓝牙通信 首先,我试图得到一个由设备检测到的所有蓝牙设备的列表 我已经设法在列表视图中加载所有配对设备,但我无法列出未配对的设备,我找到了一些教程,但我有点困惑,因为我对android开发和蓝牙通信有点陌生 所以我需要找到范围内的所有蓝牙设备,但我不知道怎么做(懒惰) 以下是(编辑过的)代码: 包com.voice.benz.instaurentremote import android.bluetooth.*; import android.support.v7.ap

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

首先,我试图得到一个由设备检测到的所有蓝牙设备的列表

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

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

以下是(编辑过的)代码:

包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.bluetooth.*;
导入android.support.v7.app.ActionBarActivity;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.ListView;
导入android.widget.TextView;
导入android.bluetooth.BluetoothAdapter;
导入android.content.Intent;
导入android.widget.ArrayAdapter;
导入java.util.Set;
导入android.content.Context;
导入android.content.BroadcastReceiver;
导入android.view.Window;
导入android.app.Activity;
导入android.content.IntentFilter;
导入android.util.Log;
导入android.widget.Button;
导入android.view.view.OnClickListener;
导入android.widget.AdapterView.OnItemClickListener;
导入java.util.ArrayList;
公共级蓝牙扩展了ActionBarActivity{
私有文本视图蓝牙;
私有文本视图txt_状态;
私人蓝牙适配器;
私有ListView新设备\u ListView;
私有设置对设备;
专用ArrayAdapter适配器=null;
专用静态最终int蓝牙_ON=1000;
私有静态最终整数请求_ENABLE_BT=1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u蓝牙);
//初始化按钮以执行设备发现
txt_status=(TextView)findViewById(R.id.txt_status);
newdevices\u listview=(listview)findViewById(R.id.newdevices\u listview);
adapter=newarrayadapter(这是android.R.layout.simple\u list\u item\u 1);
newdevices_listview.setAdapter(适配器);
//初始化适配器
btAdapter=BluetoothAdapter.getDefaultAdapter();
//在发现设备时注册广播
IntentFilter筛选器=新的IntentFilter(BluetoothDevice.ACTION\u已找到);
此。注册表接收程序(mReceiver,过滤器);
过滤器=新的IntentFilter(BluetoothAdapter.ACTION\u DISCOVERY\u FINISHED);
此。注册表接收程序(mReceiver,过滤器);
}
专用最终广播接收器mReceiver=新广播接收器(){
公共void onReceive(上下文、意图){
String action=intent.getAction();
if(BluetoothAdapter.ACTION\u DISCOVERY\u STARTED.equals(ACTION)){
//发现开始时,我们可以显示进度对话框或执行其他任务
}else if(BluetoothAdapter.ACTION\u DISCOVERY\u FINISHED.equals(ACTION)){
//发现完成,显示进度对话框
}else if(BluetoothDevice.ACTION_FOUND.equals(ACTION)){
//找到蓝牙设备
BluetoothDevice设备=(BluetoothDevice)intent.getParcelableExtra(BluetoothDevice.EXTRA_设备);
txt_status.setText(“找到的设备”+device.getName());
adapter.add(device.getName()+“\n”+device.getAddress());
}
}
};
公共无效的蓝牙(视图){
如果(!btAdapter.isEnabled()){
意向开启=新意向(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(打开,1);
}
}
公共空间cercaDispositivi(视图)
{
IntentFilter=newintentfilter();
filter.addAction(找到BluetoothDevice.ACTION);
filter.addAction(BluetoothAdapter.ACTION\u DISCOVERY\u已启动);
filter.addAction(BluetoothAdapter.ACTION\u DISCOVERY\u FINISHED);
registerReceiver(mrReceiver,过滤器);
btAdapter.startDiscovery();
}
@凌驾
受保护的空onDestroy(){
//TODO自动生成的方法存根
super.ondestory();
未注册接收人(mReceiver);
}
公共无效禁用蓝牙(视图)
{
if(btAdapter.isEnabled())
{
btAdapter.disable();
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.menu\u蓝牙,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
}

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

我已经编辑了代码,现在部分工作正常。当我按下应用程序上的“查找设备”按钮时,它不会执行任何操作,但当我将设备与应用程序运行的设备关联时,一旦配对完成,应用程序会在列表视图中显示配对的设备。顺便说一下,这不是我需要的,因为当我按下“查找设备”按钮时,什么都没有发生,看起来我遗漏了什么