gpt4 book ai didi

java - 蓝牙启用会扰乱广播接收器

转载 作者:行者123 更新时间:2023-12-02 04:57:47 24 4
gpt4 key购买 nike

我的应用程序旨在将所有扫描的蓝牙设备附加到textview。如果手机蓝牙已打开,此功能非常有用。但是,如果我的应用程序检查手机蓝牙是否关闭,如果关闭则将其打开,然后启动发现过程,我的 broadcastreciever 不会拾取事件,因此我的 textview 未填充。如有任何帮助,我们将不胜感激!

这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blue_tooth_main);

txtResults = (TextView) this.findViewById(R.id.txtResults);
mBlueToothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!(mBlueToothAdapter.isEnabled())) {
mBlueToothAdapter.enable();

}

mBlueToothAdapter.startDiscovery();

我的接收器:

public static class BlueToothBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

blueToothName = device.getName();
blueToothAddress = device.getAddress();
blueToothClass = device.getBluetoothClass();
blueToothBondState = device.getBondState();
GetBondStateStr(blueToothBondState);
blueToothUUIDS = device.getUuids();

paramsBlueTooth
.add(new BasicNameValuePair("Name: ", blueToothName));
paramsBlueTooth.add(new BasicNameValuePair("Address: ",
blueToothAddress));
paramsBlueTooth.add(new BasicNameValuePair("Class: ", String
.valueOf(blueToothClass)));
paramsBlueTooth.add(new BasicNameValuePair("Bond State: ",
blueToothBondStateStr));
paramsBlueTooth.add(new BasicNameValuePair("UUIDS: ", String
.valueOf(blueToothUUIDS)));

showBlueToothData();

}

显示蓝牙数据():

    private void showBlueToothData() {
StringBuilder results = new StringBuilder();

results.append("-----BLUETOOTH DEVICE INFORMATION-----\n");
results.append("Name: " + blueToothName + "\n");
results.append("Address: " + blueToothAddress + "\n");
results.append("Class: " + blueToothClass + "\n");
results.append("Bond State: " + blueToothBondStateStr + "\n");
results.append("UUIDS: " + blueToothUUIDS + "\n");

txtResults.append(new String(results));
txtResults.append("\n");
}

最佳答案

您选择的通过 BluetoothAdapter.enable() 方法启用蓝牙 radio 的方法是异步调用。从 method's documentation 可以看出,您不能假设该方法一返回 radio 就已启动并处于 Activity 状态。您必须等待 ACTION_STATE_CHANGED 广播才能知道 radio 已准备好供您尝试扫描。

如果您阅读文档,还请注意,这样做的体验很差,因为没有向用户发出通知。更好的选择是将用户发送到“设置”以自行启用蓝牙。您可以将启动逻辑修改为更像这样的内容:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blue_tooth_main);

txtResults = (TextView) this.findViewById(R.id.txtResults);
mBlueToothAdapter = BluetoothAdapter.getDefaultAdapter();


}

@Override
protected void onResume() {
super.onResume();
if (!(mBlueToothAdapter.isEnabled())) {
//Take the user to settings first!
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(intent);
} else {
mBlueToothAdapter.startDiscovery();
}
}

此修改将在您每次来到前台时检查蓝牙,并且仅在您准备好时才触发扫描。

关于java - 蓝牙启用会扰乱广播接收器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28599158/

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