【问题标题】:Android Bluetooth Device Picker UsageAndroid 蓝牙设备选择器用法
【发布时间】:2012-10-05 09:17:07
【问题描述】:

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

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

启动 Activity

并且设备选择器正在打开,没有任何问题。

但是设备选择器需要四个extras,我无法确定下面列出的两个extras 的确切参数。

.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"*

应该有我的包裹的名称,所以我只通过了。那就是“com.extreme.controlcenter”

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

但问题是在设备选择器中选择设备时不会调用 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);
            }


        };

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

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


    registerReceiver(mReceiver, filter); 

有一件事是,如果我不提供这两个额外的,广播事件会成功接收。但是该代码仅在我的 TAB 上运行,但同样在手机中崩溃。所以我认为提供这两个额外的东西是强制性的。

提前致谢!

【问题讨论】:

    标签: java android bluetooth device picker


    【解决方案1】:

    EXTRA 中的“com.extreme.controlcenter.WelcomeActivity”需要是 BroadcastReceiver 类,例如 MyBroadcastReceiver.class.getName()。我也在标签内的清单中声明了它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-27
      相关资源
      最近更新 更多