Skip to content

Commit

Permalink
Updated to v2.9.1
Browse files Browse the repository at this point in the history
Source code updated to reflect version 2.9.1 of the application.

* Fixed app dropping connections as soon as it lost focus
* Changed instances of BLE to Bluetooth low energy
  • Loading branch information
perssonmagnus committed Dec 1, 2020
1 parent 9d80294 commit aef730e
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 51 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Android-u-blox-BLE
# Android-u-blox-Bluetooth-Low-Energy
The u-blox Bluetooth low energy Android app allows developers to evaluate the stand-alone Bluetooth low energy modules from u-blox.

https://www.u-blox.com/
Expand All @@ -7,9 +7,9 @@ https://www.u-blox.com/

#

These files are all working with Android Studio 3.2.1 and with SDK version 28.
These files are all working with Android Studio 4.1 and with SDK version 29.

Needed files to get BLE working in your app:
Needed files to get Bluetooth low energy working in your app:
```
\services\BluetoothLeService.java
\utils\BLEQueue.java
Expand All @@ -29,7 +29,7 @@ In the application tag you must have:
<service android:name="com.[yourcompany].[yourapp].services.BluetoothLeService" android:enabled="true" />
```

For a simple and striped down Activity see BasicBLEActivity.java, here we have the simplest way of connecting to the service to be able to connect, read, write and to get notifications from a BLE device like NINA-B1. This Activity is well documented to set you started in no time.
For a simple and striped down Activity see BasicBLEActivity.java, here we have the simplest way of connecting to the service to be able to connect, read, write and to get notifications from a Bluetooth low energy device like NINA-B1. This Activity is well documented to set you started in no time.

## Disclaimer
Copyright (C) u-blox
Expand Down
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ android {
signingConfigs {

}
compileSdkVersion 28
compileSdkVersion 29
buildToolsVersion '26.0.3'
defaultConfig {
applicationId "com.ublox.BLE"
minSdkVersion 18
targetSdkVersion 28
versionCode 18
versionName "2.9.0"
targetSdkVersion 29
versionCode 19
versionName "2.9.1"
vectorDrawables.useSupportLibrary = true

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-feature
android:name="android.hardware.bluetooth_le"
Expand Down
36 changes: 24 additions & 12 deletions app/src/main/java/com/ublox/BLE/activities/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class MainActivity extends Activity implements ActionBar.TabListener,

private boolean needToWaitForMtuUpdate = false;
private boolean isFirstTimeToSetFifo = true;
private boolean mRestoreConnectionOnResume = true;

private TextView tvStatus;
private RelativeLayout rlProgress;
Expand Down Expand Up @@ -348,29 +349,38 @@ private void updateStatus() {
}

@Override
protected void onResume() {
super.onResume();
protected void onStart() {
super.onStart();

if (mBluetoothLeService != null) {
mBluetoothLeService.register(mGattUpdateReceiver);
final boolean result = mBluetoothLeService.connect(mDevice);
Log.d(TAG, "Connect request result=" + result);
mConnectionState = ConnectionState.CONNECTING;
invalidateOptionsMenu();
updateStatus();
rlProgress.setVisibility(View.VISIBLE);
}
invalidateOptionsMenu();
}

@Override
protected void onPause() {
super.onPause();
protected void onDestroy() {
super.onDestroy();
try {
mBluetoothLeService.disconnect();
mBluetoothLeService.close();
mConnectionState = ConnectionState.DISCONNECTED;
mBluetoothLeService.unregister();
} catch (Exception ignore) {}
} catch (Exception ignored){}
}

@Override
protected void onResume() {
super.onResume();

if (mConnectionState != ConnectionState.CONNECTED && mRestoreConnectionOnResume) {
boolean connectionStart = mBluetoothLeService.connect(mDevice);
mConnectionState = connectionStart
? ConnectionState.CONNECTING
: ConnectionState.DISCONNECTED;
rlProgress.setVisibility(connectionStart ? View.VISIBLE : View.INVISIBLE);
updateStatus();
}

invalidateOptionsMenu();
}

Expand Down Expand Up @@ -499,13 +509,15 @@ public boolean onCreateOptionsMenu(Menu menu) {
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_connect:
mRestoreConnectionOnResume = true;
mBluetoothLeService.connect(mDevice);
mConnectionState = ConnectionState.CONNECTING;
invalidateOptionsMenu();
updateStatus();
rlProgress.setVisibility(View.VISIBLE);
return true;
case R.id.menu_disconnect:
mRestoreConnectionOnResume = false;
mBluetoothLeService.disconnect();
updateStatus();
rlProgress.setVisibility(View.VISIBLE);
Expand Down
57 changes: 31 additions & 26 deletions app/src/main/java/com/ublox/BLE/bluetooth/BluetoothScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,38 @@ public class BluetoothScanner implements BluetoothCentral, BluetoothAdapter.LeSc

public BluetoothScanner(BluetoothAdapter adapter) {
this.adapter = adapter;
if (!usingDeprecated()) {
scanner = adapter.getBluetoothLeScanner();
callback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
Map<UUID, byte[]> services = new HashMap<>();
List<ParcelUuid> serviceUuids = result.getScanRecord().getServiceUuids();
if (serviceUuids != null) {
for (ParcelUuid puuid : serviceUuids) {
services.put(puuid.getUuid(), new byte[0]);
}
}
Map<ParcelUuid, byte[]> serviceData = result.getScanRecord().getServiceData();
for (ParcelUuid puuid : serviceData.keySet()) {
services.put(puuid.getUuid(),serviceData.get(puuid));
state = State.ON;
foundPeripherals = new ArrayList<>();
}

@TargetApi(21)
private BluetoothLeScanner Scanner() {
if (scanner != null) return scanner;

scanner = adapter.getBluetoothLeScanner();
callback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
Map<UUID, byte[]> services = new HashMap<>();
List<ParcelUuid> serviceUuids = result.getScanRecord().getServiceUuids();
if (serviceUuids != null) {
for (ParcelUuid puuid : serviceUuids) {
services.put(puuid.getUuid(), new byte[0]);
}
onPeripheralScan(result.getDevice(), result.getRssi(), services);
}

@Override
public void onScanFailed(int errorCode) {
setState(State.ON);
Map<ParcelUuid, byte[]> serviceData = result.getScanRecord().getServiceData();
for (ParcelUuid puuid : serviceData.keySet()) {
services.put(puuid.getUuid(),serviceData.get(puuid));
}
};
}
state = State.ON;
foundPeripherals = new ArrayList<>();
onPeripheralScan(result.getDevice(), result.getRssi(), services);
}

@Override
public void onScanFailed(int errorCode) {
setState(State.ON);
}
};
return scanner;
}

@Override
Expand Down Expand Up @@ -85,7 +90,7 @@ public void scan(List<UUID> withServices) {
} else {
ScanSettings.Builder settings = new ScanSettings.Builder();
if (notLegacy()) settings.setLegacy(false);
scanner.startScan(filtersFrom(withServices), settings.build(), callback);
Scanner().startScan(filtersFrom(withServices), settings.build(), callback);
setState(State.SCANNING);
}
}
Expand All @@ -107,7 +112,7 @@ public void stop() {
if (usingDeprecated()) {
adapter.stopLeScan(this);
} else {
scanner.stopScan(callback);
Scanner().stopScan(callback);
}
setState(State.ON);
}
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<resources>
<string name="app_name">u-blox BLE</string>
<string name="app_name">u-blox Bluetooth Low Energy</string>

<string name="ble_not_supported">BLE is not supported</string>
<string name="ble_not_supported">Bluetooth Low Energy is not supported</string>
<string name="label_data">Data:</string>
<string name="label_device_address">Device address:</string>
<string name="label_state">State:</string>
<string name="no_data">No data</string>
<string name="connected">Connected</string>
<string name="disconnected">Disconnected</string>
<string name="error_bluetooth_not_supported">Bluetooth not supported.</string>
<string name="error_bluetooth_not_supported">Bluetooth is not supported.</string>

<string name="unknown_device">Unknown device</string>
<string name="unknown_characteristic">Unknown characteristic</string>
Expand All @@ -27,7 +27,7 @@
<string name="title_section4">Test</string>"

<string name="overview_temperature">Temperature (°C)</string>
<string name="title_activity_basic_ble">BasicBLEActivity</string>
<string name="title_activity_basic_ble">BasicBluetoothLowEnergyActivity</string>

<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
Expand Down

0 comments on commit aef730e

Please sign in to comment.