From d358cef14f81375fad37fb4ac46b740e5e80db57 Mon Sep 17 00:00:00 2001 From: Bjørn Mork Date: Mon, 20 May 2019 14:08:57 +0200 Subject: android wip: building MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Bjørn Mork --- Defogger/AndroidManifest.xml | 5 +- .../src/no/mork/android/defogger/MainActivity.java | 65 +++++++++++++++--- .../no/mork/android/defogger/ScannerActivity.java | 80 ++++++++++++++++++---- 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 @@ - - + + 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); + } + } + } -- cgit v1.2.3