【问题标题】:Android Bluetooth - detect a disconnection from a deviceAndroid蓝牙 - 检测与设备的断开连接
【发布时间】:2012-10-30 21:11:49
【问题描述】:

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

清单:

<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 扩展了 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();

    }

【问题讨论】:

    标签: java android android-intent bluetooth


    【解决方案1】:

    使用此代码接收断开连接(当蓝牙仍然打开时):

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

    您还必须注册一个服务,然后注册一个广播接收器以更改蓝牙状态,以便您的应用知道蓝牙何时关闭并因此丢弃所有活动设备,因为当蓝牙只是打开时您不会收到 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) {
     [...]
    

    【讨论】:

    • 为什么我必须创建服务?在我的清单 xml 文件中声明一个接收器和一个扩展 BroadcastReceiver 的类还不够吗?这还不够吗?
    • 您必须创建一个服务,因为 Intent BluetoothAdapter.ACTION_STATE_CHANGED 无法通过清单注册,因此您必须创建一个注册自定义接收器的服务
    【解决方案2】:

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

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

    你的接收器看起来像

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

    【讨论】:

      【解决方案3】:

      尝试从意图过滤器中删除&lt;category .../&gt;,然后重试。

      【讨论】:

      • 也试过了,但还是没有进入onreceive回调函数。我错过了什么?请帮忙!
      【解决方案4】:

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

      【讨论】:

      • 不。还是一样。我的猜测是我的清单不正确。但我不确定问题是什么。
      • 事实上,即使是“android.bluetooth.adapter.action.DISCOVERY_STARTED”也不起作用....
      猜你喜欢
      • 1970-01-01
      • 2018-01-20
      • 2018-06-25
      • 2015-10-19
      • 2016-01-30
      • 2015-12-18
      • 2014-05-05
      • 1970-01-01
      • 2016-12-28
      相关资源
      最近更新 更多