java Android 蓝牙 - 检测与设备的断开连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13147653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:39:32  来源:igfitidea点击:

Android Bluetooth - detect a disconnection from a device

javaandroidandroid-intentbluetooth

提问by user1528794

I am trying to catch a bluetooth device disconnection intent filter. I added a log to the onReceive but it never reaches it and is not displayed in the logcat. I suspect that the problem is with my manifest.xml configuration:

我正在尝试捕获蓝牙设备断开连接意图过滤器。我在 onReceive 中添加了一个日志,但它永远不会到达它并且没有显示在 logcat 中。我怀疑问题出在我的 manifest.xml 配置上:

manifest:

显现:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.company"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="16" />

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name="com.company.MyReceiver" android:enabled="true" android:exported="true">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
                <action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" />
                <action android:name="android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED" />
                <action android:name="android.bluetooth.adapter.action.DISCOVERY_STARTED" />
            </intent-filter>
        </receiver>

        <activity
            android:name=".BTActivity"
            android:label="BTActivity" >
        </activity>
    </application>

</manifest>

MyReceiver extends BroadcastReceiver:

MyReceiver 扩展了 BroadcastReceiver:

@Override
    public void onReceive(Context context, Intent intent) {
        Log.i("got-in", "got-in-");
        // String action = intent.getAction();
        BluetoothDevice device = intent
                .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

        Log.i("disconnect", device.getName());
        Intent i = new Intent(context, BTActivity.class);
        Bundle b = new Bundle();
        b.putString("deviceName", device.getName());
        intent.putExtras(b); // Put your id to your next Intent
        context.startActivity(i);
        // finish();

    }

回答by Force

Use this code for receiving a disconnect (when Bluetooth is still turned on):

使用此代码接收断开连接(当蓝牙仍处于打开状态时):

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

Also you have to register a service that then registers a broadcast receiver for Bluetooth status change, so your app knows when Bluetooth was turned off and therefore discards all active devices, as you won't receive a ACL_DISCONNECT when Bluetooth is simply turned off.

此外,您必须注册一个服务,然后为蓝牙状态更改注册广播接收器,以便您的应用程序知道蓝牙何时关闭并因此丢弃所有活动设备,因为当蓝牙关闭时您不会收到 ACL_DISCONNECT。

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {

    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver(bluetoothTurnedOnOff, filter);
    return START_STICKY;
}

private final BroadcastReceiver bluetoothTurnedOnOff = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) {
 [...]

回答by zee008

For new users. it will works . Your manifest file looks

对于新用户。它会起作用。你的清单文件看起来

<receiver android:name="(package name).BluetoothReceiver" >
            <intent-filter>
                <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
                <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
            </intent-filter>
        </receiver>

and your receiver will look like

你的接收器看起来像

else if (intent.getAction().equals(`enter code here`
    BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
    // connection lost

回答by dnkoutso

Try removing the <category .../>from the intent-filter and try again.

尝试<category .../>从意图过滤器中删除 ,然后重试。

回答by Kirill Lebedev

Try android.bluetooth.device.action.ACL_DISCONNECTED action in intent filter. It should solve your problem.

在意图过滤器中尝试 android.bluetooth.device.action.ACL_DISCONNECTED 操作。它应该可以解决您的问题。