我正在使用Android应用程序从硬件蓝牙设备接收蓝牙数据。我已经看到了堆栈的所有帖子,还使用了示例应用程序来设计我的代码。 问题是,我的“蓝牙接收器”无法正常工作,每当我从硬件设备发送数据时,它都是第一次工作,但恰好第二次它总是无法接收数据通知。
public class BluetoothDataReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConfigClass.bluetoothDataReceive++;
if (ConfigClass.bluetoothDataReceive == 1) {
ConfigClass.showToast(context,
ConfigClass.MSG_RECEIVE_BLUETOOTH_DATA);
}else if (ConfigClass.bluetoothDataReceive ==2) {
ConfigClass.bluetoothDataReceive = 0;
}
}
}
请帮助我....我很长时间都在努力解决这个问题。
答案 0 :(得分:1)
BroadcastReceiver.onReceive()在与UI不同的线程中运行。 UI不是线程安全的。在其中做Toast()是一个坏主意,据说会导致意想不到的结果......