У меня есть два устройства Android. Мой телефон Android и Materboard RK. Установите приложение в мой телефон Android, и оно может подключить устройство BLE и получить данные уведомления с устройства BLE. Когда я устанавливаю приложение на плату RK, оно подключает устройство BLE, но оно не может получить данные уведомления. Я проверил приложение NRF Connect на материнской плате RK, и оно получает данные уведомления.
может ли кто -нибудь помочь понять, почему?public class BluetoothHelper {
private BluetoothAdapter bluetoothAdapter = null;
private BluetoothLeScanner bluetoothLeScanner = null;
private OnScanResultListener onScanResultListener =null;
private BluetoothHelper() {
}
private static volatile BluetoothHelper sInstance;
public static BluetoothHelper getInstance() {
if(sInstance == null){
synchronized (BluetoothHelper.class) {
if (sInstance == null) {
sInstance = new BluetoothHelper();
}
}
}
return sInstance;
}
public void init(Context mContext) {
BluetoothManager bluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
if(bluetoothAdapter != null){
bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
}
}
public void initPermission(Context mContext,int requestCode) {
List permissionsNeeded = getPermissionsStrings();
List permissionsToRequest = new ArrayList();
for (String permission : permissionsNeeded) {
if (ContextCompat.checkSelfPermission(mContext, permission) != PackageManager.PERMISSION_GRANTED) {
permissionsToRequest.add(permission);
}
}
if (!permissionsToRequest.isEmpty()) {
ActivityCompat.requestPermissions((Activity) mContext,
permissionsToRequest.toArray(new String[0]),
requestCode);
} else {
Log.d("TAG", "BluetoothHelper -> permission ok");
}
}
@NonNull
private static List getPermissionsStrings() {
List permissionsNeeded = new ArrayList();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
permissionsNeeded.add(Manifest.permission.BLUETOOTH_SCAN);
permissionsNeeded.add(Manifest.permission.BLUETOOTH_ADVERTISE);
permissionsNeeded.add(Manifest.permission.BLUETOOTH_CONNECT);
} else {
permissionsNeeded.add(Manifest.permission.ACCESS_COARSE_LOCATION);
permissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
return permissionsNeeded;
}
public boolean isBluetoothEnable() {
if (bluetoothAdapter == null) {
return false;
}
return bluetoothAdapter.isEnabled();
}
@SuppressLint("MissingPermission")
public void startScanBle() {
if(bluetoothLeScanner != null){
bluetoothLeScanner.startScan(scanCallback);
}
}
@SuppressLint("MissingPermission")
public void stopScanBle() {
if(bluetoothLeScanner != null){
bluetoothLeScanner.stopScan(scanCallback);
}
}
public void setOnScanResultListener(OnScanResultListener onScanResultListener) {
this.onScanResultListener = onScanResultListener;
}
public interface OnScanResultListener{
void onScanResult(BluetoothDevice device);
void onScanFailed();
}
private final ScanCallback scanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
BluetoothDevice device = result.getDevice();
if(onScanResultListener != null){
onScanResultListener.onScanResult(device);
}
}
@Override
public void onBatchScanResults(List results) {
super.onBatchScanResults(results);
}
@Override
public void onScanFailed(int errorCode) {
super.onScanFailed(errorCode);
if(onScanResultListener != null){
onScanResultListener.onScanFailed();
}
}
};
}
< /code>
mainactivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final int REQUEST_CODE_BLUETOOTH = 0x100;
private ActivityMainBinding binding;
private BluetoothHelper bluetoothHelper = null;
private SimpleAdapter simpleAdapter = null;
private final List devicesList = new ArrayList();
private final List mDeviceList = new ArrayList();
private BluetoothGatt bluetoothGatt = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
ViewCompat.setOnApplyWindowInsetsListener(binding.main, (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
bluetoothHelper = BluetoothHelper.getInstance();
bluetoothHelper.init(this);
bluetoothHelper.setOnScanResultListener(onScanResultListener);
bluetoothHelper.initPermission(this, REQUEST_CODE_BLUETOOTH);
initView();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE_BLUETOOTH) {
boolean allPermissionsGranted = true;
for (int result : grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
allPermissionsGranted = false;
break;
}
}
if (allPermissionsGranted) {
Log.d(TAG, "permission ok");
} else {
boolean shouldShowRationale = false;
for (String permission : permissions) {
if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
shouldShowRationale = true;
break;
}
}
if (shouldShowRationale) {
showPermissionDeniedDialog();
} else {
showErrorMessage();
}
}
}
}
private void showPermissionDeniedDialog() {
new AlertDialog.Builder(this)
.setTitle("Permission denied")
.setMessage("You have permanently denied the necessary permissions, please go to the settings page to grant them manually.")
.setPositiveButton("Settings", (dialog, which) -> {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
})
.setNegativeButton("Cancel", null)
.show();
}
private void showErrorMessage() {
new AlertDialog.Builder(this)
.setTitle("Error")
.setMessage("Permission denied, please grant permission to continue using the Bluetooth feature")
.setPositiveButton("Confirm", null)
.show();
}
private void initView() {
binding.scan.setOnClickListener(v -> {
boolean isEnable = bluetoothHelper.isBluetoothEnable();
if (isEnable) {
bluetoothHelper.startScanBle();
}
});
simpleAdapter = new SimpleAdapter(
this,
devicesList,
android.R.layout.simple_list_item_2,
new String[]{"title", "subtitle"},
new int[]{android.R.id.text1, android.R.id.text2}
);
binding.showBleDevices.setAdapter(simpleAdapter);
binding.showBleDevices.setOnItemClickListener((parent, view, position, id) -> {
Map map = devicesList.get(position);
String address = map.get("subtitle");
Log.d(TAG, "onItemClick -> position:" + position + ", address:" + address);
for (BluetoothDevice device : mDeviceList) {
if (address != null) {
if (address.equals(device.getAddress())) {
if (bluetoothHelper != null) {
bluetoothHelper.stopScanBle();
}
Log.d(TAG, "onItemClick -> name:" + device.getName());
connectDevice(device);
break;
}
}
}
});
}
private final BluetoothHelper.OnScanResultListener onScanResultListener =
new BluetoothHelper.OnScanResultListener() {
@SuppressLint("MissingPermission")
@Override
public void onScanResult(BluetoothDevice device) {
if (device.getName() != null) {
boolean existsByAddress = mDeviceList.stream()
.anyMatch(device1 -> device1.getAddress().equals(device.getAddress()));
if (!existsByAddress) {
mDeviceList.add(device);
devicesList.clear();
for (BluetoothDevice bluetoothDevice : mDeviceList) {
devicesList.add(new HashMap() {{
put("title", bluetoothDevice.getName());
put("subtitle", bluetoothDevice.getAddress());
}});
}
}
simpleAdapter.notifyDataSetChanged();
}
}
@Override
public void onScanFailed() {
}
};
protected void onPause() {
super.onPause();
if (bluetoothHelper != null) {
bluetoothHelper.stopScanBle();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (bluetoothHelper != null) {
bluetoothHelper.stopScanBle();
}
if (bluetoothGatt != null) {
bluetoothGatt.disconnect();
bluetoothGatt.close();
}
}
private void connectDevice(BluetoothDevice device) {
if (bluetoothGatt != null) {
bluetoothGatt.disconnect();
bluetoothGatt.close();
bluetoothGatt = null;
}
bluetoothGatt = device.connectGatt(this, false, new BluetoothGattCallback() {
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
startCommunicationWithDevice(gatt);
}
}
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if(status == BluetoothGatt.GATT_SUCCESS) {
String message;
if (newState == BluetoothGatt.STATE_CONNECTED) {
message = "Connect success";
gatt.discoverServices();
} else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
message = "disconnect";
} else {
message = "";
}
runOnUiThread(() -> Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show());
}else {
Log.i(TAG, "onConnectionStateChange----: " + "Connection status is abnormal:" + status);
}
}
@Override
public void onCharacteristicRead(@NonNull BluetoothGatt gatt, @NonNull BluetoothGattCharacteristic characteristic, @NonNull byte[] value, int status) {
super.onCharacteristicRead(gatt, characteristic, value, status);
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
}
@Override
public void onCharacteristicChanged(@NonNull BluetoothGatt gatt, @NonNull BluetoothGattCharacteristic characteristic, @NonNull byte[] value) {
super.onCharacteristicChanged(gatt, characteristic, value);
Log.i(TAG, "Characteristic changed: " + bytesToHexHasSpace(characteristic.getValue()));
runOnUiThread(() -> {
binding.bleOriginalData.setText(bytesToHexHasSpace(characteristic.getValue()));
});
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
}
});
}
private void startCommunicationWithDevice(BluetoothGatt gatt) {
BluetoothGattCharacteristic characteristic =
gatt.getService(UUID.fromString("00001826-0000-1000-8000-00805f9b34fb"))
.getCharacteristic(UUID.fromString("00002ace-0000-1000-8000-00805f9b34fb")); //cross trainer data
setCharacteristicNotificationInternal(gatt, characteristic, true);
}
private void setCharacteristicNotificationInternal(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, boolean enabled){
if (characteristic == null){
Log.e(TAG, "characteristic is null");
return;
}
gatt.setCharacteristicNotification(characteristic, enabled);
//If the number of descriptors in the eigenvalue of the notification is greater than zero
List descriptors = characteristic.getDescriptors();
if (!descriptors.isEmpty()) {
//Filter descriptors based on the uuid of the descriptor
for(BluetoothGattDescriptor descriptor : descriptors){
if (descriptor != null) {
//Write the description value
if((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0){
descriptor.setValue(enabled?BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE:BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
}else if((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0){
descriptor.setValue(enabled?BluetoothGattDescriptor.ENABLE_INDICATION_VALUE:BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
}
gatt.writeDescriptor(descriptor);
Log.d(TAG, "setCharacteristicNotificationInternal is "+enabled);
}
}
}
}
private String bytesToHexHasSpace(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02X ", b));
}
return sb.toString();
}
}
< /code>
android manifest.xml
Подробнее здесь: https://stackoverflow.com/questions/795 ... otify-data
Приложение Android не может получить данные BLE уведомления ⇐ Android
Форум для тех, кто программирует под Android
-
Anonymous
1744165008
Anonymous
У меня есть два устройства Android. Мой телефон Android и Materboard RK. Установите приложение в мой телефон Android, и оно может подключить устройство BLE и получить данные уведомления с устройства BLE. Когда я устанавливаю приложение на плату RK, оно подключает устройство BLE, но оно не может получить данные уведомления. Я проверил приложение NRF Connect на материнской плате RK, и оно получает данные уведомления.
может ли кто -нибудь помочь понять, почему?public class BluetoothHelper {
private BluetoothAdapter bluetoothAdapter = null;
private BluetoothLeScanner bluetoothLeScanner = null;
private OnScanResultListener onScanResultListener =null;
private BluetoothHelper() {
}
private static volatile BluetoothHelper sInstance;
public static BluetoothHelper getInstance() {
if(sInstance == null){
synchronized (BluetoothHelper.class) {
if (sInstance == null) {
sInstance = new BluetoothHelper();
}
}
}
return sInstance;
}
public void init(Context mContext) {
BluetoothManager bluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
if(bluetoothAdapter != null){
bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
}
}
public void initPermission(Context mContext,int requestCode) {
List permissionsNeeded = getPermissionsStrings();
List permissionsToRequest = new ArrayList();
for (String permission : permissionsNeeded) {
if (ContextCompat.checkSelfPermission(mContext, permission) != PackageManager.PERMISSION_GRANTED) {
permissionsToRequest.add(permission);
}
}
if (!permissionsToRequest.isEmpty()) {
ActivityCompat.requestPermissions((Activity) mContext,
permissionsToRequest.toArray(new String[0]),
requestCode);
} else {
Log.d("TAG", "BluetoothHelper -> permission ok");
}
}
@NonNull
private static List getPermissionsStrings() {
List permissionsNeeded = new ArrayList();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
permissionsNeeded.add(Manifest.permission.BLUETOOTH_SCAN);
permissionsNeeded.add(Manifest.permission.BLUETOOTH_ADVERTISE);
permissionsNeeded.add(Manifest.permission.BLUETOOTH_CONNECT);
} else {
permissionsNeeded.add(Manifest.permission.ACCESS_COARSE_LOCATION);
permissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
return permissionsNeeded;
}
public boolean isBluetoothEnable() {
if (bluetoothAdapter == null) {
return false;
}
return bluetoothAdapter.isEnabled();
}
@SuppressLint("MissingPermission")
public void startScanBle() {
if(bluetoothLeScanner != null){
bluetoothLeScanner.startScan(scanCallback);
}
}
@SuppressLint("MissingPermission")
public void stopScanBle() {
if(bluetoothLeScanner != null){
bluetoothLeScanner.stopScan(scanCallback);
}
}
public void setOnScanResultListener(OnScanResultListener onScanResultListener) {
this.onScanResultListener = onScanResultListener;
}
public interface OnScanResultListener{
void onScanResult(BluetoothDevice device);
void onScanFailed();
}
private final ScanCallback scanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
BluetoothDevice device = result.getDevice();
if(onScanResultListener != null){
onScanResultListener.onScanResult(device);
}
}
@Override
public void onBatchScanResults(List results) {
super.onBatchScanResults(results);
}
@Override
public void onScanFailed(int errorCode) {
super.onScanFailed(errorCode);
if(onScanResultListener != null){
onScanResultListener.onScanFailed();
}
}
};
}
< /code>
mainactivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final int REQUEST_CODE_BLUETOOTH = 0x100;
private ActivityMainBinding binding;
private BluetoothHelper bluetoothHelper = null;
private SimpleAdapter simpleAdapter = null;
private final List devicesList = new ArrayList();
private final List mDeviceList = new ArrayList();
private BluetoothGatt bluetoothGatt = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
ViewCompat.setOnApplyWindowInsetsListener(binding.main, (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
bluetoothHelper = BluetoothHelper.getInstance();
bluetoothHelper.init(this);
bluetoothHelper.setOnScanResultListener(onScanResultListener);
bluetoothHelper.initPermission(this, REQUEST_CODE_BLUETOOTH);
initView();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE_BLUETOOTH) {
boolean allPermissionsGranted = true;
for (int result : grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
allPermissionsGranted = false;
break;
}
}
if (allPermissionsGranted) {
Log.d(TAG, "permission ok");
} else {
boolean shouldShowRationale = false;
for (String permission : permissions) {
if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
shouldShowRationale = true;
break;
}
}
if (shouldShowRationale) {
showPermissionDeniedDialog();
} else {
showErrorMessage();
}
}
}
}
private void showPermissionDeniedDialog() {
new AlertDialog.Builder(this)
.setTitle("Permission denied")
.setMessage("You have permanently denied the necessary permissions, please go to the settings page to grant them manually.")
.setPositiveButton("Settings", (dialog, which) -> {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
})
.setNegativeButton("Cancel", null)
.show();
}
private void showErrorMessage() {
new AlertDialog.Builder(this)
.setTitle("Error")
.setMessage("Permission denied, please grant permission to continue using the Bluetooth feature")
.setPositiveButton("Confirm", null)
.show();
}
private void initView() {
binding.scan.setOnClickListener(v -> {
boolean isEnable = bluetoothHelper.isBluetoothEnable();
if (isEnable) {
bluetoothHelper.startScanBle();
}
});
simpleAdapter = new SimpleAdapter(
this,
devicesList,
android.R.layout.simple_list_item_2,
new String[]{"title", "subtitle"},
new int[]{android.R.id.text1, android.R.id.text2}
);
binding.showBleDevices.setAdapter(simpleAdapter);
binding.showBleDevices.setOnItemClickListener((parent, view, position, id) -> {
Map map = devicesList.get(position);
String address = map.get("subtitle");
Log.d(TAG, "onItemClick -> position:" + position + ", address:" + address);
for (BluetoothDevice device : mDeviceList) {
if (address != null) {
if (address.equals(device.getAddress())) {
if (bluetoothHelper != null) {
bluetoothHelper.stopScanBle();
}
Log.d(TAG, "onItemClick -> name:" + device.getName());
connectDevice(device);
break;
}
}
}
});
}
private final BluetoothHelper.OnScanResultListener onScanResultListener =
new BluetoothHelper.OnScanResultListener() {
@SuppressLint("MissingPermission")
@Override
public void onScanResult(BluetoothDevice device) {
if (device.getName() != null) {
boolean existsByAddress = mDeviceList.stream()
.anyMatch(device1 -> device1.getAddress().equals(device.getAddress()));
if (!existsByAddress) {
mDeviceList.add(device);
devicesList.clear();
for (BluetoothDevice bluetoothDevice : mDeviceList) {
devicesList.add(new HashMap() {{
put("title", bluetoothDevice.getName());
put("subtitle", bluetoothDevice.getAddress());
}});
}
}
simpleAdapter.notifyDataSetChanged();
}
}
@Override
public void onScanFailed() {
}
};
protected void onPause() {
super.onPause();
if (bluetoothHelper != null) {
bluetoothHelper.stopScanBle();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (bluetoothHelper != null) {
bluetoothHelper.stopScanBle();
}
if (bluetoothGatt != null) {
bluetoothGatt.disconnect();
bluetoothGatt.close();
}
}
private void connectDevice(BluetoothDevice device) {
if (bluetoothGatt != null) {
bluetoothGatt.disconnect();
bluetoothGatt.close();
bluetoothGatt = null;
}
bluetoothGatt = device.connectGatt(this, false, new BluetoothGattCallback() {
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
startCommunicationWithDevice(gatt);
}
}
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if(status == BluetoothGatt.GATT_SUCCESS) {
String message;
if (newState == BluetoothGatt.STATE_CONNECTED) {
message = "Connect success";
gatt.discoverServices();
} else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
message = "disconnect";
} else {
message = "";
}
runOnUiThread(() -> Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show());
}else {
Log.i(TAG, "onConnectionStateChange----: " + "Connection status is abnormal:" + status);
}
}
@Override
public void onCharacteristicRead(@NonNull BluetoothGatt gatt, @NonNull BluetoothGattCharacteristic characteristic, @NonNull byte[] value, int status) {
super.onCharacteristicRead(gatt, characteristic, value, status);
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
}
@Override
public void onCharacteristicChanged(@NonNull BluetoothGatt gatt, @NonNull BluetoothGattCharacteristic characteristic, @NonNull byte[] value) {
super.onCharacteristicChanged(gatt, characteristic, value);
Log.i(TAG, "Characteristic changed: " + bytesToHexHasSpace(characteristic.getValue()));
runOnUiThread(() -> {
binding.bleOriginalData.setText(bytesToHexHasSpace(characteristic.getValue()));
});
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
}
});
}
private void startCommunicationWithDevice(BluetoothGatt gatt) {
BluetoothGattCharacteristic characteristic =
gatt.getService(UUID.fromString("00001826-0000-1000-8000-00805f9b34fb"))
.getCharacteristic(UUID.fromString("00002ace-0000-1000-8000-00805f9b34fb")); //cross trainer data
setCharacteristicNotificationInternal(gatt, characteristic, true);
}
private void setCharacteristicNotificationInternal(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, boolean enabled){
if (characteristic == null){
Log.e(TAG, "characteristic is null");
return;
}
gatt.setCharacteristicNotification(characteristic, enabled);
//If the number of descriptors in the eigenvalue of the notification is greater than zero
List descriptors = characteristic.getDescriptors();
if (!descriptors.isEmpty()) {
//Filter descriptors based on the uuid of the descriptor
for(BluetoothGattDescriptor descriptor : descriptors){
if (descriptor != null) {
//Write the description value
if((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0){
descriptor.setValue(enabled?BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE:BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
}else if((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0){
descriptor.setValue(enabled?BluetoothGattDescriptor.ENABLE_INDICATION_VALUE:BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
}
gatt.writeDescriptor(descriptor);
Log.d(TAG, "setCharacteristicNotificationInternal is "+enabled);
}
}
}
}
private String bytesToHexHasSpace(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02X ", b));
}
return sb.toString();
}
}
< /code>
android manifest.xml
Подробнее здесь: [url]https://stackoverflow.com/questions/79563337/android-app-can-not-receive-ble-notify-data[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия