BLE`onCharacteristicRead`无效

时间:2017-05-12 08:20:36

标签: android bluetooth-lowenergy

如今,我使用android BLE开发App。 但我有一个问题..

这是代码......

Dusterio\LumenPassport\LumenPassport::routes($app, [ 'prefix' => 'api/v1/protected', 'middleware' => 'reject', ]);

.......... \

// 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 to\n");
                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 disconnected\n");
                        connectToDevice.setVisibility(View.VISIBLE);
                        disconnectDevice.setVisibility(View.INVISIBLE);
                    }
                });
                break;
            case BluetoothProfile.STATE_CONNECTED:
                MainActivity.this.runOnUiThread(new Runnable() {
                    public void run() {
                        peripheralTextView.append("device connected\n");
                        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 oh\n");
                    }
                });
                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 discovered\n");
            }
        });
        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());
    }
}

我运行此代码......

完成扫描和连接;  但onCharacteristicRead不起作用;(  我需要你的帮助PLZ!

1 个答案:

答案 0 :(得分:0)

在onServicesDiscovered中调用此方法,如果此方法返回true,则将命令发送到ble device

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;
    }

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