Android Bluetooth Device Picker Usage

旧城冷巷雨未停 提交于 2020-01-03 16:23:29

问题


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 !


回答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



来源:https://stackoverflow.com/questions/12894945/android-bluetooth-device-picker-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!