2014-06-26 5 views
5

मैं एक श्रृंखला में सभी उपलब्ध ब्लूटूथ डिवाइस खोजने की कोशिश कर रहा हूं। लेकिन मुझे केवल एक डिवाइस मिल रहा है जिसे मैं थ्रेड में रन विधि में उपयोग कर रहा हूं। मैंने इस समस्या के लिए पहले से ही कई लिंक देखे हैं लेकिन इस मुद्दे को हल नहीं कर सके। यहाँ मेरी कोडरेंज में उपलब्ध ब्लूटूथ डिवाइस कैसे खोजें?

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()); 
    } 
    } 

} 

अलावा में मैं प्रत्येक पाया डिवाइस के RSSI मूल्य चाहते हैं, लेकिन कृपया वाक्य रचना

उत्तर

2

उपेक्षा इस तरह मैं ब्लूटूथ डिवाइस के लिए खोज एक Activity में और उनके नाम दिखाती हैं और ListView में मैक पता। ListView में डिवाइस को प्रदर्शित करने के अलावा, आप खोजी 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); 
      } 
     } 
    }; 
} 

लेआउट .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> 

एंड्रॉयड Manifest.xml फ़ाइल:

<?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> 

अतिरिक्त जानकारी:

  • सुनिश्चित करें कि ब्लूटूथ आपके डिवाइस पर सक्षम
  • लिए अनुमतियां जोड़ें android.permission.BLUETOOTH और android.permission.BLUETOOTH_ADMIN अपने Manifest.xml फ़ाइल को
  • अपंजीकृत अपने प्रसारण रिसीवर करना सुनिश्चित करें जब नष्ट है Activity
  • ध्यान रखें कि में ब्लूटूथ डिवाइस खोजने योग्य आपके आवेदक द्वारा पाए जाने योग्य होने की आवश्यकता है समझना। अकेले उन पर ब्लूटूथ सक्षम करना पर्याप्त नहीं हो सकता है। यह अक्सर ऐसा होता है कि उपयोगकर्ता द्वारा अन्य उपकरणों द्वारा डिवाइस की खोज की जा सकने से पहले किसी प्रकार के "खोजने योग्य" मोड को उपयोगकर्ता द्वारा सक्षम करने की आवश्यकता होती है।
  • पता है कि रेंज जिसमें उपकरणों ब्लूटूथ के माध्यम से खोज योग्य हैं आमतौर पर लगभग 10 मी है रहो घर के अंदर और आसपास 50 मीटर सड़क पर
+0

लेकिन अगर आप मेरी कोड देखें ... यह आपके जैसे ही है। .. मुझे कोई फर्क नहीं पड़ता है तो मेरा कोड क्यों काम नहीं कर रहा है –

+0

आपके कोड में वास्तव में क्या काम नहीं कर रहा है? पर निर्भर है (...) कहा जा रहा है? –

+0

समस्या यह है कि इस प्रसारण रिसीवर में पहुंचने के बाद एप्लिकेशन को लटका दिया जाता है, यह नहीं पता कि क्यों और अन्य प्रमुख मुद्दे मुझे केवल एक सक्रिय ब्लूटूथ डिवाइस मिलता है, इसके बजाय मुझे सभी उपलब्ध सक्रिय ब्लूटूथ डिवाइस चाहिए ... –

संबंधित मुद्दे