0



I'm writing an application which contains 2 fragments wrapped by a viewpager. I'm trying to get the list of the bluetooth available devices but the listview never changes.
This is the first time I work with fragments. I hope you guys can help me. Thanks in advance!

Here's the fragment:

public class BluetoothDevicesFragment extends Fragment {
    private ListView mListView;
    private BluetoothListAdapter mAdapter;
    private ArrayList<String> mArray;
    private SwipeRefreshLayout mRefresh;
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            //Finding devices
            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 show in a ListView
                //Snackbar.make(getView(), device.getName(), Snackbar.LENGTH_LONG).setAction("Action", null).show();
                mArray.add(device.getName() + "\n" + device.getAddress());
                mAdapter.notifyDataSetChanged();
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                mRefresh.setRefreshing(false);
                getActivity().unregisterReceiver(mReceiver);
            }
        }
    };

    public BluetoothDevicesFragment() {

    }

    public static BluetoothDevicesFragment newInstance() {

        Bundle args = new Bundle();

        BluetoothDevicesFragment fragment = new BluetoothDevicesFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_bluetooth, container, false);
        mListView = (ListView) rootView.findViewById(R.id.listviewB);
        mArray = new ArrayList<>();
        mAdapter = new BluetoothListAdapter(getContext(), R.layout.fragment_bluetooth, mArray);
        mListView.setAdapter(mAdapter);
        mRefresh = (SwipeRefreshLayout) rootView.findViewById(R.id.refresh);
        mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
                getActivity().registerReceiver(mReceiver, filter);
                Bluetooth.bluetoothAdapter.cancelDiscovery();
                Bluetooth.bluetoothAdapter.startDiscovery();
            }
        });
        return rootView;
    }
}
Luca
  • 67
  • 1
  • 10

1 Answers1

1

You have to register BroadcastReceiver to receive events

There are two ways to do this:

Registering BroadcastReceiver in the Manifest File

<receiver
android:name="com.pycitup.pyc.MyReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
    <action android:name="com.pycitup.BroadcastReceiver" />
</intent-filter>

Registering BroadcastReceiver Programatically

IntentFilter filter = new IntentFilter("packagename.customclass");

MyReceiver myReceiver = new MyReceiver();
registerReceiver(myReceiver, filter);

Add permission in manifest.xml

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

Don't forget to check runtime permission for abaove

Kishore Jethava
  • 6,666
  • 5
  • 35
  • 51
  • I've done it programmatically following the Google's guidelines but it still doesn't work – Luca Feb 19 '17 at 19:33
  • I mean I still don't see what it is missing – Luca Feb 19 '17 at 20:01
  • Did you tried [this](http://stackoverflow.com/questions/32656510/register-broadcast-receiver-dynamically-does-not-work-bluetoothdevice-action-f) – Kishore Jethava Feb 20 '17 at 04:46
  • thanks now it works. I didn't know I had to add that location permission. Thanks a lot. – Luca Feb 20 '17 at 05:49
  • Happy to help you! – Kishore Jethava Feb 20 '17 at 05:59
  • another thing, now I have another problem, I have implemented a swipe refresh layout that updates the list according to the receiver. It should stop refreshing when the scan is ended but it never stops. Do you know why? – Luca Feb 20 '17 at 06:02
  • you should ask new que. anyway add `IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); getActivity().registerReceiver(mReceiver, filter);` below `mAdapter.notifyDataSetChanged();` – Kishore Jethava Feb 20 '17 at 06:12