获取android中所有可用蓝牙设备的地址和名称

时间:2013-10-30 13:04:11

标签: android bluetooth

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

   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);







            }

这是配对设备的代码..

1 个答案:

答案 0 :(得分:6)

在我的OnCreate中,我正在做

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

MydeviceReceiver的位置

    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();
                }
            }
        }
    };