Removed tests, added AndroidX explicitly and added callback interface

This commit is contained in:
2019-04-02 18:48:18 +02:00
parent 2138c42ee0
commit 3d36a50832
9 changed files with 152 additions and 78 deletions

View File

@@ -2,6 +2,7 @@ apply plugin: 'com.android.application'
android { android {
compileSdkVersion 28 compileSdkVersion 28
defaultConfig { defaultConfig {
applicationId "com.example.nrftest" applicationId "com.example.nrftest"
minSdkVersion 28 minSdkVersion 28
@@ -24,9 +25,7 @@ android {
dependencies { dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs') implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'androidx.annotation:annotation:1.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'androidx.appcompat:appcompat:1.0.0'
testImplementation 'junit:junit:4.12' implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
} }

View File

@@ -1,26 +0,0 @@
package com.example.nrftest;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("com.example.nrftest", appContext.getPackageName());
}
}

View File

@@ -10,61 +10,67 @@ public class DecaCallbacks implements DecaManagerCallbacks {
@Override @Override
public void onDeviceConnecting(@NonNull BluetoothDevice device) { public void onDeviceConnecting(@NonNull BluetoothDevice device) {
Log.d(TAG, "onDeviceConnecting"); Log.d("DecaCallbacks", "onDeviceConnecting");
} }
@Override @Override
public void onDeviceConnected(@NonNull BluetoothDevice device) { public void onDeviceConnected(@NonNull BluetoothDevice device) {
Log.d(TAG, "onDeviceConnected"); Log.d("DecaCallbacks", "onDeviceConnected");
} }
@Override @Override
public void onDeviceDisconnecting(@NonNull BluetoothDevice device) { public void onDeviceDisconnecting(@NonNull BluetoothDevice device) {
Log.d(TAG, "onDeviceDisconnecting"); Log.d("DecaCallbacks", "onDeviceDisconnecting");
} }
@Override @Override
public void onDeviceDisconnected(@NonNull BluetoothDevice device) { public void onDeviceDisconnected(@NonNull BluetoothDevice device) {
Log.d(TAG, "onDeviceDisconnected"); Log.d("DecaCallbacks", "onDeviceDisconnected");
} }
@Override @Override
public void onLinkLossOccurred(@NonNull BluetoothDevice device) { public void onLinkLossOccurred(@NonNull BluetoothDevice device) {
Log.d(TAG, "onLinkLossOccurred"); Log.d("DecaCallbacks", "onLinkLossOccurred");
} }
@Override @Override
public void onServicesDiscovered(@NonNull BluetoothDevice device, boolean optionalServicesFound) { public void onServicesDiscovered(@NonNull BluetoothDevice device, boolean optionalServicesFound) {
Log.d(TAG, "onServicesDiscovered"); Log.d("DecaCallbacks", "onServicesDiscovered");
} }
@Override @Override
public void onDeviceReady(@NonNull BluetoothDevice device) { public void onDeviceReady(@NonNull BluetoothDevice device) {
Log.d(TAG, "onDeviceReady"); Log.d("DecaCallbacks", "onDeviceReady");
} }
@Override @Override
public void onBondingRequired(@NonNull BluetoothDevice device) { public void onBondingRequired(@NonNull BluetoothDevice device) {
Log.d(TAG, "onBondingRequired"); Log.d("DecaCallbacks", "onBondingRequired");
} }
@Override @Override
public void onBonded(@NonNull BluetoothDevice device) { public void onBonded(@NonNull BluetoothDevice device) {
Log.d(TAG, "onBonded"); Log.d("DecaCallbacks", "onBonded");
} }
@Override @Override
public void onBondingFailed(@NonNull BluetoothDevice device) { public void onBondingFailed(@NonNull BluetoothDevice device) {
Log.d(TAG, "onBondingFailed"); Log.d("DecaCallbacks", "onBondingFailed");
} }
@Override @Override
public void onError(@NonNull BluetoothDevice device, @NonNull String message, int errorCode) { public void onError(@NonNull BluetoothDevice device, @NonNull String message, int errorCode) {
Log.d(TAG, "onError: " + message); Log.d("DecaCallbacks", "onError: " + message);
} }
@Override @Override
public void onDeviceNotSupported(@NonNull BluetoothDevice device) { public void onDeviceNotSupported(@NonNull BluetoothDevice device) {
Log.d(TAG, "onDeviceNotSupported"); Log.d("DecaCallbacks", "onDeviceNotSupported");
}
@Override
public void onTagLocationData(String dataStr) {
} }
} }

View File

@@ -7,6 +7,7 @@ import android.bluetooth.BluetoothGattService;
import android.content.Context; import android.content.Context;
import android.util.Log; import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.util.UUID; import java.util.UUID;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@@ -87,6 +88,14 @@ public class DecaManager extends BleManager<DecaManagerCallbacks> {
@Override @Override
public void onDataReceived(@NonNull BluetoothDevice device, @NonNull Data data) { public void onDataReceived(@NonNull BluetoothDevice device, @NonNull Data data) {
Log.d(TAG, "onDataReceived: length=" + data.size() + " data=" + data); Log.d(TAG, "onDataReceived: length=" + data.size() + " data=" + data);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
for (int i = 0; i < data.size(); i++) {
stream.write(data.getByte(i));
}
mCallbacks.onTagLocationData(stream.toByteArray());
} }
@Override @Override

View File

@@ -2,6 +2,7 @@ package com.example.nrftest;
import no.nordicsemi.android.ble.BleManagerCallbacks; import no.nordicsemi.android.ble.BleManagerCallbacks;
public interface DecaManagerCallbacks extends BleManagerCallbacks public interface DecaManagerCallbacks extends BleManagerCallbacks {
{
void onTagLocationData(byte[] dataStr);
} }

View File

@@ -10,9 +10,12 @@ import android.bluetooth.le.ScanSettings;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import java.util.List; import java.util.List;
@@ -35,15 +38,25 @@ public class MainActivity extends AppCompatActivity {
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
manager = new DecaManager(getApplicationContext()); manager = new DecaManager(getApplicationContext());
manager.setGattCallbacks(new DecaCallbacks()); manager.setGattCallbacks(decaCallbacks);
Button clickButton = (Button) findViewById(R.id.button2); Button button1 = findViewById(R.id.btnScan);
clickButton.setOnClickListener( new View.OnClickListener() { button1.setOnClickListener( new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
initBluetooth();
DoScan(); DoScan();
} }
}); });
Button button2 = findViewById(R.id.btnConnect);
button2.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
initBluetooth();
connectDirectly();
}
});
} }
@Override @Override
@@ -55,7 +68,7 @@ public class MainActivity extends AppCompatActivity {
bluetoothAdapter = null; bluetoothAdapter = null;
} }
private void DoScan() { private void initBluetooth() {
bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter(); bluetoothAdapter = bluetoothManager.getAdapter();
@@ -64,7 +77,9 @@ public class MainActivity extends AppCompatActivity {
new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 42); startActivityForResult(enableBtIntent, 42);
} }
}
private void DoScan() {
// Scanning settings // Scanning settings
final ScanSettings settings = new ScanSettings.Builder() final ScanSettings settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
@@ -76,7 +91,20 @@ public class MainActivity extends AppCompatActivity {
leScanner.startScan(null, settings, scanCallback); leScanner.startScan(null, settings, scanCallback);
} }
ScanCallback scanCallback = new ScanCallback() { private void connectDirectly() {
device = bluetoothAdapter.getRemoteDevice("D1:6C:7A:99:57:71");
connectToDevice(device);
}
private void connectToDevice(BluetoothDevice device) {
manager.connect(device)
.retry(3, 100)
.useAutoConnect(false)
.enqueue();
}
final ScanCallback scanCallback = new ScanCallback() {
@Override @Override
public void onScanResult(int callbackType, ScanResult result) { public void onScanResult(int callbackType, ScanResult result) {
@@ -87,10 +115,7 @@ public class MainActivity extends AppCompatActivity {
leScanner.stopScan(scanCallback); leScanner.stopScan(scanCallback);
manager.connect(device) connectToDevice(device);
.retry(3, 100)
.useAutoConnect(false)
.enqueue();
} }
} }
@@ -110,4 +135,75 @@ public class MainActivity extends AppCompatActivity {
Toast.makeText(getApplicationContext(), "Scan failed with code: " + errorCode, LENGTH_LONG).show(); Toast.makeText(getApplicationContext(), "Scan failed with code: " + errorCode, LENGTH_LONG).show();
} }
}; };
final DecaManagerCallbacks decaCallbacks = new DecaManagerCallbacks(){
@Override
public void onTagLocationData(byte[] data) {
}
@Override
public void onDeviceConnecting(@NonNull BluetoothDevice device) {
Log.d("DecaCallbacks", "onDeviceConnecting");
}
@Override
public void onDeviceConnected(@NonNull BluetoothDevice device) {
Log.d("DecaCallbacks", "onDeviceConnected");
}
@Override
public void onDeviceDisconnecting(@NonNull BluetoothDevice device) {
Log.d("DecaCallbacks", "onDeviceDisconnecting");
}
@Override
public void onDeviceDisconnected(@NonNull BluetoothDevice device) {
Log.d("DecaCallbacks", "onDeviceDisconnected");
}
@Override
public void onLinkLossOccurred(@NonNull BluetoothDevice device) {
Log.d("DecaCallbacks", "onLinkLossOccurred");
}
@Override
public void onServicesDiscovered(@NonNull BluetoothDevice device, boolean optionalServicesFound) {
Log.d("DecaCallbacks", "onServicesDiscovered");
}
@Override
public void onDeviceReady(@NonNull BluetoothDevice device) {
Log.d("DecaCallbacks", "onDeviceReady");
}
@Override
public void onBondingRequired(@NonNull BluetoothDevice device) {
Log.d("DecaCallbacks", "onBondingRequired");
}
@Override
public void onBonded(@NonNull BluetoothDevice device) {
Log.d("DecaCallbacks", "onBonded");
}
@Override
public void onBondingFailed(@NonNull BluetoothDevice device) {
Log.d("DecaCallbacks", "onBondingFailed");
}
@Override
public void onError(@NonNull BluetoothDevice device, @NonNull String message, int errorCode) {
Log.d("DecaCallbacks", "onError: " + message);
}
@Override
public void onDeviceNotSupported(@NonNull BluetoothDevice device) {
Log.d("DecaCallbacks", "onDeviceNotSupported");
}
};
} }

View File

@@ -7,12 +7,22 @@
tools:context=".MainActivity"> tools:context=".MainActivity">
<Button <Button
android:id="@+id/button2" android:id="@+id/btnScan"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="46dp" android:layout_marginStart="46dp"
android:layout_marginTop="53dp" android:layout_marginTop="60dp"
android:text="Button" android:text="Scan"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnConnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="60dp"
android:text="Connect"
app:layout_constraintStart_toEndOf="@+id/btnScan"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@@ -1,17 +0,0 @@
package com.example.nrftest;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
}
}

View File

@@ -12,7 +12,3 @@ org.gradle.jvmargs=-Xmx1536m
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true # org.gradle.parallel=true
android.enableJetifier=true
android.useAndroidX=true