Doc updates, add "wantScan" to BleConnect.java so scan starts

automatically upon grant of permissions or device enable.
This commit is contained in:
Greg 2019-11-27 11:46:04 -05:00
parent 7223b8c380
commit 2af636b946
3 changed files with 33 additions and 3 deletions

BIN
Ble.jar

Binary file not shown.

View File

@ -31,6 +31,7 @@ public class BleConnect extends Fragment {
BluetoothManager manager;
BluetoothAdapter adapter;
Handler handler;
boolean wantScan = false;
final int PERMISSION_REQUEST = 1;
@ -45,13 +46,11 @@ public class BleConnect extends Fragment {
Log.d("gio", "BleConnect: onAttach()");
ctx.registerReceiver(receiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
manager = (BluetoothManager) ctx.getSystemService(ctx.BLUETOOTH_SERVICE);
Log.d("gio", "BleUtil Enable: adapter");
adapter = manager.getAdapter();
handler = new Handler(ctx.getMainLooper());
if (!enabled()) {
Log.d("gio", "BleConnect: enabling adapter");
Intent enableBtIntent = new Intent(adapter.ACTION_REQUEST_ENABLE);
Log.d("gio", "BleConnect: handler.post");
handler.post(new Runnable() {
public void run() {
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
@ -96,9 +95,13 @@ public class BleConnect extends Fragment {
};
public void scan() {
if (!enabled()) {
if (!enabled() || getContext().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.d("gio","BleConnect: scan() not enabled or no permissions, wantScan = true");
wantScan = true;
return;
}
Log.d("gio","BleConnect: scan() starting scan");
wantScan = false;
handler.post(new Runnable() {
public void run() {
adapter.startLeScan(scanCallback);
@ -218,6 +221,9 @@ public class BleConnect extends Fragment {
switch (resultCode) {
case Activity.RESULT_OK: {
Log.d("gio", "BleConnect: onActivityResult() -- OK");
if (wantScan) {
scan();
}
break;
}
case Activity.RESULT_CANCELED: {
@ -228,6 +234,28 @@ public class BleConnect extends Fragment {
}
}
@Override
public void onRequestPermissionsResult (int requestCode, String[] permissions, int[] grantResults) {
Log.d("gio", "BleConnect: onRequestPermissionsResult");
if (requestCode == PERMISSION_REQUEST) {
boolean granted = true;
for (int x : grantResults) {
if (x == PackageManager.PERMISSION_DENIED) {
granted = false;
break;
}
}
if (!granted) {
Log.d("gio", "BleConnect: permissions not granted");
return;
}
Log.d("gio", "BleConnect: permissions granted");
if (wantScan) {
scan();
}
}
}
static private native void installComplete(BleConnect p);
static private native void updateState(int s);
static private native void onScan(String name, String id, int rssi, BluetoothDevice dev);

View File

@ -52,3 +52,5 @@ Using Gio, your main event loop will look something like this:
}
}
```
An example heart rate monitor app can be found at [git.wow.st/gmp/hrm](https://git.wow.st/gmp/hrm).