- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试通过实现我自己的 Android 蓝牙库来进行蓝牙聊天。我希望图形部分和逻辑部分之间有明确的分离(为了模块化)。
为了获取蓝牙设备列表,我以编程方式在我的 BluetoothManager 类(库)中注册了一个新的 BroadcastReceiver。
我想返回这些值或将这些值存储在此类中的数组中,以便我可以从我的(外部) Activity/fragment 访问它们。
我应该怎么做?
这是来自BluetoothManager(库)的代码:
public class BluetoothManager {
private BluetoothAdapter bluetoothAdapter;
private Context context;
public BluetoothManager(Context context) {
this.context = context;
this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Log.d("[BluetoothManager]", "Error device does not support Bluetooth");
}
}
// Create a BroadcastReceiver for ACTION_FOUND.
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object and its info from the Intent.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add value into an array or return --> TODO
}
}
};
public void discovery(){
// Check if the device is already "discovering". If it is, then cancel discovery.
if (bluetoothAdapter.isDiscovering()) {
bluetoothAdapter.cancelDiscovery();
}
// Start Discovery
bluetoothAdapter.startDiscovery();
// Register for broadcasts when a device is discovered.
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
context.getApplicationContext().registerReceiver(mReceiver, filter);
}
...
}
这是 fragment 中的代码:
public class TabFragment extends ListFragment implements AdapterView.OnItemClickListener {
private BluetoothManager bluetoothManager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.fragment_tab_fragment2, container, false);
Button button = fragmentView.findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
bluetoothManager = new BluetoothManager(getContext());
if(bluetoothManager.activeBluetooth()){
bluetoothManager.discovery();
// Get Values here --> TODO
}
}
});
// Add return values into a list
//final String[] items = ...;
final ArrayAdapter<String> aa = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, items);
setListAdapter(aa);
return fragmentView;
}
}
最佳答案
发现可用蓝牙设备列表是异步的。接收到广播后,BluetoothManager 类可以将更新的设备列表保存在列表成员变量中,并公开公共(public) getter 方法以从 Activity 或 fragment 中检索它。另外,请考虑向您的 Activity 公开监听器以提供更新的列表,因为设备可以不断动态添加或删除。在任何时间点,UI 都应该在收到更新的设备列表后自行刷新。
public void onClick(View v) {
bluetoothManager = new BluetoothManager(getContext()); ...}
是个坏主意。理想情况下,您必须在 Activity 或 fragment 的 onCreate 中实例化您的 BluetoothManager 类,以便在用户单击按钮之前有足够的时间发现设备列表。
我的建议是:
或者,如果您想与多个 Activity 共享蓝牙管理器实例,请将BluetoothManager 设置为单例并向应用程序上下文注册BroadcastReceiver。当您的所有 Activity 都被销毁并且您不再需要接收器时,请确保取消注册广播接收器。
更新:回调可能只是您的 Activity 或 fragment 实现的接口(interface)。示例代码如下:
public interface BluetoothDevicesAvailable {
void onBluetoothDeviceListChanged(List<BluetoothDevice> deviceList);
}
public class SomeActivity implements BluetoothDevicesAvailable {
//... Some code
@Override
public void onBluetoothDeviceListChanged(List<BluetoothDevice> deviceList) {
//Add your logic here to update UI
}
}
关于java - 从 BroadcastReceiver(蓝牙)返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46954426/
我的应用程序无法在 Android 7 上运行。调用了我的 BroadcastReceiver.onReceive 方法,但缺少 intent.getExtras 的内容。我已验证数据已正确加载。这是
我认为即使我退出应用程序,BroadcastReceiver AlarmManagerBroadcastReceiver 也会继续运行。我认为 BroadcastReceiver 会在我重新启动系统时
我为 Wi-Fi 创建了一个 brodcastReceiver,一切正常,除了,当我午餐应用程序时,broadcastreceiver 开始直接扫描,我无法停止它,即使我离开应用。我试图从主要 Act
我最近开始在我的 Android 应用程序上使用 Google Analytics(分析)。我想知道如何在 BroadcastReceiver 的 onReceive 方法中将事件发送到 GA。这是一
我想在 Broadcast Receiver 中检查互联网连接;并将结果( bool 标志)设置为全局变量,以便在 if 条件下将其用于整个应用程序;如果互联网断开连接,则在主要 Activity 中
如果用户向上或向下按下音量键,是否可以在我的广播接收器中检测到它?我需要完整的代码。 这是我的 Intent 过滤器 IntentFilter filter = new IntentFilter();
我正在尝试编写一个应用程序,其中我根据使用 gcm 推送通知发送的消息对 UI 进行更改,并且我设法通过使用 BroadcastReceiver onReceive 函数来实现它来实现它,但它仅在应用
我正在尝试使用 AlarmManager 安排通知当我安排一个通知时它工作得很好,但是当我安排两个通知时,第一个通知没问题,但第二个通知不起作用。 我发现几分钟后打开应用程序会通知第二个通知。我认为我
我正在阅读指南 here关于设置 BroadcastReceiver 来检查电池变化。它指出我可以像这样设置一个 BroadcastReceiver:
我想在应用程序中接收有关启用/禁用位置提供程序的信息,例如NETWORK、GPS等。我创建了简单的BroadcastReceiver: public class TestReciver extends
我正在使用广播接收器来设置警报警报,但由于某种原因它无法工作。 public class SimpleSleepActivity extends Activity { /** Called when
当我的应用程序处于后台时,我尝试播放一些音调,并按下“相机”按钮,因为我正在执行此简单的步骤。 创建BroadcastReceiver类 public class CameraButtonListen
我创建了我的广播接收器 AnprEventReciever,它应该在连接状态发生变化时被触发,但它并没有。 事件接收者: import android.content.BroadcastReceive
我有一个时间选择器来设置闹钟 public class TimePickerFragment : DialogFragment, TimePickerDialog.IOnTimeSetListener
我目前正在开发一个使用 broadcastreceiver 来检查收到的短信的应用程序,但突然间它似乎停止工作了,我什至写了一个小的测试应用程序,以至少对我来说,语法上似乎是正确的,但也不起作用。 这
我正在尝试使用 BroadcastReceiver 获取互联网连接状态。 接收类是这样的: public class ConnectivityReceiver extends BroadcastRec
我有一个小部件,它只使用服务来更新小部件的 RemoteView 的外观。该服务基于由不同注册的 BroadcastReceiver 提供的过滤器启动。当满足其中一个条件时,将调用我的 Broadca
我没有时间玩接收器,而且我对2.3.x以上的东西都不熟悉,所以当我读到这个问题时,我感到非常困惑: Can BroadcastReceiver registered in AndroidManifes
我有一个录音机 android 线程,我需要知道录音时麦克风/耳机是否已连接,所以我需要在线程内使用 BroadcastReceiver()。我如何注册它? this.registerReceiver
大家好,感谢您的帮助!我将从代码开始 所以,带有广播接收器的类是这样的: public class MyService extends Service { // ... // ACTI
我是一名优秀的程序员,十分优秀!