summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjørn Mork <bjorn@mork.no>2019-05-20 14:08:57 +0200
committerBjørn Mork <bjorn@mork.no>2019-05-20 14:08:57 +0200
commitd358cef14f81375fad37fb4ac46b740e5e80db57 (patch)
treee62d1c62f9ebe229209536ec06f2f8e077064c40
parent9104f399e0f79136549a001a06d5b98fac03c797 (diff)
android wip: building
Signed-off-by: Bjørn Mork <bjorn@mork.no>
-rw-r--r--Defogger/AndroidManifest.xml5
-rw-r--r--Defogger/src/no/mork/android/defogger/MainActivity.java65
-rw-r--r--Defogger/src/no/mork/android/defogger/ScannerActivity.java80
3 files changed, 124 insertions, 26 deletions
diff --git a/Defogger/AndroidManifest.xml b/Defogger/AndroidManifest.xml
index 686ca8b..e09de9d 100644
--- a/Defogger/AndroidManifest.xml
+++ b/Defogger/AndroidManifest.xml
@@ -7,12 +7,13 @@
<!-- minimum required for EC signing key support -->
<uses-sdk a:minSdkVersion="18" />
- <!-- not yet...
<uses-permission a:name="android.permission.BLUETOOTH"/>
<uses-permission a:name="android.permission.BLUETOOTH_ADMIN"/>
+ <!-- not yet...
<uses-permission a:name="android.permission.ACCESS_FINE_LOCATION"/>
-->
-
+ <uses-feature a:name="android.hardware.bluetooth_le" a:required="true"/>
+
<application a:label="Defogger">
<activity a:name="no.mork.android.defogger.MainActivity">
<intent-filter>
diff --git a/Defogger/src/no/mork/android/defogger/MainActivity.java b/Defogger/src/no/mork/android/defogger/MainActivity.java
index 69682ad..e3c1de3 100644
--- a/Defogger/src/no/mork/android/defogger/MainActivity.java
+++ b/Defogger/src/no/mork/android/defogger/MainActivity.java
@@ -1,7 +1,11 @@
package no.mork.android.defogger;
import android.app.Activity;
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothManager;
+import android.content.Context;
import android.content.Intent;
+import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
@@ -10,6 +14,10 @@ import android.widget.TextView;
import no.mork.android.defogger.ScannerActivity;
public class MainActivity extends Activity {
+
+ private static final int REQUEST_ENABLE_BT = 0x1042;
+ private BluetoothAdapter bluetoothAdapter;
+
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -18,30 +26,65 @@ public class MainActivity extends Activity {
Button start_scan = (Button) findViewById(R.id.start_scan);
// button2 = (Button) findViewById(R.id.button2);
-
start_scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), ScannerActivity.class);
- startActivityForResult(intent, 1);
+ startActivityForResult(intent, R.id.hello_text);
}
});
}
@Override
+ protected void onResume() {
+ super.onResume();
+
+ getBluetoothAdapter();
+ }
+
+
+ @Override
protected void onActivityResult(int requestCode, int resultCode, Intent dataIntent) {
super.onActivityResult(requestCode, resultCode, dataIntent);
+ switch (requestCode) {
+ case REQUEST_ENABLE_BT:
+ if (resultCode != RESULT_OK) { // user refused to enable BT?
+ // logError("BT disabled.");
+ finish();
+ }
+
+ break;
+ default:
+ TextView hello_text = (TextView) findViewById(requestCode);
+ String messageReturn = resultCode == RESULT_OK ? dataIntent.getStringExtra("scan_ret") : "not OK";
+ hello_text.setText(messageReturn);
+ }
+ }
- switch (requestCode)
- {
- case 1:
- TextView hello_text = (TextView) findViewById(R.id.hello_text);
- if(resultCode == RESULT_OK)
- {
- String messageReturn = dataIntent.getStringExtra("scan_ret");
- hello_text.setText(messageReturn);
- }
+ // find and enable a bluetooth adapter with LE support
+ protected void getBluetoothAdapter() {
+ final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
+ bluetoothAdapter = bluetoothManager.getAdapter();
+
+
+ // Bluetooth is not supported?
+ if (bluetoothAdapter == null) {
+ // logError("BT unsupported.");
+ finish();
+ }
+
+ // Check low energy support
+ if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
+ // Get a newer device
+ // logError("No LE Support.");
+ finish();
}
+
+ // Request user permission to enable Bluetooth.
+ if (!bluetoothAdapter.isEnabled()) {
+ Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
+ startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
+ }
}
}
diff --git a/Defogger/src/no/mork/android/defogger/ScannerActivity.java b/Defogger/src/no/mork/android/defogger/ScannerActivity.java
index 72b72bd..5ec114f 100644
--- a/Defogger/src/no/mork/android/defogger/ScannerActivity.java
+++ b/Defogger/src/no/mork/android/defogger/ScannerActivity.java
@@ -1,23 +1,77 @@
package no.mork.android.defogger;
import android.app.Activity;
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
+import android.os.Handler;
+//import android.widget.ArrayAdapter;
import android.widget.Toast;
+//class LeDeviceListAdapter extends ArrayAdapter {
+// protected void addDevice(final BluetoothDevice device);
+//}
+
public class ScannerActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //setContentView(R.layout.scanning);
-
- CharSequence text = "Hello toast!";
-
- Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
- Intent intent = new Intent();
- intent.putExtra("scan_ret", "This data is returned when scan activity is finished.");
- setResult(RESULT_OK, intent);
- finish();
- }
+
+ // Stops scanning after 10 seconds.
+ private static final long SCAN_PERIOD = 10000;
+ private BluetoothAdapter bluetoothAdapter;
+ private boolean mScanning;
+ private Handler handler;
+ // private LeDeviceListAdapter leDeviceListAdapter;
+ private BluetoothAdapter.LeScanCallback leScanCallback;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ //setContentView(R.layout.scanning);
+
+ bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
+
+ leScanCallback = new BluetoothAdapter.LeScanCallback() {
+
+ @Override
+ public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
+ runOnUiThread(new Runnable() {
+
+ @Override
+ public void run() {
+ // leDeviceListAdapter.addDevice(device);
+ // leDeviceListAdapter.notifyDataSetChanged();
+ }
+ });
+ }
+ };
+
+ CharSequence text = "Hello toast!";
+
+ Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
+ Intent intent = new Intent();
+ intent.putExtra("scan_ret", "This data is returned when scan activity is finished.");
+ setResult(RESULT_OK, intent);
+ finish();
+ }
+
+ protected void scanForCamera(final boolean enable) {
+ if (enable) {
+ // Stops scanning after a pre-defined scan period.
+ handler.postDelayed(new Runnable() {
+ @Override
+ public void run() {
+ mScanning = false;
+ bluetoothAdapter.stopLeScan(leScanCallback);
+ }
+ }, SCAN_PERIOD);
+
+ mScanning = true;
+ bluetoothAdapter.startLeScan(leScanCallback);
+ } else {
+ mScanning = false;
+ bluetoothAdapter.stopLeScan(leScanCallback);
+ }
+ }
+
}