2014-06-26 16 views
5

Próbuję znaleźć wszystkie dostępne urządzenia Bluetooth w zakresie. Ale otrzymuję tylko jedno urządzenie, którego używam w metodzie wątek w uruchomieniu. Sprawdziłem już wiele linków dla tego problemu, ale nie mogłem rozwiązać tego problemu. Oto mój kodJak znaleźć dostępne urządzenia Bluetooth w zasięgu?

public void run() { 


if(service != null) { 
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
    service.registerReceiver(this.bReceiver, filter); 
     bluetooth.startDiscovery(); 
} 

} 
class BluetoothReceiver extends BroadcastReceiver { 
public void onReceive(Context context, Intent intent) { 

    Set<BluetoothDevice> pairedDevices = bluetooth.getBondedDevices(); 

    String action = intent.getAction(); 
    if (pairedDevices.size() > 0) { 
      for (BluetoothDevice device : pairedDevices) { 
       int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE); 
     Log.d(TAG, device.getName()); 
    } 
    } 

    if(BluetoothDevice.ACTION_FOUND.equals(action)) { 
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
    String uuid = intent.getStringExtra(BluetoothDevice.EXTRA_UUID); 
    int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE); 
    Log.d(TAG, device.getName()); 
    } 
    } 

} 

Ponadto chcę RSSI wartości każdego znalezionego urządzenia, ale proszę ignorować składnię

Odpowiedz

2

To jak ja wyszukiwanie urządzeń Bluetooth w Activity i pokazać swoje nazwisko i adres mac w ListView. Oprócz wyświetlania urządzeń w ListView, możesz zrobić praktycznie wszystko z odkrytym obiektem BluetoothDevice.

FindBluetoothActivity.java

public class FindBluetoothActivity extends Activity { 

    private BluetoothAdapter mBtAdapter; 
    private ListView mLvDevices; 
    private ArrayList<String> mDeviceList = new ArrayList<String>(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_find_bluetooth); 

     mLvDevices = (ListView) findViewById(R.id.lvDevices); 

     IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);   
     registerReceiver(mBtReceiver, filter); 

     // Getting the Bluetooth adapter 
     mBtAdapter = BluetoothAdapter.getDefaultAdapter(); 
     if(mBtAdapter != null) { 
      mBtAdapter.startDiscovery(); 
      Toast.makeText(this, "Starting discovery...", Toast.LENGTH_SHORT).show(); 
     } 
     else { 
      Toast.makeText(this, "Bluetooth disabled or not available.", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     if (mBtAdapter != null) { 
      mBtAdapter.cancelDiscovery(); 
     } 
     unregisterReceiver(mBtReceiver); 
    } 

    private final BroadcastReceiver mBtReceiver = new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 
      if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
       mDeviceList.add(device.getAddress() + ", " + device.getName()); // get mac address 

       ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, mDeviceList); 
       mLvDevices.setAdapter(adapter); 
      } 
     } 
    }; 
} 

Układ plik .xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".FindBluetoothActivity" > 

    <ListView 
     android:id="@+id/lvDevices" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" > 
    </ListView> 

</RelativeLayout> 

Android manifest.xml file:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.bluetoothexample" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="11" 
     android:targetSdkVersion="18" /> 
    <uses-permission android:name="android.permission.BLUETOOTH"/> 
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@android:style/Theme.Holo.Light" > 
     <activity 
      android:name="com.example.bluetoothexample.FindBluetoothActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

Dodatkowe informacje:

  • Upewnij Bluetooth jest włączonyna urządzeniu
  • dodać uprawnienia android.permission.BLUETOOTH i android.permission.BLUETOOTH_ADMIN do pliku Manifest.xml
  • Koniecznie wyrejestrować swojej transmisji odbiornika podczas niszczenia Activity
  • Należy pamiętać, że urządzenia Bluetooth w zakresie muszą być wykrywalne, które można znaleźć za pomocą aplikacji acji. Włączenie Bluetooth na nich sam może nie wystarczyć. Bardzo często zdarza się, że użytkownik musi włączyć jakiś tryb "wykrywalny", zanim urządzenie zostanie wykryte przez inne urządzenia.
  • Należy pamiętać, że zakres , w których urządzenia są wykrywalne przez Bluetooth jest zwykle około 10m wewnątrz i wokół 50m zewnątrz
+0

Ale jeśli widzisz mój kod ... to jest taki sam jak twój. ..i nie widzę żadnej różnicy, dlaczego mój kod nie działa –

+0

Co dokładnie nie działa w twoim kodzie? Czy nazywa się onReceive (...)? –

+0

Problem polega na tym, że aplikacja się zawiesza po dotarciu do tego odbiornika nie wiem dlaczego i innym poważnym problemem jest to, że dostaję tylko jedno aktywne urządzenie bluetooth zamiast tego, że chcę wszystkie dostępne aktywne urządzenia bluetooth ... –

Powiązane problemy