Skip to content Skip to sidebar Skip to footer

To Discover And Pair Bluetooth Devices

How to discover and pair Android Bluetooth devices using Java? Any codes for me to refer to?

Solution 1:

the following code will discover the list of paired and the unpaired devices after that u have to implement the Client and server, which takes care of pairing the devices and sending data to the devices, for tat u can make use of the BluetoothChatSample which will give an idea to u.

private Set<BluetoothDevice> pairedDevices;
publicstatic ArrayList<Object> BondedDeviceList;
publicstatic ArrayList<Object> NewDeviceList;

 publicvoidmakeDiscoverable()
{
    discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
    activity.startActivity(discoverableIntent);
}

//It will Add the paired device in the BondedDeviceListpublicvoidqueryPairedDevice(){
    pairedDevices = mBluetoothAdapter.getBondedDevices();

    // If there are paired devicesif(pairedDevices==null)
    {
        //No Bonded Devices 

    }else
    {
        if (pairedDevices.size() > 0) {
            // Loop through paired devicesfor (BluetoothDevice device : pairedDevices) {
                BondedDeviceList.add(device);
            }
            BondedDeviceList.add("End");
        }
    }
}

//Broadcast Receiver will find the Available devices and the discovery finishedprivate final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    publicvoidonReceive(Context context, Intent intent) {
        String action = intent.getAction();

        // When discovery finds a deviceif (BluetoothDevice.ACTION_FOUND.equals(action.trim())) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // If it's already paired, skip it, because it's been listed alreadyif (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                NewDeviceList.add(device);
            }
            // When discovery is finished, change the Activity title
        } elseif (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            if (NewDeviceList.isEmpty() == true) {
                String noDevices = "No Devices";
                NewDeviceList.add(noDevices);
            }
            System.out.println("Discovery Finished!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            NewDeviceList.add("End");
        }
    }
};

//This is query for the bluetooth devices publicvoidqueryDevices(){
    actionFoundFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    activity.registerReceiver(mReceiver, actionFoundFilter);
    // Don't forget to unregister during onDestroy

    discoveryFinishedFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    activity.registerReceiver(mReceiver, discoveryFinishedFilter); 
    // Don't forget to unregister during onDestroy
    queryPairedDevice();
    mBluetoothAdapter.startDiscovery();
}


//Unregister the receiverspublicvoidunregisterReceiver() {
    // Make sure we're not doing discovery anymoreif (mBluetoothAdapter != null) {
            mBluetoothAdapter.cancelDiscovery();
    }
    // Unregister broadcast listeners
    activity.unregisterReceiver(mReceiver);
}

Cheers.

Post a Comment for "To Discover And Pair Bluetooth Devices"