improvement on scan/re-scan logic to avoid failure
This commit is contained in:
parent
c53df989b8
commit
e930e03378
@ -26,6 +26,8 @@ import android.widget.AdapterView;
|
|||||||
import android.widget.ArrayAdapter;
|
import android.widget.ArrayAdapter;
|
||||||
import android.widget.ListView;
|
import android.widget.ListView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
import org.fdroid.fdroid.R;
|
import org.fdroid.fdroid.R;
|
||||||
import org.fdroid.fdroid.localrepo.SwapManager;
|
import org.fdroid.fdroid.localrepo.SwapManager;
|
||||||
import org.fdroid.fdroid.net.BluetoothDownloader;
|
import org.fdroid.fdroid.net.BluetoothDownloader;
|
||||||
@ -48,6 +50,8 @@ public class BluetoothDeviceListView extends ListView implements
|
|||||||
private MenuItem scanMenuItem;
|
private MenuItem scanMenuItem;
|
||||||
private MenuItem cancelMenuItem;
|
private MenuItem cancelMenuItem;
|
||||||
|
|
||||||
|
private boolean firstScan = true;
|
||||||
|
|
||||||
public BluetoothDeviceListView(Context context) {
|
public BluetoothDeviceListView(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
}
|
}
|
||||||
@ -132,6 +136,7 @@ public class BluetoothDeviceListView extends ListView implements
|
|||||||
addHeaderView(headerView);
|
addHeaderView(headerView);
|
||||||
|
|
||||||
setAdapter(adapter);
|
setAdapter(adapter);
|
||||||
|
setOnItemClickListener(this);
|
||||||
|
|
||||||
final BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
|
final BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
|
||||||
|
|
||||||
@ -147,6 +152,8 @@ public class BluetoothDeviceListView extends ListView implements
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void cancelBluetoothScan() {
|
private void cancelBluetoothScan() {
|
||||||
|
|
||||||
Log.d(TAG, "Cancelling bluetooth scan.");
|
Log.d(TAG, "Cancelling bluetooth scan.");
|
||||||
@ -178,49 +185,62 @@ public class BluetoothDeviceListView extends ListView implements
|
|||||||
|
|
||||||
loadingBar.show();
|
loadingBar.show();
|
||||||
|
|
||||||
|
|
||||||
final BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
|
final BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
|
||||||
final BroadcastReceiver deviceFoundReceiver = new BroadcastReceiver() {
|
|
||||||
@Override
|
|
||||||
public void onReceive(Context context, Intent intent) {
|
|
||||||
if (BluetoothDevice.ACTION_FOUND.equals(intent.getAction())) {
|
|
||||||
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
|
||||||
Log.d(TAG, "Found bluetooth device: " + device.toString());
|
|
||||||
|
|
||||||
if (device != null && device.getName() != null)
|
if (firstScan) {
|
||||||
if (device.getName().contains(BluetoothServer.BLUETOOTH_NAME_TAG)) {
|
final BroadcastReceiver deviceFoundReceiver = new BroadcastReceiver() {
|
||||||
boolean exists = false;
|
@Override
|
||||||
for (int i = 0; i < adapter.getCount(); i++) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
if (adapter.getItem(i).getAddress().equals(device.getAddress())) {
|
|
||||||
exists = true;
|
if (BluetoothDevice.ACTION_FOUND.equals(intent.getAction())) {
|
||||||
break;
|
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
||||||
|
Log.d(TAG, "Found bluetooth device: " + device.toString());
|
||||||
|
|
||||||
|
if (device != null && device.getName() != null)
|
||||||
|
if (device.getName().contains(BluetoothServer.BLUETOOTH_NAME_TAG)) {
|
||||||
|
boolean exists = false;
|
||||||
|
for (int i = 0; i < adapter.getCount(); i++) {
|
||||||
|
if (adapter.getItem(i).getAddress().equals(device.getAddress())) {
|
||||||
|
exists = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!exists) {
|
||||||
|
adapter.add(device);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (!exists) {
|
|
||||||
adapter.add(device);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
};
|
|
||||||
|
|
||||||
final BroadcastReceiver scanCompleteReceiver = new BroadcastReceiver() {
|
final BroadcastReceiver scanCompleteReceiver = new BroadcastReceiver() {
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
Log.d(TAG, "Scan complete: " + intent.getAction());
|
Log.d(TAG, "Scan complete: " + intent.getAction());
|
||||||
loadingBar.hide();
|
loadingBar.hide();
|
||||||
cancelMenuItem.setVisible(false);
|
cancelMenuItem.setVisible(false);
|
||||||
scanMenuItem.setVisible(true);
|
scanMenuItem.setVisible(true);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
getContext().registerReceiver(deviceFoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
|
getContext().registerReceiver(deviceFoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
|
||||||
getContext().registerReceiver(scanCompleteReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));
|
getContext().registerReceiver(scanCompleteReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));
|
||||||
|
|
||||||
|
firstScan = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (bluetooth.isDiscovering())
|
||||||
|
{
|
||||||
|
bluetooth.cancelDiscovery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!bluetooth.startDiscovery()) {
|
if (!bluetooth.startDiscovery()) {
|
||||||
// TODO: Discovery did not start for some reason :(
|
// TODO: Discovery did not start for some reason :(
|
||||||
Log.e(TAG, "Could not start bluetooth discovery, but am not sure why :(");
|
Log.e(TAG, "Could not start bluetooth discovery, but am not sure why :(");
|
||||||
|
Toast.makeText(getContext(),"There was a problem looking for Bluetooth devices",Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -231,6 +251,8 @@ public class BluetoothDeviceListView extends ListView implements
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||||
|
|
||||||
@ -327,11 +349,11 @@ public class BluetoothDeviceListView extends ListView implements
|
|||||||
BluetoothDevice device = getItem(position);
|
BluetoothDevice device = getItem(position);
|
||||||
TextView nameView = (TextView)view.findViewById(android.R.id.text1);
|
TextView nameView = (TextView)view.findViewById(android.R.id.text1);
|
||||||
TextView addressView = (TextView)view.findViewById(android.R.id.text2);
|
TextView addressView = (TextView)view.findViewById(android.R.id.text2);
|
||||||
TextView descriptionView = (TextView)view.findViewById(R.id.text3);
|
//TextView descriptionView = (TextView)view.findViewById(R.id.text3);
|
||||||
|
|
||||||
nameView.setText(device.getName() == null ? getContext().getString(R.string.unknown) : device.getName());
|
nameView.setText(device.getName() == null ? getContext().getString(R.string.unknown) : device.getName());
|
||||||
addressView.setText(device.getAddress());
|
addressView.setText(device.getAddress());
|
||||||
descriptionView.setText(bondStateToLabel(device.getBondState()));
|
//descriptionView.setText(bondStateToLabel(device.getBondState()));
|
||||||
|
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user