连接到服务蓝牙低能量Android后读取特性值(Reading value of characteristic after connecting to service Bluetooth Low En

编程入门 行业动态 更新时间:2024-10-20 07:59:05
连接到服务蓝牙低能量Android后读取特性值(Reading value of characteristic after connecting to service Bluetooth Low Energy Android)

建立连接后,我想读取特征值并将值保存在字节数组中。

这是我的BluetoothLeService内部阅读功能:

public byte[] readWhiteAndIntensityCharacteristic() { if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w(TAG, "BluetoothAdapter not initialized"); return null; } /*check if the service is available on the device*/ BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString(UuidAdresssService)); if (mCustomService == null) { Log.w(TAG, "Custom BLE Service not found"); return null; } /*get the read characteristic from the service*/ BluetoothGattCharacteristic mReadCharacteristic = mCustomService.getCharacteristic(UUID.fromString(UuidAdresssWhiteAndIntensityCharastic)); byte[] messageByte = mReadCharacteristic.getValue(); if (messageByte != null && messageByte.length > 0) { final StringBuilder stringBuilder = new StringBuilder(messageByte.length); for (byte byteChar : messageByte) stringBuilder.append(String.format("%02X", byteChar)); s = "0x" + stringBuilder.toString(); Log.v("Scan Activity", s); if (mBluetoothGatt.readCharacteristic(mReadCharacteristic) == false) { Log.w(TAG, "Failed to read characteristic"); } } return messageByte;

}这个函数在我的DeviceControlActivity中被调用:

public void getStartUpValue(){ Log.w(TAG, "Reading completed");} if(mBluetoothLeService.readWhiteAndIntensityCharacteristic() == null) { Log.w(TAG, "FAILED Reading value failed"); } startValue = mBluetoothLeService.readWhiteAndIntensityCharacteristic(); Log.w(TAG, "Reading completed"); }

连接建立后,我调用getStartUpValue函数。

private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) { mConnected = true; updateConnectionState(R.string.connected); invalidateOptionsMenu(); } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) { mConnected = false; updateConnectionState(R.string.disconnected); invalidateOptionsMenu(); clearUI(); } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) { // Show all the supported services and characteristics on the user interface. mBluetoothLeService.getSupportedGattServices(); getStartUpValue(); } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) { displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA)); } }

};

阅读失败每次,但发送值的特点是没有问题的。

我该如何解决这个问题?

after the connection is established I want to read the value from the characteristic and save the value in a byte array.

Here is my function for the reading inside my BluetoothLeService:

public byte[] readWhiteAndIntensityCharacteristic() { if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w(TAG, "BluetoothAdapter not initialized"); return null; } /*check if the service is available on the device*/ BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString(UuidAdresssService)); if (mCustomService == null) { Log.w(TAG, "Custom BLE Service not found"); return null; } /*get the read characteristic from the service*/ BluetoothGattCharacteristic mReadCharacteristic = mCustomService.getCharacteristic(UUID.fromString(UuidAdresssWhiteAndIntensityCharastic)); byte[] messageByte = mReadCharacteristic.getValue(); if (messageByte != null && messageByte.length > 0) { final StringBuilder stringBuilder = new StringBuilder(messageByte.length); for (byte byteChar : messageByte) stringBuilder.append(String.format("%02X", byteChar)); s = "0x" + stringBuilder.toString(); Log.v("Scan Activity", s); if (mBluetoothGatt.readCharacteristic(mReadCharacteristic) == false) { Log.w(TAG, "Failed to read characteristic"); } } return messageByte;

} This function get called inside my DeviceControlActivity:

public void getStartUpValue(){ Log.w(TAG, "Reading completed");} if(mBluetoothLeService.readWhiteAndIntensityCharacteristic() == null) { Log.w(TAG, "FAILED Reading value failed"); } startValue = mBluetoothLeService.readWhiteAndIntensityCharacteristic(); Log.w(TAG, "Reading completed"); }

I call the getStartUpValue function after the connection is established.

private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) { mConnected = true; updateConnectionState(R.string.connected); invalidateOptionsMenu(); } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) { mConnected = false; updateConnectionState(R.string.disconnected); invalidateOptionsMenu(); clearUI(); } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) { // Show all the supported services and characteristics on the user interface. mBluetoothLeService.getSupportedGattServices(); getStartUpValue(); } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) { displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA)); } }

};

The reading is failing everytime but sending values to the characteristic is no problem.

How can I fix this problem?

最满意答案

解决方案是:mBluetoothGatt.readCharacteristic(mReadCharacteristic)

读取完成后,将调用回调。

感谢Andrew Vovk

The solution is: mBluetoothGatt.readCharacteristic(mReadCharacteristic)

After the reading is finished a callback will be called.

Thanks to Andrew Vovk

更多推荐

本文发布于:2023-08-05 21:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1438007.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:蓝牙   连接到   能量   特性   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!