gpt4 book ai didi

java - 从 BroadcastReceiver(蓝牙)返回值

转载 作者:行者123 更新时间:2023-12-02 02:31:54 25 4
gpt4 key购买 nike

我正在尝试通过实现我自己的 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 类,以便在用户单击按钮之前有足够的时间发现设备列表。

我的建议是:

  1. 在Activity的onCreate中实例化BluetoothManager,更改构造函数以接受回调对象,以便在设备列表发生变化时进行通知。
  2. 监听BluetoothManager中的广播以识别设备的添加或删除。更新本地列表,每当更新发生时,通知 UI 的回调
  3. 在 Activity/fragment 中实现回调,并在收到来自 BluetoothManager 类的更新时更新 UI
  4. 当用户点击按钮时,调用蓝牙管理器来获取更新的设备列表并更新您的 UI。
  5. 通过 Activity 的 onDestroy 取消注册在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com