将数据发送到连接的蓝牙低功耗设备失败

时间:2016-01-08 09:37:35

标签: android

我正在开发Android应用程序,它需要向ble设备发送和接收数据。我已设法进行扫描,配对和连接活动但未能发送数据。我在连接事件成功后立即调用gatt.discoverServices()。下面是我尝试发送数据的代码部分。

public class BluetoothLeUart extends BluetoothGattCallback implements BluetoothAdapter.LeScanCallback {

// UUIDs for UART service and associated characteristics.
public static UUID UART_UUID = UUID.fromString("6E400001-B5A3-F393-E0A9-E50E24DCCA9E");
public static UUID TX_UUID   = UUID.fromString("6E400002-B5A3-F393-E0A9-E50E24DCCA9E");
public static UUID RX_UUID   = UUID.fromString("6E400003-B5A3-F393-E0A9-E50E24DCCA9E");

// Internal UART state.
private Context context;
private WeakHashMap<Callback, Object> callbacks;
private BluetoothAdapter adapter;
private BluetoothGatt gatt;
private BluetoothGattCharacteristic tx;
private BluetoothGattCharacteristic rx;
private boolean connectFirst;
private boolean writeInProgress; // Flag to indicate a write is currently in progress

// Queues for characteristic read (synchronous)
private Queue<BluetoothGattCharacteristic> readQueue;

public BluetoothLeUart(Context context) {
    super();
    this.context = context;
    this.callbacks = new WeakHashMap<Callback, Object>();
    this.adapter = BluetoothAdapter.getDefaultAdapter();
    this.gatt = null;
    this.tx = null;
    this.rx = null;
    this.writeInProgress = false;
    this.readQueue = new ConcurrentLinkedQueue<BluetoothGattCharacteristic>();
}    
// Send data to connected UART device.
public void send(byte[] data) {
    // Update TX characteristic value.  Note the setValue overload that takes a byte array must be used.
    tx.setValue(data);
    writeInProgress = true; // Set the write in progress flag
    gatt.writeCharacteristic(tx);
    while (writeInProgress); // Wait for the flag to clear in onCharacteristicWrite

}

//my onServicesDiscovered function
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    super.onServicesDiscovered(gatt, status);
    // Notify connection failure if service discovery failed.
    if (status == BluetoothGatt.GATT_FAILURE) {
        connectFailure();
        return;
    }

    // Save reference to each UART characteristic.
    tx = gatt.getService(UART_UUID).getCharacteristic(TX_UUID);
    rx = gatt.getService(UART_UUID).getCharacteristic(RX_UUID);



    // Setup notifications on RX characteristic changes (i.e. data received).
    // First call setCharacteristicNotification to enable notification.
    if (!gatt.setCharacteristicNotification(rx, true)) {
        // Stop if the characteristic notification setup failed.
        connectFailure();
        return;
    }
    // Next update the RX characteristic's client descriptor to enable notifications.
    BluetoothGattDescriptor desc = rx.getDescriptor(CLIENT_UUID);
    if (desc == null) {
        // Stop if the RX characteristic has no client descriptor.
        connectFailure();
        return;
    }
    desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    if (!gatt.writeDescriptor(desc)) {
        // Stop if the client descriptor could not be written.
        connectFailure();
        return;
    }
    // Notify of connection completion.
    notifyOnConnected(this);
}

0 个答案:

没有答案