我的应用程序需要蓝牙连接。在第一阶段我试图开放标准 Activity“蓝牙设备选择器”帮助用户扫描新设备或从列表中选择设备。
问题是我无法获得蓝牙设备选择器的任何工作示例。任务很简单。有意图地启动活动“android.bluetooth.devicepicker.action.LAUNCH”
并且设备选择器打开没有任何问题。
但设备选择器需要四个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);
}
};
我创建了一个意图过滤器并在主 Activity 的 onCreate() 中注册接收
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter("android.bluetooth.devicepicker.action.DEVICE_SELECTED");
registerReceiver(mReceiver, filter);
一件事是,如果我不提供这两个额外的东西,播送事件接收成功。但该代码仅在我的 TAB 上运行,但同样在手机中崩溃。所以我认为提供这两个额外功能是强制性的。
提前致谢 !