蓝牙增加授权校验

master
hwf452 10 months ago
parent 9b2779f0c3
commit 4639bc7d9d

@ -232,6 +232,7 @@
<service <service
android:name=".bleUtil.BluetoothLeService" android:name=".bleUtil.BluetoothLeService"
android:exported="false"
android:enabled="true" /> android:enabled="true" />
<provider <provider

@ -1,6 +1,7 @@
package com.rehome.bhdxj.bleUtil; package com.rehome.bhdxj.bleUtil;
import android.Manifest;
import android.app.Service; import android.app.Service;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
@ -12,10 +13,13 @@ import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile; import android.bluetooth.BluetoothProfile;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Binder; import android.os.Binder;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log; import android.util.Log;
import androidx.core.app.ActivityCompat;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -59,6 +63,16 @@ public class BluetoothLeService extends Service {
broadcastUpdate(intentAction); broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server."); Log.i(TAG, "Connected to GATT server.");
// Attempts to discover services after successful connection. // Attempts to discover services after successful connection.
if (ActivityCompat.checkSelfPermission(BluetoothLeService.this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Log.i(TAG, "Attempting to start service discovery:" + Log.i(TAG, "Attempting to start service discovery:" +
mBluetoothGatt.discoverServices()); mBluetoothGatt.discoverServices());
@ -185,6 +199,16 @@ public class BluetoothLeService extends Service {
// Previously connected device. Try to reconnect. // Previously connected device. Try to reconnect.
if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress)&& mBluetoothGatt != null) { if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress)&& mBluetoothGatt != null) {
Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection."); Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return false;
}
if (mBluetoothGatt.connect()) { if (mBluetoothGatt.connect()) {
mConnectionState = STATE_CONNECTING; mConnectionState = STATE_CONNECTING;
return true; return true;
@ -218,6 +242,16 @@ public class BluetoothLeService extends Service {
Log.w(TAG, "BluetoothAdapter not initialized"); Log.w(TAG, "BluetoothAdapter not initialized");
return; return;
} }
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mBluetoothGatt.disconnect(); mBluetoothGatt.disconnect();
} }
@ -229,6 +263,16 @@ public class BluetoothLeService extends Service {
if (mBluetoothGatt == null) { if (mBluetoothGatt == null) {
return; return;
} }
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mBluetoothGatt.close(); mBluetoothGatt.close();
mBluetoothGatt = null; mBluetoothGatt = null;
} }
@ -245,6 +289,16 @@ public class BluetoothLeService extends Service {
Log.w(TAG, "BluetoothAdapter not initialized"); Log.w(TAG, "BluetoothAdapter not initialized");
return; return;
} }
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mBluetoothGatt.readCharacteristic(characteristic); mBluetoothGatt.readCharacteristic(characteristic);
} }
public void writeCharacteristic(BluetoothGattCharacteristic characteristic) { public void writeCharacteristic(BluetoothGattCharacteristic characteristic) {
@ -252,6 +306,16 @@ public class BluetoothLeService extends Service {
Log.w(TAG, "BluetoothAdapter not initialized"); Log.w(TAG, "BluetoothAdapter not initialized");
return; return;
} }
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mBluetoothGatt.writeCharacteristic(characteristic); mBluetoothGatt.writeCharacteristic(characteristic);
} }
/** /**
@ -266,6 +330,16 @@ public class BluetoothLeService extends Service {
Log.w(TAG, "BluetoothAdapter not initialized"); Log.w(TAG, "BluetoothAdapter not initialized");
return; return;
} }
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
} }

Loading…
Cancel
Save