如何在按钮点击时将数据写入特征?

时间:2016-01-02 09:42:07

标签: java android bluetooth bluetooth-lowenergy

我正在制作一个使用BLE的Android应用程序。我试图在点击按钮时将数据写入特征,但无法执行此操作。

我有一个具有两个特征的服务,在这两个特征之外我在按钮点击上写一个数据。

Ble class我已经定义了writeDatatoCharacteristic

/* set new value for particular characteristic */
public void writeDataToCharacteristic(final BluetoothGattCharacteristic ch, final byte[] value) {

    if (mBluetoothAdapter == null || mBluetoothGatt == null || ch == null) {
        return;
    }

    // first set it locally....
    ch.setValue(value);


    // ... and then "commit" changes to the peripheral
    mBluetoothGatt.writeCharacteristic(ch);
}

按钮的

 <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButtonPlay"
        android:src="@drawable/play"
        android:background="@android:color/transparent"
        android:layout_alignTop="@+id/ivRestart"
        android:layout_centerHorizontal="true"
        android:onClick="onClickWrite"/>

我将调用数据写入特征函数的主要活动

public class Tens_modes extends AppCompatActivity {

private final String LOGTAG = "BLETEST";
private final String TARGET = "CC2650 SensorTag";
private BleWrapper mBleWrapper = null;
private mSensorState mState;
private String gattList = "";
private TextView mTv;

private enum mSensorState {IDLE, ACC_ENABLE, ACC_READ};

public final static UUID
        UUID_ACC_SERV = fromString("FFE0"),
        UUID_ACC_DATA = fromString("f000aa11-0451-4000-b000-000000000000"),
        UUID_ACC_CONF = fromString("FFE9"), // 0: disable, 1: enable
        UUID_ACC_PERI = fromString("f000aa13-0451-4000-b000-000000000000"); // Period in tens of milliseconds




private ImageButton play;
private ImageButton pause;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tens_modes);

    play = (ImageButton)findViewById(R.id.imageButtonPlay);
    pause = (ImageButton)findViewById(R.id.imageButtonPause);


    play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            play.setVisibility(View.INVISIBLE);
            pause.setVisibility(View.VISIBLE);

    pause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pause.setVisibility(View.INVISIBLE);
            play.setVisibility(View.VISIBLE);


    //Ble stuff

    mBleWrapper = new BleWrapper(this, new BleWrapperUiCallbacks.Null()
    {

        @Override
        public void uiDeviceFound(final BluetoothDevice device, final int rssi, final byte[] record)
        {

            Log.d(LOGTAG, "uiDeviceFound: " + device.getName() + ", " + rssi + ", " + record.toString());
            if (device.getName().equals(TARGET))
            {
                if (!mBleWrapper.connect(device.getAddress()))
                {
                    Log.d(LOGTAG, "uiDeviceFound: Problem connecting to remote device.");
                }
            }
            //stopScan();
        }
        @Override
        public void uiDeviceConnected(BluetoothGatt gatt, BluetoothDevice device)
        {
            Log.d(LOGTAG, "uiDeviceConnected: State = " + mBleWrapper.getAdapter().getState());

        }

        @Override
        public void uiDeviceDisconnected(BluetoothGatt gatt, BluetoothDevice device) {
            Log.d(LOGTAG, "uiDeviceDisconnected: State = " + mBleWrapper.getAdapter().getState());
        }

        @Override
        public void uiAvailableServices(BluetoothGatt gatt, BluetoothDevice device, List<BluetoothGattService> services)
        {
            BluetoothGattCharacteristic c;
            BluetoothGattDescriptor d;

            for (BluetoothGattService service : services)
            {
                String serviceName = BleNamesResolver.resolveUuid(service.getUuid().toString());
                Log.e(LOGTAG, serviceName);
                gattList += serviceName + "\n";

                mBleWrapper.getCharacteristicsForService(service);
            }

            // enable services
            Log.e(LOGTAG, "uiAvailableServices: Enabling services");
            c = gatt.getService(UUID_ACC_SERV).getCharacteristic(UUID_ACC_CONF);

            mBleWrapper.writeDataToCharacteristic(c, new byte[] {0300000000AA});

            //mState = mSensorState.ACC_ENABLE;



            // set notification on characteristic
            //Log.d(LOGTAG, "uiAvailableServices: Setting notification");
            //c = gatt.getService(UUID_IRT_SERV).getCharacteristic(UUID_IRT_DATA);
            //mBleWrapper.setNotificationForCharacteristic(c, true);

            // enable notification on descriptor
            //d = c.getDescriptor(UUID_CCC_DESC);
            //d.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            //gatt.writeDescriptor(d);
        }

        @Override
        public void uiCharacteristicForService( BluetoothGatt gatt,
                                                   BluetoothDevice device,
                                                   BluetoothGattService service,
                                                   List<BluetoothGattCharacteristic> chars)
        {
            super.uiCharacteristicForService(gatt, device, service, chars);
            for (BluetoothGattCharacteristic c : chars)
            {
                String charName = BleNamesResolver.resolveCharacteristicName(c.getUuid().toString());
                Log.d(LOGTAG, charName);
                gattList += "Characteristic: " + charName + "\n";
            }
        }


        @Override
        public void uiSuccessfulWrite(  BluetoothGatt gatt,
                                          BluetoothDevice device,
                                          BluetoothGattService service,
                                          BluetoothGattCharacteristic ch,
                                          String description)
        {
            BluetoothGattCharacteristic c;

            super.uiSuccessfulWrite(gatt, device, service, ch, description);
            Log.d(LOGTAG, "uiSuccessfulWrite");

            switch (mState)
            {
                case ACC_ENABLE:
                    Log.d(LOGTAG, "uiSuccessfulWrite: Reading acc");
                    c = gatt.getService(UUID_ACC_SERV).getCharacteristic(UUID_ACC_DATA);
                    mBleWrapper.requestCharacteristicValue(c);
                    mState = mSensorState.ACC_READ;
                    break;

                case ACC_READ:
                    Log.d(LOGTAG, "uiSuccessfulWrite: state = ACC_READ");
                    break;

                default:
                    break;
            }
        }

        @Override
        public void uiFailedWrite(  BluetoothGatt gatt,
                                      BluetoothDevice device,
                                      BluetoothGattService service,
                                      BluetoothGattCharacteristic ch,
                                      String description)
        {
            super.uiFailedWrite(gatt, device, service, ch, description);
            Log.d(LOGTAG, "uiFailedWrite");
        }

        @Override
        public void uiNewValueForCharacteristic(BluetoothGatt gatt,
                                                BluetoothDevice device,
                                                BluetoothGattService service,
                                                BluetoothGattCharacteristic ch,
                                                String strValue,
                                                int intValue,
                                                byte[] rawValue,
                                                String timestamp)
        {
            super.uiNewValueForCharacteristic(gatt, device, service, ch, strValue, intValue, rawValue, timestamp);
            Log.d(LOGTAG, "uiNewValueForCharacteristic");
            for (byte b:rawValue)
            {
                Log.d(LOGTAG, "Val: " + b);
            }
        }

        @Override
        public void uiGotNotification(  BluetoothGatt gatt,
                                          BluetoothDevice device,
                                          BluetoothGattService service,
                                          BluetoothGattCharacteristic characteristic)
        {
            super.uiGotNotification(gatt, device, service, characteristic);
            String ch = BleNamesResolver.resolveCharacteristicName(characteristic.getUuid().toString());

            Log.d(LOGTAG,  "uiGotNotification: " + ch);
        }
    });

}

public void onClickWrite(View v){
    if(mBleWrapper != null) {
        // enable services
            Log.e(LOGTAG, "uiAvailableServices: Enabling services");
            byte[] value = new byte[1];
            value[0] = (byte)21;
            mBleWrapper.writeDataToCharacteristic(UUID_ACC_CONF, new byte[] {0300000000AA});

    }
}

我想写0300000000AA作为特征值,但不知道如何做到这一点。在过去的4,5个小时内搜索它但没有得到任何有用的东西

如果有人能帮助我,请告诉我我做错了什么 感谢

1 个答案:

答案 0 :(得分:2)

这应该为你做。 (好一个开始,我不确定编码)

byte[] temp;
//This will at least compile, however I don't know if that is what you want, strings in java are unicode, not ascii. you might have to convert to your desired encoding
temp = "0300000000AA".getBytes();
mBleWrapper.writeDataToCharacteristic(UUID_ACC_CONF, temp );