summaryrefslogtreecommitdiff
path: root/Defogger/src/no/mork/android/defogger/ScannerActivity.java
blob: 5ec114f17ba4d71327b445f069cbecc0cda7cd05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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 {

    // 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);
        }
    }

}