0

からのインテントフィルタを既に持っている放送受信機にデータを渡します。私は自分のアプリを自動的にオンにしたい。これを行うには、私はペアリングされた車のBluetoothのデバイス名を文字列に保存する必要があります。携帯電話のBluetoothが何かに接続されているとき。私は車を確認する必要があります。もし私がサービスを開始したいのであれば。電話機が車内のブルートゥースに接続されているときに、アクティビティ

受信者が既にACTION_ACL_CONNECTEDを聴取するインテントフィルタを受信して​​いるため、Bluetoothカーデバイス名を含む文字列を受信者に渡すことができません。インテントフィルタをインテントフィルタ&を同じ受信者に送信することが可能です。

この場合、アクティビティから受信者にbtdeviceName文字列を送信するにはどうすればよいですか。

主な活動

private void addDrawerItems() { 

    final BroadcastReceiver bluetoothBroadcast = new BluetoothReceiver(); 
    final IntentFilter blueToothFilter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED); 


    final Intent btbroadcastIntent = new Intent(this, BluetoothReceiver.class); 
    btbroadcastIntent.putExtra("btDeviceName", mPairedBluetoothDevice); 


    String[] osArray = {"Bluetooth Auto Start", "Reply to Calls", "Reply to sms", "Customise Message"}; 

    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, osArray); 


    if (mIsPremiumUser) { 
     mDrawerList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE); 
    } else { 
     mDrawerList.setChoiceMode(AbsListView.CHOICE_MODE_NONE); 
    } 

    mDrawerList.setAdapter(mAdapter); 


    mDrawerList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
     @Override 
     public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 


      if (position == 0) { 
       Toast.makeText(getApplicationContext(), "blue", Toast.LENGTH_LONG).show(); 
       showBluetoothDialog(); 
      } 
      return true; 
     } 
    }); 


    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      CheckedTextView ctv = (CheckedTextView) view; 

      if (!mIsPremiumUser) { 
       Toast.makeText(getApplication(), "Upgrade", Toast.LENGTH_LONG).show(); 
       return; 
      } 


      switch (position) { 

       case 0: 

        if (ctv.isChecked()) { 

         if (!isblueToothRegistered) { 
          registerReceiver(bluetoothBroadcast, blueToothFilter); 
          sendBroadcast(btbroadcastIntent); 
          isblueToothRegistered = true; 
         } 

        } else { 
         if (isblueToothRegistered) { 
          unregisterReceiver(bluetoothBroadcast); 
          isblueToothRegistered = false; 
         } 
        } 

        break; 

BluetoothReceiver

public class BluetoothReceiver extends BroadcastReceiver { 

private MainActivity ma; 
private String pairedDevice; 


public void onReceive(Context context, Intent intent) { 


    Toast.makeText(context, "Receieved", Toast.LENGTH_LONG).show(); 
    String action = intent.getAction(); 

    pairedDevice = intent.getStringExtra("btDeviceName"); 
    Toast.makeText(context, pairedDevice + "2", Toast.LENGTH_LONG).show(); 

    final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 


    if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) { 
     Toast.makeText(context, "Bluetooth Connected", Toast.LENGTH_LONG).show(); 


     if (device.getName().equals(pairedDevice)) { 
      Toast.makeText(context, device.getName() + " 1", Toast.LENGTH_LONG).show(); 
     } 


    } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) { 
     Toast.makeText(context, "Bluetooth Disconnected", Toast.LENGTH_LONG).show(); 
    } 


} 

}

答えて

1

IntentFilterは複数のアクションを有することができます。まず、自分のカスタムアクション名を作成して、それを聞いてblueToothFilterに追加します。あなたはこのIntentFilterbluetoothBroadcast受信機を登録すると

blueToothFilter.addAction("my.custom.action"); 

は、それが今、両方のアクションの呼び出しを受信します。新しいカスタムアクションを処理する別の条件をonReceiveに追加します。

最後に、Activityのカスタムアクションと準備ができたらデバイス名でブロードキャストを送信します。

Intent intent = new Intent() 
     .setAction("my.custom.action") 
     .putExtra("btDeviceName", mPairedBluetoothDevice); 

sendBroadcast(intent); 

UPDATE

私は今あなたがonReceive()への単一の呼び出しでBluetoothDevice deviceString pairedDeviceの両方を望んでいることを理解しています。これらの変数は別々のアクションから取られ、各アクションはonReceive()を1回呼び出すため、これは不可能です。

これを修正するには、BluetoothReceiverActivityの内部クラスに変更して、必要なデータへの参照を保持することができます。

+0

ありがとうございます。私はまだこれが放送受信機でどうやって見えるのか少し混乱しているのですか? –

+0

どのように私は受信機の2つのアクションを区別するのですか? –

+0

'intent.getAction()'から返される内容を確認します。今は2つのケースがあります.1つは 'BluetoothDevice.ACTION_ACL_DISCONNECTED'と' BluetoothDevice.ACTION_ACL_CONNECTED'です。あなたはそれを呼び出すために選択したカスタムアクション名(私の例では 'my.custom.action')をチェックする3番目の要素を追加する必要があります。これがアクションであれば、その意図に 'btDeviceName'が追加されることがわかります。 –

関連する問題