简体   繁体   English

Android蓝牙设备选择器用法

[英]Android Bluetooth Device Picker Usage

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" 用Intent “android.bluetooth.devicepicker.action.LAUNCH”启动一个Activity

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"* * “android.bluetooth.devicepicker.extra.LAUNCH_PACKAGE” *

should have the name of my package, so I passed that only. 应该有我的包的名称,所以我只通过了它。 That is "com.extreme.controlcenter" 那就是“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. 在这里,我尝试将具有onReceive()函数的类的名称。

But the problem is that the onReceive() function is NOT getting called when a device is picked in device picker! 但问题是当在设备选择器中拾取设备时,不会调用onReceive()函数!

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 我创建了一个Intent过滤器,并在主Activity的onCreate()中注册了一个接收

 // 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. 但是这个代码只能在我的TAB上运行,但同样会在手机上崩溃。 So I think providing those two extras are mandatory. 所以我认为提供这两个额外内容是强制性的。

Thanks in Advance ! 提前致谢 !

"com.extreme.controlcenter.WelcomeActivity" in your EXTRAs needs to be a BroadcastReceiver class such as MyBroadcastReceiver.class.getName(). 您的EXTRA中的“com.extreme.controlcenter.WelcomeActivity”需要是一个BroadcastReceiver类,例如MyBroadcastReceiver.class.getName()。 I also have it declared in the manifest inside the tags 我也在标签内的清单中声明了它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM