Android: Shutting down / losing a Bluetooth connection or receiving files & # 8594; Do something

I want to write an application that tracks my paired bluetooth connection as follows:

If the file comes from a paired source, it must be saved. If the file was not transferred and the Bluetooth connection is disconnected, my application should save the dummy file.

Saving the file works fine, my main problem is how to run all this without having activity on my display ....

I read a lot about services, but they mostly say that the service is activity / application dependent ... is that right?

Is there any other way to implement something like this? What about broadcast receivers? How can I program this functionality?

I look forward to your (creative) answers ;-) nice hello, poeschlorn

+3
source share
1 answer

As you may have guessed, you can do this with BroadcastReceiverand Service. You must configure the broadcast receiver to handle the “bluetooth disconnect” event, and then disconnect the service in order to do something.

In the manifest, declare the receiver:

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

In yours BroadcastReceiveryou would do something like this:

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

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

And yours Servicewill handle the creation of a dummy file:

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

- .., . , Bluetooth, , .

+4

Source: https://habr.com/ru/post/1747026/


All Articles