views:

289

answers:

1

Hi guys,

I want to write an App that monitors my paired bluetooth connection in the following way:

If a file comes from a paired source it should be stored. If no file was passed and the bluetooth connection breaks down, my app shall store a dummy file.

Storing a file works great, my main issue is how to run this whole thing without having an activity on my display....

I read much about services, but mostly it is said that a service is depending on an activity/app...is that right?

Is there any other possibility to realize something like that? What about broadcast receivers? How can I programm this functionality?

I'm looking forward to read your (creative) answers ;-) nice greetings, poeschlorn

+1  A: 

As you guessed, you could do this with a BroadcastReciever and a Service. You would setup your broadcast reciever to handle the "bluetooth disconnect" event, and then fire off the service to do something about it.

In the manifest, declare your reciever:

<receiver andriod:name=".YourReciever">
    <intent-filter>
        <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED"/>
    </intent-filter>
</receiver>

In your BroadcastReciever, you would do something like this:

@Override
public void onRecieve(Context context, Intent intent) {

    if (intent.getAction().equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
        context.startService(new Intent(context, YourService.class));
    }
}

And your Service would handle creating the dummy file:

@Override
public void onCreate() {
    // Create the dummy file, etc...
}

You'll also want to do things like check the device that's being disconnected, etc, but this should get you started. Also, I've never used the Bluetooth stack, but I think that's the relevant action name.

Erich Douglass
hi, thanks for the answer...is there maybe an similar intent-filter for any incoming file transfer via bluetooth?
poeschlorn
It looks like all the Bluetooth related broadcast intents are all very low level (connect, disconnect, discovery, etc). For reference, they're all listed in the javadoc for BluetoothDevice.
Erich Douglass