简体   繁体   English

Android BLE,无法真正写出特色

[英]Android BLE, can not really write characteristic

I'm working on a Android project which is to connect Nexus 7 and a bio sensor through BLE link. 我正在研究一个通过BLE链接连接Nexus 7和生物传感器的Android项目。 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. 当我将一些数据写入特定的特性时, onCharacteristicWrite会自动调用并显示我的写操作是成功的。 However, the sensor never receive anything from the tablet. 但是,传感器从未从平板电脑接收任何东西。 And if I use similar app on iPhone, everything works fine. 如果我在iPhone上使用类似的应用程序,一切正常。 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. 我还尝试一次写一个字节,或者添加一个计时器让平板电脑每隔2秒写入传感器。 But none of them worked. 但它们都没有奏效。 Any ideas? 有任何想法吗?

(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: OP写道:

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. 因此,仅使用Android提供的连接Gatt的代码片段并不足以配对设备。 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()); } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM