からのインテントフィルタを既に持っている放送受信機にデータを渡します。私は自分のアプリを自動的にオンにしたい。これを行うには、私はペアリングされた車の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();
}
}
}
ありがとうございます。私はまだこれが放送受信機でどうやって見えるのか少し混乱しているのですか? –
どのように私は受信機の2つのアクションを区別するのですか? –
'intent.getAction()'から返される内容を確認します。今は2つのケースがあります.1つは 'BluetoothDevice.ACTION_ACL_DISCONNECTED'と' BluetoothDevice.ACTION_ACL_CONNECTED'です。あなたはそれを呼び出すために選択したカスタムアクション名(私の例では 'my.custom.action')をチェックする3番目の要素を追加する必要があります。これがアクションであれば、その意図に 'btDeviceName'が追加されることがわかります。 –