建立连接后,我想读取特征值并将值保存在字节数组中。
这是我的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
更多推荐
发布评论