Reputation: 2979
i develop an Android-Bluetooth App with 3-4 Activitys. Now i have to receive bluetooth data in any of these activitys. I think i have to implement a Service which contains a BroadcastReceiver which listens to incoming BlueTooth Data and send a Broadcast, but i don't know how to do that.
Thanks in advance.
Upvotes: 0
Views: 1413
Reputation: 1737
You can implement your own BroadcastReceiver. So, when your LocalService receive a data, it will notify using sendBroadcast method. Your activities should register the specific BrodcastReceiver.
In your Service
Notify about received messages:
public void onMessageReceived(String message) {
Intent intent = new Intent(ACTION_BLUETOOTH_MESSAGE);
intent.putExtra(BLUETOOTH_MESSAGE_CONTENT, message);
sendBroadcast(intent);
}
On each activity
Registering the broadcast receiver:
registerReceiver(messageReceiver,
new IntentFilter(ACTION_BLUETOOTH_MESSAGE));
Implementation of the broadcast receiver:
private BroadcastReceiver messageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra(BLUETOOTH_MESSAGE_CONTENT);
//Do something you want
}
};
Upvotes: 2