3

Bluetooth デバイス切断インテント フィルターをキャッチしようとしています。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();

    }
4

4 に答える 4

6

切断を受信するには、次のコードを使用します (Bluetooth がまだオンになっている場合)。

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

また、Bluetooth ステータス変更のためにブロードキャスト レシーバーを登録するサービスを登録する必要があります。これにより、アプリは Bluetooth がいつオフになったかを認識し、アクティブなデバイスをすべて破棄します。これは、Bluetooth がオフになっているだけでは 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) {
 [...]
于 2012-11-01T12:59:07.457 に答える
1

<category .../>インテント フィルタから を削除してから、もう一度お試しください。

于 2012-10-31T05:34:56.497 に答える
0

インテント フィルターで android.bluetooth.device.action.ACL_DISCONNECTED アクションを試してください。それはあなたの問題を解決するはずです。

于 2012-10-30T22:04:24.417 に答える