通过android从ble设备发送和接收数据

时间:2015-03-14 18:25:59

标签: java android bluetooth

我正在将BLE计步器连接到Android手机。我发现了ble设备并能够连接到它。我发送20字节数据,但是 我怎样才能检查ble是否能够接收数据。

public  void writeDataToPedometer(BluetoothDevice device) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            Log.d(TAG, "Inside Thread's catch");
            e.printStackTrace();
        }
        BluetoothGattService mGattService = mBluetoothGatt.getService(UUID_PEDOMETER_SERVICE);
        mtransmitData = new TransmitData();
        if(mGattService == null) {
           Log.e("YHService", "SEND UUID Service not found");
        } else {
           BluetoothGattCharacteristic characterstic = mGattService.getCharacteristic(UUID.fromString("0000ffb2-0000-1000-8000-00805f9b34fb"));
           if(characterstic == null) {
              Log.e("YHService", "SET_TIME characterstic not found");
           } else {

             byte command[] = new byte[20]; 
             command[0] = 42;
             command[1] = 41;
             command[2] = 54;
             command[3] = 54;
             command[4] = 45 ;
             command[5] = 52 ;
             command[6] = 59 ;

             for (int i = 7 ;i < 20 ; i++ )
                 command[i] = 0x00;

              characterstic.setValue(command);

              try {
                 Thread.sleep(500L);
              } catch (InterruptedException var9) {
                  Log.d("Service", "Inside Catch");
                 var9.printStackTrace();
              }

              mBluetoothGatt.writeCharacteristic(characterstic);
              setRecieverNotify(mGattService);
              setCharacteristicNotification(characterstic, true);

           }
        }
     }

我正在使用上面的功能将数据发送到ble设备。上面的功能可以发送数据,或者我应该对其进行一些修改。

我应该在哪里寻找计步器的回复。当计步器发送数据时,是否会调用任何函数。

1 个答案:

答案 0 :(得分:0)

实施此callBack: public final BluetoothGattCallback btleGattCallback = new BluetoothGattCallback() { @Override public void onCharacteristicWrite(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) { if(status==BluetoothGatt.GATT_SUCCESS) { //this callback gets called after a successful write operation is performed } } }