Skip to content

Commit

Permalink
BLE: termux-bluetooth-scaninfo now can support bluetooth low energy scan
Browse files Browse the repository at this point in the history
Signed-off-by: Steven Salazar <stevensalazarmolina@gmail.com>
  • Loading branch information
StevenSalazarM committed Aug 2, 2020
1 parent 4833040 commit d9cb3da
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ Terminal emulators
* xterm: The grandfather of terminal emulators. [Source](http://invisible-island.net/datafiles/release/xterm.tar.gz).
* Connectbot: Android SSH client. [Source](https://github.com/connectbot/connectbot)
* Android Terminal Emulator: Android terminal app which Termux terminal handling is based on. Inactive. [Source](https://github.com/jackpal/Android-Terminal-Emulator).

Additional Features
==================
This repository is maily an extesion of the Termux Project that allows the usage of bluetooth commands from console:
* termux-bluetooth-scaninfo: The first time termux will ask for permissions if needed, the second time you type this command it will start the scanning of devices (BLE is also supported under the dev-ble branch, however it hasn't been merged due to the lack of LE devices for testing), the third time you type this command it will stop the scanner.
* termux-bluetooth-connect: This command should allows to connect to a device, however it hasnt been implemented yet, feel free to insert your own code under the [BluetoothAPI](https://github.com/StevenSalazarM/termux-app-bluetooth/blob/master/app/src/main/java/com/termux/api/BluetoothAPI.java#L106).
2 changes: 1 addition & 1 deletion app/src/main/java/com/termux/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void minimizeApp() {
@Override
protected void onDestroy() {
TermuxActivity.first_activity_options=false;
BluetoothAPI.bluetoothStopScanning();
//BluetoothAPI.bluetoothStopScanning();
super.onDestroy();
}
}
1 change: 1 addition & 0 deletions app/src/main/java/com/termux/api/BluetoothAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class BluetoothAPI {
private static Set<String> deviceList = new HashSet<String>();
public static boolean unregistered = true;
public static BluetoothAdapter mBluetoothAdapter;

// Create a BroadcastReceiver for ACTION_FOUND.
private static BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Expand Down
126 changes: 126 additions & 0 deletions app/src/main/java/com/termux/api/BluetoothLowEnergyAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package com.termux.api;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.util.JsonWriter;
import android.util.Log;

import com.termux.MainActivity;
import com.termux.api.util.ResultReturner;

import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;

public class BluetoothLowEnergyAPI {

private static boolean scanning = false;
private static Set<String> deviceList = new HashSet<String>();
public static BluetoothManager btManager;
public static BluetoothAdapter btAdapter;
public static BluetoothLeScanner btScanner;
public final static int REQUEST_ENABLE_BT = 1;

// Device scan callback.
private static ScanCallback leScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
BluetoothDevice device = result.getDevice();
if (device != null) {
String deviceName = device.getName();
// you can get more info from device here
// ...

if (deviceName != null && !deviceName.equals("null") && !deviceName.trim().equals("")) {
deviceList.add(deviceName);

}
}

}
};


static void onReceiveBluetoothScanInfo(TermuxApiReceiver apiReceiver, final Context context, final Intent intent) {

ResultReturner.returnData(apiReceiver, intent, new ResultReturner.ResultJsonWriter() {


@Override
public void writeJson(final JsonWriter out) throws Exception {
out.beginObject();
if(!scanning) {
if(btManager==null) {
// stuff for BLE, you can move this in a init function inside BluetoothLowEnergyAPI
btManager = (BluetoothManager) MainActivity.activity.getSystemService(Context.BLUETOOTH_SERVICE);
if(btManager == null) Log.d("termux-bl","MANAGER IS NULL");
btAdapter = btManager.getAdapter();
if(btAdapter == null) Log.d("termux-bl","ADAPTER IS NULL");
btScanner = btAdapter.getBluetoothLeScanner();
if(btScanner == null) Log.d("termux-bl","SCANNER IS NULL");
}

// start scanning with a broadcast receiver
out.name("message").value("scanning ble devices, please type termux-bluetooth-scaninfo again to stop the scanning and print the results");
scanning = true;
deviceList.clear();
System.out.println("start scanning");
AsyncTask.execute(new Runnable() {
@Override
public void run() {
btScanner.startScan(leScanCallback);
}
});
}
else{
System.out.println("stop scanning");
AsyncTask.execute(new Runnable() {
@Override
public void run() {
btScanner.stopScan(leScanCallback);
}
});
for(String s: deviceList)
out.name("device").value(s);
scanning =false;
}
out.endObject();

}
});
}


static void onReceiveBluetoothConnect(TermuxApiReceiver apiReceiver, final Context context, final Intent intent) {
// To be implemented, the idea is to receive the command 'termux-bluetooth-connect args'
// be careful to do not send only termux-bluetooth-connect without args because the termux-api-package didnt control 0 args so it will block
ResultReturner.returnData(apiReceiver, intent,new ResultReturner.WithStringInput(){
@Override
public void writeResult(PrintWriter out) throws Exception {
try {
JsonWriter writer = new JsonWriter(out);
writer.setIndent(" ");

if(inputString.equals("")) {

writer.beginObject().name("message:").value("invalid input").endObject();
}else
writer.beginObject().name("message:").value("BluetoothConnect to be implemented, it should connect to " + inputString).endObject();
out.println(); // To add trailing newline.
}catch(Exception e){
Log.d("Except BluetoothConnect", e.getMessage());

}
}
});
}
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/termux/api/TermuxApiReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ public void onReceive(Context context, Intent intent) {
break;
case "BluetoothScanInfo":
if (TermuxApiPermissionActivity.checkAndRequestPermissions(context, intent, Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.BLUETOOTH, Manifest.permission.ACCESS_FINE_LOCATION)) {
BluetoothAPI.onReceiveBluetoothScanInfo(this, context, intent);
BluetoothLowEnergyAPI.onReceiveBluetoothScanInfo(this, context, intent);
}
break;
case "BluetoothConnect":
if (TermuxApiPermissionActivity.checkAndRequestPermissions(context, intent, Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.BLUETOOTH, Manifest.permission.ACCESS_FINE_LOCATION)) {
BluetoothAPI.onReceiveBluetoothConnect(this, context, intent);
BluetoothLowEnergyAPI.onReceiveBluetoothConnect(this, context, intent);
}
break;
case "CameraInfo":
Expand Down

0 comments on commit d9cb3da

Please sign in to comment.