Insight: fix pairing

This commit is contained in:
Milos Kozak 2022-03-14 22:22:12 +01:00
parent b31e328022
commit 5861c0ee6a
5 changed files with 29 additions and 11 deletions

View file

@ -61,7 +61,7 @@
android:minHeight="3dp" /> android:minHeight="3dp" />
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
style="@style/OkCancelButton" style="@style/OkCancelButton.Text"
android:id="@+id/ok" android:id="@+id/ok"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View file

@ -4,6 +4,8 @@
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.VIBRATE" />
<application> <application>
<activity <activity

View file

@ -9,6 +9,8 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -20,6 +22,8 @@ import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -54,6 +58,8 @@ public class InsightPairingActivity extends NoSplashAppCompatActivity implements
private RecyclerView deviceList; private RecyclerView deviceList;
private final DeviceAdapter deviceAdapter = new DeviceAdapter(); private final DeviceAdapter deviceAdapter = new DeviceAdapter();
private final int PERMISSION_REQUEST_BLUETOOTH = 30242;
private InsightConnectionService service; private InsightConnectionService service;
private final ServiceConnection serviceConnection = new ServiceConnection() { private final ServiceConnection serviceConnection = new ServiceConnection() {
@ -100,9 +106,18 @@ public class InsightPairingActivity extends NoSplashAppCompatActivity implements
deviceList.setLayoutManager(new LinearLayoutManager(this)); deviceList.setLayoutManager(new LinearLayoutManager(this));
deviceList.setAdapter(deviceAdapter); deviceList.setAdapter(deviceAdapter);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (ContextCompat.checkSelfPermission(context, "android.permission.BLUETOOTH_CONNECT") != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(context, "android.permission.BLUETOOTH_SCAN") != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(this, new String[]{"android.permission.BLUETOOTH_SCAN", "android.permission.BLUETOOTH_CONNECT"}, PERMISSION_REQUEST_BLUETOOTH);
finish();
return;
}
}
bindService(new Intent(this, InsightConnectionService.class), serviceConnection, BIND_AUTO_CREATE); bindService(new Intent(this, InsightConnectionService.class), serviceConnection, BIND_AUTO_CREATE);
} }
@Override @Override
protected void onDestroy() { protected void onDestroy() {
@ -110,8 +125,8 @@ public class InsightPairingActivity extends NoSplashAppCompatActivity implements
service.withdrawConnectionRequest(InsightPairingActivity.this); service.withdrawConnectionRequest(InsightPairingActivity.this);
service.unregisterStateCallback(InsightPairingActivity.this); service.unregisterStateCallback(InsightPairingActivity.this);
service.unregisterExceptionCallback(InsightPairingActivity.this); service.unregisterExceptionCallback(InsightPairingActivity.this);
unbindService(serviceConnection);
} }
unbindService(serviceConnection);
super.onDestroy(); super.onDestroy();
} }
@ -172,7 +187,7 @@ public class InsightPairingActivity extends NoSplashAppCompatActivity implements
private void startBLScan() { private void startBLScan() {
if (!scanning) { if (!scanning) {
BluetoothAdapter bluetoothAdapter = ((BluetoothManager)context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter(); BluetoothAdapter bluetoothAdapter = ((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();
if (bluetoothAdapter != null) { if (bluetoothAdapter != null) {
if (!bluetoothAdapter.isEnabled()) bluetoothAdapter.enable(); if (!bluetoothAdapter.isEnabled()) bluetoothAdapter.enable();
IntentFilter intentFilter = new IntentFilter(); IntentFilter intentFilter = new IntentFilter();
@ -188,13 +203,14 @@ public class InsightPairingActivity extends NoSplashAppCompatActivity implements
private void stopBLScan() { private void stopBLScan() {
if (scanning) { if (scanning) {
unregisterReceiver(broadcastReceiver); unregisterReceiver(broadcastReceiver);
BluetoothAdapter bluetoothAdapter = ((BluetoothManager)context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter(); BluetoothAdapter bluetoothAdapter = ((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();
if (bluetoothAdapter != null) { if (bluetoothAdapter != null) {
bluetoothAdapter.cancelDiscovery(); bluetoothAdapter.cancelDiscovery();
} }
scanning = false; scanning = false;
} }
} }
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (v == exit) finish(); if (v == exit) finish();
@ -216,7 +232,7 @@ public class InsightPairingActivity extends NoSplashAppCompatActivity implements
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
String action = intent.getAction(); String action = intent.getAction();
if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED))
((BluetoothManager)context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter().startDiscovery(); ((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter().startDiscovery();
else if (action.equals(BluetoothDevice.ACTION_FOUND)) { else if (action.equals(BluetoothDevice.ACTION_FOUND)) {
BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
deviceAdapter.addDevice(bluetoothDevice); deviceAdapter.addDevice(bluetoothDevice);

View file

@ -51,7 +51,7 @@
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/mute" android:id="@+id/mute"
style="@style/OkCancelButton" style="@style/OkCancelButton.Text"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:onClick="muteClicked" android:onClick="muteClicked"
@ -59,7 +59,7 @@
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/confirm" android:id="@+id/confirm"
style="@style/OkCancelButton" style="@style/OkCancelButton.Text"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:onClick="confirmClicked" android:onClick="confirmClicked"

View file

@ -66,14 +66,14 @@
android:orientation="horizontal"> android:orientation="horizontal">
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
style="@style/OkCancelButton" style="@style/OkCancelButton.Text"
android:id="@+id/no" android:id="@+id/no"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/no" /> android:text="@string/no" />
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
style="@style/OkCancelButton" style="@style/OkCancelButton.Text"
android:id="@+id/yes" android:id="@+id/yes"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -97,7 +97,7 @@
android:text="@string/pairing_completed" /> android:text="@string/pairing_completed" />
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
style="@style/OkCancelButton" style="@style/OkCancelButton.Text"
android:id="@+id/exit" android:id="@+id/exit"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"