Reputation: 2519
I am working in an Android application to receive Bluetooth data from an Hardware Bluetooth device. I have seen all the posts of stack and also used sample app to design my code. The problem is, my "Bluetooth receiver" is not working, whenever i send data from hardware device, it works for the first time but exactly 2nd time it always failed to receive the data notification.
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;
}
}
}
Please help me out....I am struggling with this problem from a long time.
Upvotes: 0
Views: 744
Reputation: 324
the BroadcastReceiver.onReceive() runs in different thread than your UI. UI is not thread safe. Doing Toast() within it is a bad idea, and supposedly leads to unexpected results...
Upvotes: 1