Android蓝牙设备选择器的使用 [英] Android Bluetooth Device Picker Usage

查看:366
本文介绍了Android蓝牙设备选择器的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序需要蓝牙连接.在第一阶段中,我尝试打开标准的活动蓝牙设备选择器" ,以帮助用户扫描新设备或从列表中选择设备.

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.

问题是我无法获得蓝牙设备选择器的任何有效示例.任务很简单.要使用Intent "android.bluetooth.devicepicker.action.LAUNCH"

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")

我认为参数应该是

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

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

应该有我的包裹的名字,所以我只通过了.那是"com.extreme.controlcenter"

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

第二个应该是必须接收选择设备后完成的广播的组件的名称.在这里,我尝试放置具有 onReceive()函数的类的名称.

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!

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);
            }


        };

我创建了一个 Intent过滤器,并在主Activity的onCreate()中注册了一个接收记录

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); 

一件事是,如果我不提供这两个额外功能,则会成功接收 Broadcast 事件.但是该代码仅在我的TAB上运行,但是在手机中也崩溃了.因此,我认为必须提供这两个额外功能.

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.

预先感谢!

推荐答案

"com.extreme.controlcenter.WelcomeActivity"必须是诸如MyBroadcastReceiver.class.getName()之类的BroadcastReceiver类.我也在标签内的清单中声明了它

"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

这篇关于Android蓝牙设备选择器的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆