简体   繁体   中英

Android - check for loss of Bluetooth connection?

I am writing an app that connects to an Arduino bluetooth device. The goal is for the Android user to receive a push notification if the phone leaves the range of the Arduino. This should occur regardless of whether the app is in the foreground or not. To do this, I am currently using a BroadcastReceiver in the Android Manifest. However, I am not receiving any such notifications.

Here is the Receiver class that implements BroadcastReceiver:

public class Receiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        String action = intent.getAction();
        if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
            if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
                pushNotification(context);
            }
        }
    }

    public void pushNotification(Context context) {

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setSmallIcon(R.mipmap.ic_launcher);

        builder.setAutoCancel(true);

        builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher));

        builder.setContentTitle("This is a notification!");
        builder.setContentText("This is the notification text!");
        builder.setSubText("This is the notification subtext!");

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, builder.build());
    }
}

And the AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver
            android:name=".Receiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

I'm fairly certain that the problem lies in the constants I am using in my logic. However, I'm not sure which ones I should use. As it is, the receiver is activated when ANY state change occurs to the Android Bluetooth, but I only want a notification when the connection is lost, which might not have anything to do with a state change of the Android Bluetooth receiver. What should I do to make sure pushNotification() is called under these conditions?

You have register wrong intent.

<action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />

This intent only indicate wheather the bluetooth is on and off.If you want to receive bluetooth devices connect state , you should use follow actions:

<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />

In you onRecive method:

if(TextUtils.equals(action,BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
   BluetoothDevice device = intent.getExtras()
                                   .getParcelable(BluetoothDevice.EXTRA_DEVICE);
   if (isYourDevice(device)) {
       // to push your notification
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM