2010-07-03 11 views
9

मुझे google android 2.1 का उपयोग कर क्षेत्र में उपलब्ध ब्लूटूथ डिवाइसों की एक सूची प्राप्त करने की आवश्यकता है।एंड्रॉइड में रेंज में उपलब्ध ब्लूटूथ डिवाइस के लिए स्कैन कैसे करें?

बात यह है कि मुझे केवल उन उपकरणों की सूची की आवश्यकता नहीं है, मुझे प्रत्येक डिवाइस के लिए कुछ अद्वितीय आईडी चाहिए और मुझे संकेतक की आवश्यकता है, सिग्नल कितना "अच्छा" प्राप्त होता है (जैसे एंड्रॉइड में "स्तर" .wifi.ScanResult) ... मैं यह कैसे करूँ?

उत्तर

7

Here डिवाइस खोजने के लिए एक संपूर्ण उदाहरण है।

आप मैक पते का उपयोग अद्वितीय आईडी के रूप में कर सकते हैं।

सिग्नल शक्ति के बारे में मुझे लगता है कि आपको RSSI (सिग्नल स्ट्रेंथ इंडिकेटर प्राप्त) का उपयोग करना चाहिए। नीचे

+0

किसी को इस आरएसएसआई चीज़ पर अनुभव है? मैं यहाँ थोड़ा असुरक्षित हूँ, क्योंकि यह निरंतर के रूप में definded है? – xenonite

+1

आपका पहला लिंक - http://developer.android.com/guide/topics/wireless/bluetooth.html#FindingDevices मर चुका है – Dayan

+0

लिंक वर्तमान में – Charlie

35

कोड की जाँच करें:

शुरू खोज

mBluetoothAdapter.startDiscovery(); 
mReceiver = new BroadcastReceiver() { 
public void onReceive(Context context, Intent intent) { 
    String action = intent.getAction(); 

    //Finding devices     
    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 
     mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
    } 
    } 
}; 

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
registerReceiver(mReceiver, filter); 
+3

+1 को अच्छे और आसान कोड के लिए तोड़ दिया है :) – Lucifer

+0

अच्छा साफ कोड, यह चाहिए सही जवाब हो। – Dayan

+0

मेरे लिए काम नहीं कर रहा है। –

1

कॉल विधि bluetoothScanning, संदर्भ की आवश्यकता है

void bluetoothScanning(){ 

    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
    context.registerReceiver(mReceiver, filter); 
    final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    mBluetoothAdapter.startDiscovery(); 

} 


// Create a BroadcastReceiver for ACTION_FOUND. 
private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
      // Discovery has found a device. Get the BluetoothDevice 
      // object and its info from the Intent. 
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      String deviceName = device.getName(); 
      String deviceHardwareAddress = device.getAddress(); // MAC address 

      Log.i("Device Name: " , "device " + deviceName); 
      Log.i("deviceHardwareAddress " , "hard" + deviceHardwareAddress); 
     } 
    } 
}; 

परिणाम

नाम: LE-बोस रिवोल्व + SoundLink deviceHardwareAddress : मैक 04: 52: सी 7: डी 1: बी 2: 76

.....

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