5

My Application required bluetooth connectivity. And in the first phase I am trying to open up the standard Activity "Bluetooth Device Picker" to help user scan for new device or chose a device from the list.

The problem is that I am unable to get any working example for the bluetooth device picker. The task is simple. To start an Activity with Intent "android.bluetooth.devicepicker.action.LAUNCH"

And the device picker is opening without any problem.

But the device picker requires four extras and I am unable to figure out the exact parameters for two of the extras listed below.

.putExtra("android.bluetooth.devicepicker.extra.LAUNCH_PACKAGE","com.extreme.controlcenter"
.putExtra("android.bluetooth.devicepicker.extra.DEVICE_PICKER_LAUNCH_CLASS","com.extreme.controlcenter.WelcomeActivity")

What I thought the parameters should be that

*"android.bluetooth.devicepicker.extra.LAUNCH_PACKAGE"*

should have the name of my package, so I passed that only. That is "com.extreme.controlcenter"

The second should be the name of the component that must receive the broadcast that is done after a device is selected. Here I tried putting the name of the class that has the onReceive() function.

But the problem is that the onReceive() function is NOT getting called when a device is picked in device picker!

public void onReceive(Context context, Intent intent) {
 String action = intent.getAction();
 //Device Selected on Device Picker
 if("android.bluetooth.devicepicker.action.DEVICE_SELECTED".equals(action)) {
 //context.unregisterReceiver(this);
 BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
 Toast.makeText(context, "device" + device.getAddress(), Toast.LENGTH_SHORT).show();
 String MAC = device.getAddress();
 //Log.d("my", MAC);
 Intent intent2 = new Intent(WelcomeActivity.this, ControlActivity.class);
 intent2.putExtra(EXTRA_DEVICE_ADDRESS, MAC);
 startActivity(intent2);
 }
 };

I have created an Intent filter and registered a receive in the onCreate() of the main Activity

 // Register the BroadcastReceiver
 IntentFilter filter = new IntentFilter("android.bluetooth.devicepicker.action.DEVICE_SELECTED");
 registerReceiver(mReceiver, filter); 

One thing is that if I don't provide those two extras, the Broadcast event is received successfully. But that code only runs on my TAB, but same is crashing in cell phone. So I think providing those two extras are mandatory.

Thanks in Advance !

asked Oct 15, 2012 at 11:44

2 Answers 2

1

"com.extreme.controlcenter.WelcomeActivity" in your EXTRAs needs to be a BroadcastReceiver class such as MyBroadcastReceiver.class.getName(). I also have it declared in the manifest inside the tags

answered Oct 30, 2012 at 1:10
Sign up to request clarification or add additional context in comments.

Comments

0

Tested on android 12, pure kotlin code, no manifiest configuration needed.

private const val ACTION_BLUETOOTH_SELECTED =
 "android.bluetooth.devicepicker.action.DEVICE_SELECTED"
fun showBluetoothSearch(activity: Activity) {
 activity.registerReceiver(bluetoothReceiver, IntentFilter(ACTION_BLUETOOTH_SELECTED))
 val bluetoothPicker = Intent("android.bluetooth.devicepicker.action.LAUNCH")
 activity.startActivity(bluetoothPicker)
}
private var bluetoothReceiver: BroadcastReceiver = broadcastReceiver { _, intent ->
 if (ACTION_BLUETOOTH_SELECTED == intent.action) {
 val device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) as BluetoothDevice?
 toast(device?.address)
 }
}

It's strange that there is no official document for the system device picker use case, I combined the code from several sources. Hope it helpful.

answered Apr 7, 2023 at 5:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.