我的应用程序需要蓝牙连接。在第一阶段,我尝试打开标准 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);
一件事是,如果我不提供这两个附加项,Broadcast 事件将被成功接收。但该代码仅在我的 TAB 上运行,但同样在手机中崩溃。所以我认为提供这两个附加功能是强制性的。
提前致谢!
最佳答案
您的 EXTRA 中的“com.extreme.controlcenter.WelcomeActivity”需要是 BroadcastReceiver 类,例如 MyBroadcastReceiver.class.getName()。我还在标签内的 list 中声明了它
关于java - Android 蓝牙设备选择器使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12894945/