BLE 'onCharacteristicRead' 不起作用



现在,我用安卓BLE开发App。但我有一个问题..

这是代码....

// Device connect call back
private final BluetoothGattCallback btleGattCallback = new BluetoothGattCallback() {
    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
        // this will get called anytime you perform a read or write characteristic operation
                peripheralTextView.append("device read or wrote ton");
                broadcastUpdate(ACTION_DATA_AVAILABLE,characteristic);
    }
    @Override
    public void onConnectionStateChange(final BluetoothGatt gatt, final int status, final int newState) {
        // this will get called when a device connects or disconnects
        System.out.println(newState);
        switch (newState) {
            case BluetoothProfile.STATE_DISCONNECTED:
                MainActivity.this.runOnUiThread(new Runnable() {
                    public void run() {
                        peripheralTextView.append("device disconnectedn");
                        connectToDevice.setVisibility(View.VISIBLE);
                        disconnectDevice.setVisibility(View.INVISIBLE);
                    }
                });
                break;
            case BluetoothProfile.STATE_CONNECTED:
                MainActivity.this.runOnUiThread(new Runnable() {
                    public void run() {
                        peripheralTextView.append("device connectedn");
                        connectToDevice.setVisibility(View.INVISIBLE);
                        disconnectDevice.setVisibility(View.VISIBLE);
                    }
                });
                // discover services and characteristics for this device
                bluetoothGatt.discoverServices();
                break;
            default:
                MainActivity.this.runOnUiThread(new Runnable() {
                    public void run() {
                        peripheralTextView.append("we encounterned an unknown state, uh ohn");
                    }
                });
                break;
        }
    }
    @Override
    public void onServicesDiscovered(final BluetoothGatt gatt, final int status) {
        // this will get called after the client initiates a            BluetoothGatt.discoverServices() call
        MainActivity.this.runOnUiThread(new Runnable() {
            public void run() {
                peripheralTextView.append("device services have been discoveredn");
            }
        });
        displayGattServices(bluetoothGatt.getServices());
    }
    @Override
    // Result of a characteristic read operation
    public void onCharacteristicRead(BluetoothGatt gatt,
                                     BluetoothGattCharacteristic characteristic,
                                     int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        }
    }
};
private void broadcastUpdate(final String action,
                             final BluetoothGattCharacteristic characteristic) {
    // For all other profiles, writes the data formatted in HEX.
    final byte[] data = characteristic.getValue();
    if (data != null && data.length > 0) {
        final StringBuilder stringBuilder = new StringBuilder(data.length);
        for (byte byteChar : data)
            stringBuilder.append(String.format("%02X ", byteChar));
            System.out.println(stringBuilder.toString());
    }
}

..........\

public void disconnectDeviceSelected() {
    peripheralTextView.append("Disconnecting from devicen");
    bluetoothGatt.disconnect();
}
private void displayGattServices(List<BluetoothGattService> gattServices) {
    if (gattServices == null) return;
    // Loops through available GATT Services.
    for (BluetoothGattService gattService : gattServices) {
        final String uuid = gattService.getUuid().toString();
        System.out.println("Service discovered: " + uuid);
        MainActivity.this.runOnUiThread(new Runnable() {
            public void run() {
                peripheralTextView.append("Service disovered: "+uuid+"n");
            }
        });
        new ArrayList<HashMap<String, String>>();
        List<BluetoothGattCharacteristic> gattCharacteristics =
                gattService.getCharacteristics();
        // Loops through available Characteristics.
        for (BluetoothGattCharacteristic gattCharacteristic :
                gattCharacteristics) {
            final String charUuid = gattCharacteristic.getUuid().toString();
            System.out.println("Characteristic discovered for service: " + charUuid);
            MainActivity.this.runOnUiThread(new Runnable() {
                public void run() {
                    peripheralTextView.append("Characteristic discovered for service: "+charUuid+"n");
                }
            });
        }
    }
}

我运行这段代码...

扫描和连接完成; 但是在特征读取不起作用;( 我需要你的帮助!

onServicesDiscover中调用此方法,如果此方法返回true,则将命令发送到ble设备

public Boolean enableTXNotification() {
        /*
        if (mBluetoothGatt == null) {
            showMessage("mBluetoothGatt null" + mBluetoothGatt);
            broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
            return;
        }
            */
        BluetoothGattService RxService = mBluetoothGatt.getService(Lock.RX_SERVICE_UUID);
        if (RxService == null) {
            showMessage("Rx service not found!");
            broadcastUpdate(Lock.DEVICE_DOES_NOT_SUPPORT_UART);
            return false;
        }
        BluetoothGattCharacteristic TxChar = RxService.getCharacteristic(Lock.TX_CHAR_UUID);
        if (TxChar == null) {
            showMessage("Tx charateristic not found!");
            broadcastUpdate(Lock.DEVICE_DOES_NOT_SUPPORT_UART);
            return false;
        }
        mBluetoothGatt.setCharacteristicNotification(TxChar, true);
        BluetoothGattDescriptor descriptor = TxChar.getDescriptor(Lock.CCCD);
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        mBluetoothGatt.writeDescriptor(descriptor);
        return true;
    }

如果您有任何疑问,请告诉我

最新更新