Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.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_Bluetooth - Fatal编程技术网

Android 如何获取附近所有可发现蓝牙设备的列表?

Android 如何获取附近所有可发现蓝牙设备的列表?,android,bluetooth,Android,Bluetooth,我想在我的应用程序中获得附近所有可发现蓝牙设备的列表,但我没有得到任何列表。我的deviceDiscovery()方法执行得很好,但在onCreate()中的BroadcastReceiver()中没有得到任何响应。有人能告诉我为什么吗?这是我的密码: MainActivity.java public class MainActivity extends AppCompatActivity implements View.OnClickListener{ BluetoothAdapt

我想在我的应用程序中获得附近所有可发现蓝牙设备的列表,但我没有得到任何列表。我的deviceDiscovery()方法执行得很好,但在onCreate()中的BroadcastReceiver()中没有得到任何响应。有人能告诉我为什么吗?这是我的密码:

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener{


    BluetoothAdapter ba;
    ToggleButton tb;
    ArrayAdapter aa;
    ListView l;
    private static final int ENABLE_REQUEST=1;
    private static final int DISCOVERABLE_REQUEST=2;
    private static final int DISCOVERABLE_DURATION=10;


    BroadcastReceiver br=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {


            String action=intent.getAction();

            if(BluetoothDevice.ACTION_FOUND.equals(action))
            {
              BluetoothDevice bd=  intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                aa.add(bd.getName()+"  "+bd.getAddress());


            }
        }
    };






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

        tb= (ToggleButton) findViewById(R.id.toggle);
aa=new ArrayAdapter(this,android.R.layout.simple_list_item_1);
        l= (ListView) findViewById(R.id.list);
        l.setAdapter(aa);


        ba= BluetoothAdapter.getDefaultAdapter();

        tb.setOnClickListener(this);
    }




    @Override
    public void onClick(View v) {

        aa.clear();

        ToggleButton tb= (ToggleButton) v;

        if(ba==null) {
            Toast.makeText(this, "This feature is not supported", Toast.LENGTH_SHORT).show();
            tb.setChecked(false);
        }
        else
        {
            if(tb.isChecked())
            {
                if(!ba.isEnabled())
                {
                    Intent i=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(i,ENABLE_REQUEST);
                }
                else
                {
                    Toast.makeText(this, "Your device is already enabled", Toast.LENGTH_SHORT).show();
                    deviceDiscovery();
                    makeDiscoverable();
                }
            }
            else
            {
                aa.clear();
                ba.disable();
                Toast.makeText(this, "Your device is now disabled", Toast.LENGTH_SHORT).show();
            }
        }



    }








    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch(requestCode)
        {
            case ENABLE_REQUEST:if(resultCode==RESULT_OK)
            {
                Toast.makeText(this, "Bluetooth Enabled", Toast.LENGTH_SHORT).show();
                deviceDiscovery();
                makeDiscoverable();
            }
            else
            {
                Toast.makeText(this, "Bluetooth Enabling Rejected", Toast.LENGTH_SHORT).show();
                tb.setChecked(false);
            }
                break;

            case DISCOVERABLE_REQUEST:if(resultCode==DISCOVERABLE_DURATION)
            {
                Toast.makeText(this, "Your device is discoverable by other devices", Toast.LENGTH_SHORT).show();
            }
            else
            {
                Toast.makeText(this, "Your device is not discoverable", Toast.LENGTH_SHORT).show();
            }
                break;
        }
    }


    private void makeDiscoverable() {
        Intent i=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        i.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,DISCOVERABLE_DURATION);
        startActivityForResult(i,DISCOVERABLE_REQUEST);
    }



    private void deviceDiscovery()
    {
if(ba.startDiscovery())
{
    IntentFilter filter=new IntentFilter(BluetoothDevice.ACTION_FOUND);

    this.registerReceiver(br,filter);
    Toast.makeText(this, "Discovering other bluetooth devices", Toast.LENGTH_SHORT).show();
}
else
{
    Toast.makeText(this, "Discovery failed to start", Toast.LENGTH_SHORT).show();
}

    }


}
MainActivity.xml

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="On"
        android:textOff="Off"
        android:id="@+id/toggle"
        />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list"
        android:layout_below="@+id/toggle"/>

AndroidManifest.xml

<uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>


Add
filter.addAction(BluetoothAdapter.ACTION\u DISCOVERY\u完成)并运行项目。您可以阅读更多内容

您测试的是哪一版本的Android?@VladMatvienko Android 6.0版从6.0版开始,您还必须声明位置权限才能使用蓝牙。@VladMatvienko但这是正常权限,对吗?这不是一个危险的权限,所以不必在java代码中明确提到它,在清单中使用它也可以吗?我错了吗?不幸的是这是危险的。