2015-08-03 23:09:49 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.bluetooth.BluetoothAdapter;
|
|
|
|
import android.bluetooth.BluetoothDevice;
|
|
|
|
import android.bluetooth.BluetoothManager;
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.os.Bundle;
|
2015-05-06 22:06:09 +02:00
|
|
|
import android.os.Handler;
|
2015-05-10 23:14:32 +02:00
|
|
|
import android.os.Message;
|
2015-05-05 00:48:02 +02:00
|
|
|
import android.os.Parcelable;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.ProgressBar;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
2015-05-12 06:28:11 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
2015-05-18 20:56:19 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2015-09-24 14:45:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.adapter.DeviceCandidateAdapter;
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
|
2015-09-24 14:45:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
|
|
|
public class DiscoveryActivity extends Activity implements AdapterView.OnItemClickListener {
|
2015-05-12 06:28:11 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(DiscoveryActivity.class);
|
2015-05-06 22:06:09 +02:00
|
|
|
private static final long SCAN_DURATION = 60000; // 60s
|
|
|
|
|
2015-11-23 23:04:46 +01:00
|
|
|
private final Handler handler = new Handler();
|
2015-05-05 00:48:02 +02:00
|
|
|
|
2015-11-23 23:04:46 +01:00
|
|
|
private final BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {
|
2015-05-05 00:48:02 +02:00
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
switch (intent.getAction()) {
|
|
|
|
case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
|
2015-05-09 23:54:47 +02:00
|
|
|
discoveryStarted(Scanning.SCANNING_BT);
|
2015-05-05 00:48:02 +02:00
|
|
|
break;
|
|
|
|
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
|
2015-05-09 23:54:47 +02:00
|
|
|
// continue with LE scan, if available
|
2015-05-11 00:50:18 +02:00
|
|
|
if (isScanning == Scanning.SCANNING_BT) {
|
|
|
|
startDiscovery(Scanning.SCANNING_BTLE);
|
|
|
|
} else {
|
|
|
|
discoveryFinished();
|
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
break;
|
|
|
|
case BluetoothAdapter.ACTION_STATE_CHANGED:
|
|
|
|
int oldState = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, BluetoothAdapter.STATE_OFF);
|
|
|
|
int newState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
|
|
|
|
bluetoothStateChanged(oldState, newState);
|
|
|
|
break;
|
2015-05-10 21:32:41 +02:00
|
|
|
case BluetoothDevice.ACTION_FOUND: {
|
2015-05-09 23:54:47 +02:00
|
|
|
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
|
|
|
short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, GBDevice.RSSI_UNKNOWN);
|
|
|
|
handleDeviceFound(device, rssi);
|
2015-05-05 00:48:02 +02:00
|
|
|
break;
|
2015-05-10 21:32:41 +02:00
|
|
|
}
|
|
|
|
case BluetoothDevice.ACTION_BOND_STATE_CHANGED: {
|
|
|
|
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
|
|
|
if (device != null && device.getAddress().equals(bondingAddress)) {
|
|
|
|
int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
|
|
|
|
if (bondState == BluetoothDevice.BOND_BONDED) {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.info("Successfully bonded with: " + bondingAddress);
|
2015-05-10 21:32:41 +02:00
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-05-09 23:54:47 +02:00
|
|
|
|
2015-11-23 23:04:46 +01:00
|
|
|
private final BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
|
2015-05-09 23:54:47 +02:00
|
|
|
@Override
|
|
|
|
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
|
|
|
|
handleDeviceFound(device, (short) rssi);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-23 23:04:46 +01:00
|
|
|
private final Runnable stopRunnable = new Runnable() {
|
2015-05-06 22:06:09 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
stopDiscovery();
|
|
|
|
}
|
|
|
|
};
|
2015-05-05 00:48:02 +02:00
|
|
|
|
|
|
|
private ProgressBar progressView;
|
|
|
|
private BluetoothAdapter adapter;
|
2015-11-23 23:04:46 +01:00
|
|
|
private final ArrayList<GBDeviceCandidate> deviceCandidates = new ArrayList<>();
|
2015-05-05 00:48:02 +02:00
|
|
|
private DeviceCandidateAdapter cadidateListAdapter;
|
|
|
|
private Button startButton;
|
2015-05-09 23:54:47 +02:00
|
|
|
private Scanning isScanning = Scanning.SCANNING_OFF;
|
2015-05-10 21:32:41 +02:00
|
|
|
private String bondingAddress;
|
2015-05-09 23:54:47 +02:00
|
|
|
|
|
|
|
private enum Scanning {
|
|
|
|
SCANNING_BT,
|
|
|
|
SCANNING_BTLE,
|
|
|
|
SCANNING_OFF
|
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
setContentView(R.layout.activity_discovery);
|
|
|
|
startButton = (Button) findViewById(R.id.discovery_start);
|
|
|
|
startButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
onStartButtonClick(startButton);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
progressView = (ProgressBar) findViewById(R.id.discovery_progressbar);
|
|
|
|
progressView.setProgress(0);
|
|
|
|
progressView.setIndeterminate(true);
|
|
|
|
progressView.setVisibility(View.GONE);
|
2015-05-12 20:09:35 +02:00
|
|
|
ListView deviceCandidatesView = (ListView) findViewById(R.id.discovery_deviceCandidatesView);
|
2015-05-05 00:48:02 +02:00
|
|
|
|
|
|
|
cadidateListAdapter = new DeviceCandidateAdapter(this, deviceCandidates);
|
|
|
|
deviceCandidatesView.setAdapter(cadidateListAdapter);
|
|
|
|
deviceCandidatesView.setOnItemClickListener(this);
|
|
|
|
|
|
|
|
IntentFilter bluetoothIntents = new IntentFilter();
|
|
|
|
bluetoothIntents.addAction(BluetoothDevice.ACTION_FOUND);
|
2015-05-10 21:32:41 +02:00
|
|
|
bluetoothIntents.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
|
2015-05-05 00:48:02 +02:00
|
|
|
bluetoothIntents.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
|
|
|
|
bluetoothIntents.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
|
|
|
|
bluetoothIntents.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
|
|
|
|
|
|
|
|
registerReceiver(bluetoothReceiver, bluetoothIntents);
|
|
|
|
|
|
|
|
startDiscovery();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onSaveInstanceState(Bundle outState) {
|
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
outState.putParcelableArrayList("deviceCandidates", deviceCandidates);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onRestoreInstanceState(Bundle savedInstanceState) {
|
|
|
|
super.onRestoreInstanceState(savedInstanceState);
|
|
|
|
ArrayList<Parcelable> restoredCandidates = savedInstanceState.getParcelableArrayList("deviceCandidates");
|
|
|
|
if (restoredCandidates != null) {
|
|
|
|
deviceCandidates.clear();
|
|
|
|
for (Parcelable p : restoredCandidates) {
|
2015-08-03 23:09:49 +02:00
|
|
|
deviceCandidates.add((GBDeviceCandidate) p);
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onStartButtonClick(View button) {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.debug("Start Button clicked");
|
2015-05-09 23:54:47 +02:00
|
|
|
if (isScanning()) {
|
2015-05-05 00:48:02 +02:00
|
|
|
stopDiscovery();
|
|
|
|
} else {
|
|
|
|
startDiscovery();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
|
|
|
unregisterReceiver(bluetoothReceiver);
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
2015-05-09 23:54:47 +02:00
|
|
|
private void handleDeviceFound(BluetoothDevice device, short rssi) {
|
2015-08-03 23:09:49 +02:00
|
|
|
GBDeviceCandidate candidate = new GBDeviceCandidate(device, rssi);
|
2015-05-09 23:54:47 +02:00
|
|
|
if (DeviceHelper.getInstance().isSupported(candidate)) {
|
|
|
|
int index = deviceCandidates.indexOf(candidate);
|
|
|
|
if (index >= 0) {
|
|
|
|
deviceCandidates.set(index, candidate); // replace
|
|
|
|
} else {
|
|
|
|
deviceCandidates.add(candidate);
|
|
|
|
}
|
|
|
|
cadidateListAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 00:48:02 +02:00
|
|
|
/**
|
2015-05-09 23:54:47 +02:00
|
|
|
* Pre: bluetooth is available, enabled and scanning is off.
|
|
|
|
* Post: BT is discovering
|
2015-05-05 00:48:02 +02:00
|
|
|
*/
|
|
|
|
private void startDiscovery() {
|
2015-05-09 23:54:47 +02:00
|
|
|
if (isScanning()) {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.warn("Not starting discovery, because already scanning.");
|
2015-05-06 22:06:09 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-05-10 00:18:42 +02:00
|
|
|
startDiscovery(Scanning.SCANNING_BT);
|
|
|
|
}
|
2015-05-06 22:06:09 +02:00
|
|
|
|
2015-05-10 00:18:42 +02:00
|
|
|
private void startDiscovery(Scanning what) {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.info("Starting discovery: " + what);
|
2015-05-10 00:18:42 +02:00
|
|
|
discoveryStarted(what); // just to make sure
|
2015-05-05 00:48:02 +02:00
|
|
|
if (ensureBluetoothReady()) {
|
2015-05-10 00:18:42 +02:00
|
|
|
if (what == Scanning.SCANNING_BT) {
|
|
|
|
startBTDiscovery();
|
2015-05-18 20:56:19 +02:00
|
|
|
} else if (what == Scanning.SCANNING_BTLE) {
|
2015-05-10 00:18:42 +02:00
|
|
|
if (GB.supportsBluetoothLE()) {
|
|
|
|
startBTLEDiscovery();
|
|
|
|
} else {
|
|
|
|
discoveryFinished();
|
|
|
|
}
|
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
} else {
|
|
|
|
discoveryFinished();
|
|
|
|
Toast.makeText(this, "Enable Bluetooth to discover devices.", Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-09 23:54:47 +02:00
|
|
|
private boolean isScanning() {
|
|
|
|
return isScanning != Scanning.SCANNING_OFF;
|
|
|
|
}
|
|
|
|
|
2015-05-05 00:48:02 +02:00
|
|
|
private void stopDiscovery() {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.info("Stopping discovery");
|
2015-05-09 23:54:47 +02:00
|
|
|
if (isScanning()) {
|
2015-05-11 00:50:18 +02:00
|
|
|
Scanning wasScanning = isScanning;
|
|
|
|
// unfortunately, we don't always get a call back when stopping the scan, so
|
|
|
|
// we do it manually; BEFORE stopping the scan!
|
|
|
|
discoveryFinished();
|
|
|
|
|
|
|
|
if (wasScanning == Scanning.SCANNING_BT) {
|
2015-05-09 23:54:47 +02:00
|
|
|
stopBTDiscovery();
|
2015-05-11 00:50:18 +02:00
|
|
|
} else if (wasScanning == Scanning.SCANNING_BTLE) {
|
2015-05-09 23:54:47 +02:00
|
|
|
stopBTLEDiscovery();
|
|
|
|
}
|
2015-05-06 22:06:09 +02:00
|
|
|
handler.removeMessages(0, stopRunnable);
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-09 23:54:47 +02:00
|
|
|
private void stopBTLEDiscovery() {
|
|
|
|
adapter.stopLeScan(leScanCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void stopBTDiscovery() {
|
|
|
|
adapter.cancelDiscovery();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void bluetoothStateChanged(int oldState, int newState) {
|
|
|
|
discoveryFinished();
|
|
|
|
startButton.setEnabled(newState == BluetoothAdapter.STATE_ON);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void discoveryFinished() {
|
|
|
|
isScanning = Scanning.SCANNING_OFF;
|
|
|
|
progressView.setVisibility(View.GONE);
|
|
|
|
startButton.setText(getString(R.string.discovery_start_scanning));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void discoveryStarted(Scanning what) {
|
|
|
|
isScanning = what;
|
|
|
|
progressView.setVisibility(View.VISIBLE);
|
|
|
|
startButton.setText(getString(R.string.discovery_stop_scanning));
|
|
|
|
}
|
|
|
|
|
2015-05-05 00:48:02 +02:00
|
|
|
private boolean ensureBluetoothReady() {
|
|
|
|
boolean available = checkBluetoothAvailable();
|
|
|
|
startButton.setEnabled(available);
|
|
|
|
if (available) {
|
|
|
|
adapter.cancelDiscovery();
|
|
|
|
// must not return the result of cancelDiscovery()
|
|
|
|
// appears to return false when currently not scanning
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean checkBluetoothAvailable() {
|
|
|
|
BluetoothManager bluetoothService = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
|
|
|
|
if (bluetoothService == null) {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.warn("No bluetooth available");
|
2015-05-05 00:48:02 +02:00
|
|
|
this.adapter = null;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
BluetoothAdapter adapter = bluetoothService.getAdapter();
|
|
|
|
if (!adapter.isEnabled()) {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.warn("Bluetooth not enabled");
|
2015-05-05 00:48:02 +02:00
|
|
|
this.adapter = null;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this.adapter = adapter;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-09 23:54:47 +02:00
|
|
|
private void startBTLEDiscovery() {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.info("Starting BTLE Discovery");
|
2015-05-06 22:06:09 +02:00
|
|
|
handler.removeMessages(0, stopRunnable);
|
2015-05-10 23:14:32 +02:00
|
|
|
handler.sendMessageDelayed(getPostMessage(stopRunnable), SCAN_DURATION);
|
2015-05-05 00:48:02 +02:00
|
|
|
adapter.startLeScan(leScanCallback);
|
|
|
|
}
|
|
|
|
|
2015-05-09 23:54:47 +02:00
|
|
|
private void startBTDiscovery() {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.info("Starting BT Discovery");
|
2015-05-09 23:54:47 +02:00
|
|
|
handler.removeMessages(0, stopRunnable);
|
2015-05-10 23:14:32 +02:00
|
|
|
handler.sendMessageDelayed(getPostMessage(stopRunnable), SCAN_DURATION);
|
2015-05-09 23:54:47 +02:00
|
|
|
adapter.startDiscovery();
|
|
|
|
}
|
|
|
|
|
2015-05-10 23:14:32 +02:00
|
|
|
private Message getPostMessage(Runnable runnable) {
|
|
|
|
Message m = Message.obtain(handler, runnable);
|
|
|
|
m.obj = runnable;
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2015-05-05 00:48:02 +02:00
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
2015-08-03 23:09:49 +02:00
|
|
|
GBDeviceCandidate deviceCandidate = deviceCandidates.get(position);
|
2015-05-05 00:48:02 +02:00
|
|
|
if (deviceCandidate == null) {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.error("Device candidate clicked, but item not found");
|
2015-05-05 00:48:02 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-10 21:35:31 +02:00
|
|
|
stopDiscovery();
|
2015-05-05 00:48:02 +02:00
|
|
|
DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(deviceCandidate);
|
2015-05-10 16:27:31 +02:00
|
|
|
Class<? extends Activity> pairingActivity = coordinator.getPairingActivity();
|
|
|
|
if (pairingActivity != null) {
|
|
|
|
Intent intent = new Intent(this, pairingActivity);
|
|
|
|
intent.putExtra(DeviceCoordinator.EXTRA_DEVICE_MAC_ADDRESS, deviceCandidate.getMacAddress());
|
|
|
|
startActivity(intent);
|
2015-05-18 20:56:19 +02:00
|
|
|
} else {
|
2015-05-10 16:27:31 +02:00
|
|
|
try {
|
|
|
|
BluetoothDevice btDevice = adapter.getRemoteDevice(deviceCandidate.getMacAddress());
|
2015-05-10 21:32:41 +02:00
|
|
|
if (btDevice.createBond()) {
|
|
|
|
// async, wait for bonding event to finish this activity
|
|
|
|
bondingAddress = btDevice.getAddress();
|
|
|
|
}
|
2015-05-10 16:27:31 +02:00
|
|
|
} catch (Exception e) {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.error("Error pairing device: " + deviceCandidate.getMacAddress());
|
2015-05-10 16:27:31 +02:00
|
|
|
}
|
|
|
|
}
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
|
|
|
}
|