我試圖不斷地顯示藍牙設備並在屏幕上顯示它們,但它永遠不會顯示我所有的設備,而是一次只顯示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;
我用藍牙啓用附近的兩個設備檢查,但我可以只有一個,我已經測試了多次 – 2014-10-07 09:30:16
啓用!=可見,請確保它們也可見。您可以在mDeviceList.add之上添加Log.d消息來查看會發生什麼。註冊到BluetoothAdapter.ACTION_DISCOVERY_STARTED和BluetoothAdapter.ACTION_DISCOVERY_FINISHED也是有意義的。 – mikkokoo 2014-10-07 12:33:22
當然,我讓它們可見,讓我試試Log.d和其他意圖過濾器,你的意圖 – 2014-10-07 14:24:32