-
Notifications
You must be signed in to change notification settings - Fork 471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add bluetooth scanning #686
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package com.termux.api.apis; | ||
|
||
import android.bluetooth.BluetoothAdapter; | ||
import android.bluetooth.BluetoothDevice; | ||
import android.content.BroadcastReceiver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.IntentFilter; | ||
import android.util.JsonWriter; | ||
|
||
import com.termux.api.util.ResultReturner; | ||
import com.termux.shared.logger.Logger; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Objects; | ||
|
||
public class BluetoothScanAPI { | ||
|
||
private static final ArrayList<BluetoothDevice> devices = new ArrayList<>(); | ||
private static final String LOG_TAG = "BluetoothScanAPI"; | ||
private static BluetoothAdapter adapter; | ||
private static boolean scanning = false; | ||
|
||
private static final BroadcastReceiver receiver = new BroadcastReceiver() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please give this a better name, even though it's the only |
||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
if (Objects.equals(intent.getAction(), BluetoothDevice.ACTION_FOUND)) { | ||
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); | ||
if (device != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason we'd expect an |
||
devices.add(device); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
public static void onReceive(final Context context, final Intent intent) { | ||
Logger.logDebug(LOG_TAG, "onReceive"); | ||
|
||
ResultReturner.returnData(context, intent, new ResultReturner.ResultJsonWriter() { | ||
@Override | ||
public void writeJson(JsonWriter out) throws Exception { | ||
String mode = intent.getStringExtra("mode"); | ||
if (mode == null) return; | ||
out.beginObject(); | ||
switch (mode.toLowerCase()) { | ||
case "start": | ||
if (start(context)) { | ||
out.name("error").value(false); | ||
} else { | ||
out.name("error").value(true); | ||
out.name("reason").value("Already running!"); | ||
Comment on lines
+50
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of having two keys, how about |
||
} | ||
break; | ||
case "stop": | ||
if (stop(context)) { | ||
out.name("error").value(false); | ||
printList(out, true); | ||
} else { | ||
out.name("error").value(true); | ||
out.name("reason").value("Already stopped!"); | ||
} | ||
break; | ||
case "info": | ||
if (scanning) { | ||
out.name("error").value(false); | ||
printList(out, false); | ||
} else { | ||
out.name("error").value(true); | ||
out.name("reason").value("Scan is not running!"); | ||
} | ||
break; | ||
default: | ||
out.name("error").value(true); | ||
out.name("reason").value("Invalid option! Choose one from [start, stop, info]"); | ||
break; | ||
} | ||
out.endObject(); | ||
} | ||
}); | ||
|
||
|
||
} | ||
|
||
private static void printList(JsonWriter out, boolean clear) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: This method doesn't actually print. (If you accept my suggested API change, you should remove |
||
try { | ||
out.name("devices"); | ||
out.beginArray(); | ||
for (BluetoothDevice device : devices) { | ||
out.beginObject(); | ||
out.name("name").value(device.getName()); | ||
out.name("address").value(device.getAddress()); | ||
out.endObject(); | ||
} | ||
out.endArray(); | ||
if (clear) { | ||
devices.clear(); | ||
} | ||
} catch (IOException ex) { | ||
Logger.logError(Arrays.toString(ex.getStackTrace())); | ||
} | ||
} | ||
|
||
private static boolean start(Context context) { | ||
if (scanning) return false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mega nit: |
||
context.getApplicationContext().registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); | ||
adapter = BluetoothAdapter.getDefaultAdapter(); | ||
adapter.startDiscovery(); | ||
scanning = true; | ||
return true; | ||
} | ||
|
||
private static boolean stop(Context context) { | ||
if (!scanning) return false; | ||
adapter.cancelDiscovery(); | ||
context.getApplicationContext().unregisterReceiver(receiver); | ||
scanning = false; | ||
return true; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's strictly necessary to track and return last-seen timestamps. It also looks like the scan API will trigger multiple times for the same devices as it's repeatedly discovered, and a longer scan with the code would result in multiple copies of the same device in this array and the returned output.
This seems like it should be
HashMap<BluetoothDevice, long>
.