2014-10-02 77 views
5

我試圖不斷地顯示藍牙設備並在屏幕上顯示它們,但它永遠不會顯示我所有的設備,而是一次只顯示1個。我找不到我做錯了什麼。這裏是我的代碼,可能是你可以找到它的任何問題。謝謝找不到所有的藍牙設備android

class monitorBluetooth extends monitor { 
private ListView mLvDevices; 
private ArrayList<String> mDeviceList = new ArrayList<String>(); 

public monitorBluetooth(service service) { 

super(service); 
    bluetooth = BluetoothAdapter.getDefaultAdapter(); 

this.bReceiver = new BluetoothReceiver(); 
} 

public void finalize() throws Throwable { 

super.finalize(); 
} 

public void run() {   

    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
    service.registerReceiver(this.bReceiver, filter); 

if(service != null) { 
     bluetooth = BluetoothAdapter.getDefaultAdapter(); 
     bluetooth.startDiscovery(); 
}  
} 
class BluetoothReceiver extends BroadcastReceiver { 
public void onReceive(Context context, Intent intent) { 
    Set<BluetoothDevice> pairedDevices = bluetooth.getBondedDevices(); 

    String action = intent.getAction(); 

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

    } 

} 
} 

BluetoothAdapter bluetooth; 
private BluetoothReceiver bReceiver; 

回答

1

您的代碼似乎工作。我給LogCat寫了你添加到mDeviceList中的東西,並得到了多個設備。

也許這是你如何顯示名稱或可能有不超過一個可見的BT設備?

編輯:添加的代碼

public class MonitorBluetooth extends Thread{ 
BluetoothAdapter bluetooth; 
private BluetoothReceiver bReceiver; 
private Context mContext; 

public MonitorBluetooth(Context context){ 
    bluetooth=BluetoothAdapter.getDefaultAdapter(); 
    this.bReceiver=new BluetoothReceiver(); 
    this.mContext=context; 
} 

public void finalize() throws Throwable{ 
    super.finalize(); 
} 

public void run(){ 
    { 
    IntentFilter filter=new IntentFilter(BluetoothDevice.ACTION_FOUND); 
    mContext.registerReceiver(this.bReceiver,filter); 
    } 
    { 
    IntentFilter filter=new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED); 
    mContext.registerReceiver(this.bReceiver,filter); 
    } 
    { 
    IntentFilter filter=new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
    mContext.registerReceiver(this.bReceiver,filter); 
    } 
    bluetooth=BluetoothAdapter.getDefaultAdapter(); 
    bluetooth.startDiscovery(); 
} 

class BluetoothReceiver extends BroadcastReceiver{ 
    public void onReceive(Context context,Intent intent){ 
    String action=intent.getAction(); 
    if(BluetoothDevice.ACTION_FOUND.equals(action)){ 
    BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
    Log.d("BluetoothReceiver","found "+device.getAddress()+", "+device.getName()); 
    } 
    else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){ 
    Log.d("BluetoothReceiver","discovery started"); 
    } 
    else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){ 
    Log.d("BluetoothReceiver","discovery finished"); 
    context.unregisterReceiver(this); 
    } 
    } 
} 
} 

有了這個代碼,我得到的所有設備周圍致電

MonitorBluetooth monitor=new MonitorBluetooth(this.getApplicationContext()); 
monitor.start(); 
從Activity.onCreate(捆綁)

+0

我用藍牙啓用附近的兩個設備檢查,但我可以只有一個,我已經測試了多次 – 2014-10-07 09:30:16

+0

啓用!=可見,請確保它們也可見。您可以在mDeviceList.add之上添加Log.d消息來查看會發生什麼。註冊到BluetoothAdapter.ACTION_DISCOVERY_STARTED和BluetoothAdapter.ACTION_DISCOVERY_FINISHED也是有意義的。 – mikkokoo 2014-10-07 12:33:22

+0

當然,我讓它們可見,讓我試試Log.d和其他意圖過濾器,你的意圖 – 2014-10-07 14:24:32

1

嘗試這樣的一些感受訂購:

public void displayDetectedDevices(){ 
    mBluetoothAdapter.startDiscovery(); 

    // Create a BroadcastReceiver for ACTION_FOUND 
    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)){ 
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
       String deviceName = device.getName(); 
       String deviceAddress = device.getAddress(); 
       String s = " "; 
       unpairedDevices.add(deviceName + s + deviceAddress +" \n"); 
       unpairedDevicesList = new ArrayList<String>(unpairedDevices); 
       Toast.makeText(getActivity(), unpairedDevicesList.toString(), Toast.LENGTH_LONG).show(); 
      } 
     } 
    }; 
} 

記得的onCreate()聲明如下:

public void onCreate(Bundle savedInstance){ 
    super.onCreate(savedInstance); 
    unpairedDevicesList = new ArrayList<String>(); 
    unpairedDevices = new HashSet<String>(); 
} 

記住#2聲明如下之前的onCreate():

ArrayList<String> unpairedDevicesList; 
Set<String> unpairedDevices; 

最後的思考:

我同時使用一個Set和一個arrayList來放置檢測到的設備。一個設置將不包含重複項。取決於你想要做什麼,因爲它們提供了不同的功能,所定義的都是有用的。另外here是我的github頁面,它包含更多的信息和完整的工作藍牙應用程序。

隨着設置的填充設備將開始填充屏幕取決於你希望你的UI的工作方式。我用了一個簡單的列表。

0

我對藍牙耳機有同樣的問題。我有兩個藍牙耳機,兩個都開機,但是他們沒有被blueToothAdapter.startDiscovery()檢測到。

後來我發現,在藍牙耳機的情況下,您需要長按啓動按鈕才能激活他們的配對模式,以便通過android藍牙發現進行檢測。

下面是從developer.android.com發現附近的藍牙設備的一個很好的例子。