Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing Bluetooth device to a service, null getParcelableExtra
    primarykey
    data
    text
    <p>I am trying to maintain bluetooth connection when the screen is off. So I created an app that you can discover bluetooth devices around and choose from list. Then pass the chosen bluetooth device to backgroundService class which will handle the rest.But I am getting null pointer when I try to get device from intent. Any help will be appreciated.</p> <p>Here is my code segments</p> <pre><code> @Override public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) { if(btAdapter.isDiscovering()){ btAdapter.cancelDiscovery(); } if(listAdapter.getItem(arg2).contains("Paired")){ BluetoothDevice selectedDevice = devices.get(arg2); Intent intent = new Intent(this,backgroundService.class); if(selectedDevice == null) Log.i(backgroundService.EXTRA_MESSAGE, "I am telling you it is null"); intent.putExtra(backgroundService.EXTRA_MESSAGE, (Parcelable)selectedDevice); startService(intent); } else{ Toast.makeText(getApplicationContext(), "device is not paired", 0).show(); } } </code></pre> <p>I also populate devices as follows</p> <pre><code> receiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(action.equals(BluetoothDevice.ACTION_FOUND)){ Log.i("onReceive", "ACTION_FOUND"); BluetoothDevice d = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); String s = "Not Paired"; devices.add(d); for(int a = 0; a &lt; pairedDevices.size(); a++){ if(d.getName().equals(pairedDevices.get(a).getName())){ //append s = "Paired"; break; } } listAdapter.add(d.getName()+"("+s+")\n"+ d.getAddress()); } </code></pre> <p>So devices list must already have parcelable devices. I am getting null device from following code</p> <pre><code>public int onStartCommand(Intent intent, int flags, int startId) { Log.i(EXTRA_MESSAGE,"onStartCommand"); BluetoothDevice selectedDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if(selectedDevice == null) {Log.i(EXTRA_MESSAGE,"null it is "); return -1;} connect = new ConnectThread(selectedDevice); connect.start(); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload