繁体   English   中英

Android-检查蓝牙连接是否丢失?

[英]Android - check for loss of Bluetooth connection?

我正在写一个连接到Arduino蓝牙设备的应用程序。 目标是让Android用户在手机离开Arduino的范围时收到推送通知。 无论应用程序是否在前台,都应该发生这种情况。 为此,我目前在Android Manifest中使用BroadcastReceiver。 但是,我没有收到任何此类通知。

这是实现BroadcastReceiver的Receiver类:

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());
    }
}

还有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>

我相当确定问题出在我在逻辑中使用的常量。 但是,我不确定应该使用哪个。 照原样,当Android蓝牙发生任何状态更改时,都会激活接收器,但是我只想在连接断开时收到通知,这可能与Android蓝牙接收器的状态更改无关。 如何确保在这些情况下调用pushNotification()?

您注册了错误的意图。

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

此意图仅表明蓝牙已打开和关闭。如果要接收蓝牙设备的连接状态,则应使用以下操作:

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

在您的onRecive方法中:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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