Belboz
Belboz

Reputation: 107

Finding Android Bluetooth Paired Devices

I'm trying to create an image button that, when pressed, presents the users a list of Paired Bluetooth devices to connect to.

However, I get "Set cannot be resolved as a variable" at point ##1, and "mArrayAdapber cannot be resolved" at point ##2 (##1 and ##2 are not part of the code...)

I used the code from the Android site, but being in the dark, I find myself in the dark.

I'd appreciate some guidance...

//Search

ImageButton bSearch = (ImageButton) findViewById(R.id.Search);
bSearch.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {                        
        ##1Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        // If there are paired devices
        if (pairedDevices.size() > 0) {
            // Loop through paired devices
            for (BluetoothDevice device : pairedDevices) {
                // Add the name and address to an array adapter to show in a ListView
                ##2mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            }
        }                                                                           
    }                       
});

Upvotes: 4

Views: 21819

Answers (3)

Maverick
Maverick

Reputation: 3053

Hi, You can also try this code where you just have get the Set of bonded devices.

 private ArrayAdapter<String> bondedAdapter = null;
    private ListView listViewPairedDevices = null;

@Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        try {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    listViewPairedDevices = (ListView) findViewById(R.id.listViewPairedDevices);
    bondedAdapter = new ArrayAdapter<String>(this, R.layout.lyt_scanlist_textview);

    Set<BluetoothDevice> bondedSet = bluetoothAdapter.getBondedDevices();
                Log.v(BluetoothDemoActivity.LOG, "BluetoothDemo : bondedSet: "+bondedSet);

                int count = 0;
                if(bondedSet.size() > 0){
                    for(BluetoothDevice device : bondedSet){
                        textViewPairedDevice.setVisibility(View.VISIBLE);
                        bondedAdapter.add(device.getName()+"\n"+device.getAddress());
                        Log.v(BluetoothDemoActivity.LOG, " count = "+count++);
                    }
                }else{
                    bondedAdapter.add("No Devices");
                }

    listViewPairedDevices.setAdapter(bondedAdapter);
} catch (Exception e) {
            // TODO Auto-generated catch block
            Log.e(BluetoothDemoActivity.LOG, e.toString(),e.fillInStackTrace());
        }
    }//onStart ends 

Upvotes: 3

Muhammad Shahab
Muhammad Shahab

Reputation: 4270

For 1) Well if you haven't done so , add

> import java.util.Set;

in your import statements . This will resolve "Set" error.

For 2) Declare and initialize

mArrayAdapter

For example in your Activity do :

private ArrayAdapter<String> mArrayAdapter;

and then on onCreate:

 mArrayAdapter= new ArrayAdapter<String>(this, <your layout file>);

which should then be added to a ListView

// Find and set up the ListView for newly discovered devices

   ListView newDevicesListView = (ListView)
 findViewById(R.id.<layout_file>);
         newDevicesListView.setAdapter(mArrayAdapter);

 newDevicesListView.setOnItemClickListener(mDeviceClickListener);

Refer to Bluetooth Chat example from Android examples. It should help you get going with the Bluetooth api's


Update on comment :

If you look closely on BluetoothChat.java file in BT example, you'll see this

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(D) Log.d(TAG, "onActivityResult " + resultCode);
        switch (requestCode) {
        case REQUEST_CONNECT_DEVICE:
            // When DeviceListActivity returns with a device to connect
            if (resultCode == Activity.RESULT_OK) {
                // Get the device MAC address
                String address = data.getExtras()
                                     .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
                // Get the BLuetoothDevice object
                BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
                // Attempt to connect to the device
                mChatService.connect(device);
            }
            break;
        case REQUEST_ENABLE_BT:
            // When the request to enable Bluetooth returns
            if (resultCode == Activity.RESULT_OK) {
                // Bluetooth is now enabled, so set up a chat session
                setupChat();
            } else {
                // User did not enable Bluetooth or an error occured
                Log.d(TAG, "BT not enabled");
                Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }

Watch this line :

 // Attempt to connect to the device
 mChatService.connect(device);

This function connects to bluetooth device. First time it'll ask you to pair it automatically. Once paired, next time it'll auto connect to the bluetooth device.

Upvotes: 4

San Francesco
San Francesco

Reputation: 646

Just remove ##1 and ##2 respectively from ##1Set<BluetoothDevice> and ##2mArrayAdapter on your code. I think you'd just copy/paste from another source and did not pay attention to. That is not part of the original code. It just used for list number purposes.

Upvotes: 0

Related Questions