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

时间:2016-04-09 07:53:13

标签: java android xml bluetooth

我正在编写一个连接到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()?

1 个答案:

答案 0 :(得分:1)

您注册了错误的意图。

<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
   }
}