簡體   English   中英

通過android從ble設備發送和接收數據

[英]Sending and receiving data from a ble device over android

我正在將BLE計步器連接到Android手機。 我發現了ble設備並能夠連接到它。 我正在向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設備。上面的功能是否可以發送數據,或者我應該對其進行一些修改。

我應該在哪里尋找計步器的答復。 計步器發送數據時會調用任何功能嗎?

實現此回調: 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 } } }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM