2

I want to filter BluetoothDevice when discovering and only keep which are started listening by same software. For example, the Android sample BluetoothChat, it should only show those bluetooth devices which are started by BluetoothChat too.

Here is my code.

public class DeviceListActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Setup UI here
        ......

        // Register for broadcasts when a device is discovered
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        this.registerReceiver(mReceiver, filter);

        // Register for broadcasts when discovery has finished
        filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        this.registerReceiver(mReceiver, filter);

        // Get the local Bluetooth adapter
        mBtAdapter = BluetoothAdapter.getDefaultAdapter();

        // Request discover from BluetoothAdapter
        mBtAdapter.startDiscovery();
    }

    // The BroadcastReceiver that listens for discovered devices and
    // changes the title when discovery is finished
    private final BroadcastReceiver mReceiver = 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);

                // *** My problem is here. I got a BluetoothDevice now, but how can I
                // *** check whether it is created by my software?
                // *** if I can't, I have to add all of them to my list, and when user
                // *** chooses it, connection will fail if it's not created by my software.
                mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            }
            // When discovery is finished, change the Activity title
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                // Discovery is finished...
                // wrap up code here...
                ......
            }
        }
    };
}
user3068231
  • 147
  • 1
  • 8

1 Answers1

1

You can do one thing :

When you start bluetooth by using your software, change the name of your bluetooth to a particular pattern so that you can identify the device is started by your software.

Then while searching for bluetooth device, check whether the bluetooth device name that you came accross while searching is of the pattern that you defined.

To change device name :

Change the Android bluetooth device name

Community
  • 1
  • 1
Eldhose M Babu
  • 14,382
  • 8
  • 39
  • 44