Jak wyświetlić listę wszystkich podłączonych urządzeń bluetooth na urządzeniu z systemem Android?listę podłączonych urządzeń bluetooth?
dziękuję!
Jak wyświetlić listę wszystkich podłączonych urządzeń bluetooth na urządzeniu z systemem Android?listę podłączonych urządzeń bluetooth?
dziękuję!
Proszę przeanalizować this class online.
Tutaj dowiesz się, jak odkryć wszystkie podłączone (sparowane) urządzenia Bluetooth.
Więc tutaj są kroki:
Pierwsze, zaczynasz zamiar odkryć urządzeń
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
Zarejestruj reciver rozgłaszania do niego:
registerReceiver(mReceiver, filter);
Z definicji mReceiver:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
arrayadapter.add(device.getName())//arrayadapter is of type ArrayAdapter<String>
lv.setAdapter(arrayadapter); //lv is the list view
arrayadapter.notifyDataSetChanged();
}
}
i lista zostanie automatycznie wypełniane na nowym odkryciu urządzenia.
Brakuje}; na końcu kodu. Nie mogę dokonać edycji osobiście, ponieważ musi to być 6-znakowa zmiana, której brakuje tylko 2. –
System Android nie pozwala wyszukiwać wszystkich "aktualnie" podłączonych urządzeń. Można jednak wyszukiwać sparowane urządzenia. Konieczne będzie użycie odbiornika transmisji, aby wysłuchać zdarzeń ACTION_ACL_ {CONNECTED | DISCONNECTED} wraz ze zdarzeniem STATE_BONDED, aby zaktualizować stany aplikacji, aby śledzić, co jest aktualnie połączone.
Jak API 14 (lody), Android ma kilka nowych metod BluetoothAdapter tym:
public int getProfileConnectionState (int profile)
gdzie profil jest jednym z HEALTH, HEADSET, A2DP
Sprawdź odpowiedzi, jeśli nie jest to STATE_DISCONNECTED
wiesz masz połączenie na żywo.
Oto przykład kodu, który będzie działać na każdym urządzeniu API:
BluetoothAdapter mAdapter;
/**
* Check if a headset type device is currently connected.
*
* Always returns false prior to API 14
*
* @return true if connected
*/
public boolean isVoiceConnected() {
boolean retval = false;
try {
Method method = mAdapter.getClass().getMethod("getProfileConnectionState", int.class);
// retval = mAdapter.getProfileConnectionState(android.bluetooth.BluetoothProfile.HEADSET) != android.bluetooth.BluetoothProfile.STATE_DISCONNECTED;
retval = (Integer)method.invoke(mAdapter, 1) != 0;
} catch (Exception exc) {
// nothing to do
}
return retval;
}
public void checkConnected()
{
// true == headset connected && connected headset is support hands free
int state = BluetoothAdapter.getDefaultAdapter().getProfileConnectionState(BluetoothProfile.HEADSET);
if (state != BluetoothProfile.STATE_CONNECTED)
return;
try
{
BluetoothAdapter.getDefaultAdapter().getProfileProxy(_context, serviceListener, BluetoothProfile.HEADSET);
}
catch (Exception e)
{
e.printStackTrace();
}
}
private ServiceListener serviceListener = new ServiceListener()
{
@Override
public void onServiceDisconnected(int profile)
{
}
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy)
{
for (BluetoothDevice device : proxy.getConnectedDevices())
{
Log.i("onServiceConnected", "|" + device.getName() + " | " + device.getAddress() + " | " + proxy.getConnectionState(device) + "(connected = "
+ BluetoothProfile.STATE_CONNECTED + ")");
}
BluetoothAdapter.getDefaultAdapter().closeProfileProxy(profile, proxy);
}
};
Jeżeli masz rozwiązanie tego. Szukam tego samego, proszę podać odpowiedź. –