Search code examples
androidbluetoothbroadcastreceiveractionbarsherlockfragment

Android, using bluetooth inside sherlockFragment


I have a Fragment Tabs Pager and used sherlockFragment and I need to search for other bluetooth devices inside it. but I cannot registerReceiver as it's needed. I get the following error: The method registerReceiver(BroadcastReceiver, IntentFilter) is undefined for the type Fragment_Bluetooth. I am wondering if I should use registerReceiver in another scope? and where and how? here is my code:

public class Fragment_Bluetooth extends SherlockFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Create a BroadcastReceiver for ACTION_FOUND
        BroadcastReceiver mReceiver = new BroadcastReceiver() {
            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);
                    // Add the name and address to an array adapter to ListView
                    mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                }
            }
        };
        // Register the BroadcastReceiver
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
    }
}

Solution

  •  getActivity().registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
    

    Or getSherlockActivity()

     getSherlockActivity().registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy