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

标签 java android android-intent bluetooth

我正在 try catch 蓝牙设备断开连接 Intent 过滤器。 我向 onReceive 添加了一个日志,但它从未到达它,也没有显示在 logcat 中。 我怀疑问题出在我的 manifest.xml 配置上:

list :

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

    }

最佳答案

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

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

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

关于java - Android 蓝牙 - 检测与设备的断开连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13147653/

相关文章:

android - 从服务器向 Android 应用程序发送命令

java - 使用 RestTemplate (POST) 使用 SOAP 服务抛出 405 Method Not allowed

java - 如何使驱动程序类中的方法看到另一个类的子类?

java - 属性顺序放入 Java 属性文件中

java - 单击首选项时如何启动 fragment

android - 如何不打开已通过最近的应用程序完成的 Activity

java - 按下 JButton 后将 JTable 放置在 JPanel 上

Android Application onCreate,什么时候调用

android - 无法从设备打印

Android 使用 android.content.Intent.ACTION_SEND 发送电子邮件(带/用户名和密码身份验证)