5

I'm working on a Android project which is to connect Nexus 7 and a bio sensor through BLE link. The problem is that, I can successfully detect and get list of services and characteristics of the sensor. When I write some data to the specific characteristic, onCharacteristicWrite is automatically called and showed me writing operation is successful. However, the sensor never receive anything from the tablet. And if I use similar app on iPhone, everything works fine. So there's no problem with the device. Does anyone have any idea of the problem?

Here is my code for write:

 private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            mConnected = true;
            Log.i(TAG, "Connected to GATT server.");
            // Attempts to discover services after successful connection.
            Log.i(TAG, "Attempting to start service discovery:" +
                    mBluetoothGatt.discoverServices());

        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            mConnected = false;
            Log.i(TAG, "Disconnected from GATT server.");
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {

         //Once detected services, write to characteristic for 6 times.
          int count =6;
            while(count>0){

              writeCharacteristic();

                count--;

            }

        } else {
            Log.w(TAG, "onServicesDiscovered received: " + status);
        }
    }

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt,
                                      BluetoothGattCharacteristic characteristic,
                                      int status){

        if (status == BluetoothGatt.GATT_SUCCESS){

            Log.d(TAG,"Write to Characteristic Success! !");
        }

    }
};

public boolean writeCharacteristic(){

    //check mBluetoothGatt is available
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return false;
    }

    BluetoothGattService Service = mBluetoothGatt.getService(UUID_MY_SERVICE);
    if (Service == null) {
        Log.e(TAG, "service not found!");
        return false;
    }
    BluetoothGattCharacteristic characteristic = Service
            .getCharacteristic(UUID_MY_CHARACTERISTIC);
    if (characteristic == null) {
        Log.e(TAG, "char not found!");
        return false;
    }

    byte[] value = {(byte)300,(byte)100,(byte)100};
    characteristic.setValue(value);

    boolean status = mBluetoothGatt.writeCharacteristic(characteristic);

    return status;
}

The output shows "Write to Characteristic Success! !" for six times, thus the writing operation succeeded. However, the device shows that nothing been received from tablet. I also tried to write one byte at a time, or add a timer to let the tablet write to sensor every 2 seconds. But none of them worked. Any ideas?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Magic
  • 385
  • 1
  • 5
  • 13
  • Have you tried turning on HCI snoop (developer settings on the phone) and checking to see what is being sent and being received? – Zomb Mar 24 '14 at 20:13
  • Hi, I just did. However, I don't know how to open it correctly. Could you tell me how to open it with logcat? I tried open it with text or sublime but turn out to be unreadable. And what is the difference between this log file and the information I get from Android Studio Logcat while I ran the program? – Magic Mar 24 '14 at 21:16
  • I believe you need to use Wireshark to read the log. I have not tried it, just figured that it might help you in this case. – Zomb Mar 24 '14 at 23:33
  • Hi Zomb, thanks for your reply. The problem was solved by manually pairing tablet with device. – Magic Mar 26 '14 at 20:06
  • I'm having trouble writing multiple bytes to a characteristic at a time. Does this piece of code work for you? byte[] value = {(byte)300,(byte)100,(byte)100}; characteristic.setValue(value); boolean status = mBluetoothGatt.writeCharacteristic(characteristic); – deadbabykitten Jun 20 '14 at 20:55
  • It looks as if createBond was added according to the documentation via SDK 19, are you experiencing any issues with 18 at all with this? – AllDayAmazing Jan 27 '15 at 06:33

1 Answers1

1

(Answered by question edit. Converted to a community wiki answer. See What is the appropriate action when the answer to a question is added to the question itself? )

The OP wrote:

Follow Up:

The problem solved by manually pairing the tablet with the device first in the setting instead of pairing by code.

So only using the code snippet of connecting Gatt provided by Android is not good enough to pair the device. I should add another code I found online to pair the devices if I don't want to pair them manually every time:

private void pairDevice(BluetoothDevice device) {
    try {
        Log.d("pairDevice()", "Start Pairing...");
        Method m = device.getClass()
                .getMethod("createBond", (Class[]) null);
        m.invoke(device, (Object[]) null);
        Log.d("pairDevice()", "Pairing finished.");
    } catch (Exception e) {
        Log.e("pairDevice()", e.getMessage());
    }
}
Community
  • 1
  • 1
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129