когда телефон подключен к блютусу в машине. Я хочу, чтобы мое приложение включалось автоматически. для этого я должен сохранить имя сопряженного автомобильного Bluetooth-устройства в строку. затем, когда телефоны bluetooth подключены к чему-либо. Я должен проверить, машина ли это. Если это так, я хочу запустить службу.
У меня возникли трудности с передачей строки, содержащей имя автомобильного устройства Bluetooth, в приемник, так как мой приемник уже получает фильтр намерений для прослушивания ACTION_ACL_CONNECTED. можно ли отправить намерение и фильтр намерения одному и тому же получателю.
Как я могу отправить строку 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();
}
}
}