获取Android中所有可用蓝牙设备的地址和名称 [英] Getting the address and names of all available bluetooth devices in android

查看:1394
本文介绍了获取Android中所有可用蓝牙设备的地址和名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够检索配对的设备名称和地址,但是我不知道如何检索可用的蓝牙设备名称和地址?

I am able to retrieve the paired device names and address, but I don't know how I can retrieve the available bluetooth devices name and address?

   String outV="";
   b1=(Button)findViewById(R.id.button1);
    b2=(Button)findViewById(R.id.button2);
    tv=(TextView)findViewById(R.id.textView1);
    b1.setOnClickListener(new View.OnClickListener() {

        BluetoothAdapter Badap = BluetoothAdapter.getDefaultAdapter();



        @Override
        public void onClick(View arg0) {




            Set<BluetoothDevice> pairedDevices= Badap.getBondedDevices();

            if (pairedDevices.size() > 0) {

                for (BluetoothDevice device : pairedDevices) {

                    outV+= device.getName() + " ::" + device.getAddress()+"\n";


                }
                tv.setText(outV);







            }

这是配对设备的代码.

推荐答案

在我正在执行的OnCreate中

In my OnCreate I'm doing

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(MydeviceReceiver, filter);  

MydeviceReceiver在哪里

Where MydeviceReceiver is

    private final BroadcastReceiver deviceReceiver = new BroadcastReceiver() 
        {
            @Override
            public void onReceive(Context context, Intent intent) 
            {
                String action = intent.getAction();
            // When discovery finds a device
            if (BluetoothDevice.ACTION_FOUND.equals(action)) 
            {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                if( !PairedDeviceNames.contains(device) && !newDevices.contains(device))
                    newDevices.add(device);                
            }
            else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) 
            {
                Log.v(" ","discovery Finished ");
                if(newDevices.size() != 0)
                {                   
                    deviceList.invalidateViews();
                    sectionAdapter.notifyDataSetChanged();
                }
                else
                {
                    Toast.makeText(YourActivity.this, "No New Devices Found", Toast.LENGTH_LONG).show();
                }
            }
        }
    };

这篇关于获取Android中所有可用蓝牙设备的地址和名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆