2017-05-12 25 views
0

最近、私はアンドロイドBLEでアプリケーションを開発します。 しかし、私は問題を抱えている。..BLE `onCharacteristicRead`が動作しない

ここ

コードがある....

// Device connect call back 
private final BluetoothGattCallback btleGattCallback = new BluetoothGattCallback() { 

    @Override 
    public void onCharacteristicChanged(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { 
     // this will get called anytime you perform a read or write characteristic operation 
       peripheralTextView.append("device read or wrote to\n"); 
       broadcastUpdate(ACTION_DATA_AVAILABLE,characteristic); 

    } 

    @Override 
    public void onConnectionStateChange(final BluetoothGatt gatt, final int status, final int newState) { 
     // this will get called when a device connects or disconnects 
     System.out.println(newState); 
     switch (newState) { 
      case BluetoothProfile.STATE_DISCONNECTED: 
       MainActivity.this.runOnUiThread(new Runnable() { 
        public void run() { 
         peripheralTextView.append("device disconnected\n"); 
         connectToDevice.setVisibility(View.VISIBLE); 
         disconnectDevice.setVisibility(View.INVISIBLE); 
        } 
       }); 
       break; 
      case BluetoothProfile.STATE_CONNECTED: 
       MainActivity.this.runOnUiThread(new Runnable() { 
        public void run() { 
         peripheralTextView.append("device connected\n"); 
         connectToDevice.setVisibility(View.INVISIBLE); 
         disconnectDevice.setVisibility(View.VISIBLE); 
        } 
       }); 

       // discover services and characteristics for this device 
       bluetoothGatt.discoverServices(); 

       break; 
      default: 
       MainActivity.this.runOnUiThread(new Runnable() { 
        public void run() { 
         peripheralTextView.append("we encounterned an unknown state, uh oh\n"); 
        } 
       }); 
       break; 
     } 
    } 

    @Override 
    public void onServicesDiscovered(final BluetoothGatt gatt, final int status) { 
     // this will get called after the client initiates a   BluetoothGatt.discoverServices() call 
     MainActivity.this.runOnUiThread(new Runnable() { 
      public void run() { 
       peripheralTextView.append("device services have been discovered\n"); 
      } 
     }); 
     displayGattServices(bluetoothGatt.getServices()); 
    } 

    @Override 
    // Result of a characteristic read operation 
    public void onCharacteristicRead(BluetoothGatt gatt, 
            BluetoothGattCharacteristic characteristic, 
            int status) { 
     if (status == BluetoothGatt.GATT_SUCCESS) { 
      broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); 
     } 
    } 
}; 

private void broadcastUpdate(final String action, 
          final BluetoothGattCharacteristic characteristic) { 
    // For all other profiles, writes the data formatted in HEX. 
    final byte[] data = characteristic.getValue(); 
    if (data != null && data.length > 0) { 
     final StringBuilder stringBuilder = new StringBuilder(data.length); 
     for (byte byteChar : data) 
      stringBuilder.append(String.format("%02X ", byteChar)); 
      System.out.println(stringBuilder.toString()); 
    } 
} 

..........

public void disconnectDeviceSelected() { 
    peripheralTextView.append("Disconnecting from device\n"); 
    bluetoothGatt.disconnect(); 
} 

private void displayGattServices(List<BluetoothGattService> gattServices) { 
    if (gattServices == null) return; 

    // Loops through available GATT Services. 
    for (BluetoothGattService gattService : gattServices) { 

     final String uuid = gattService.getUuid().toString(); 
     System.out.println("Service discovered: " + uuid); 
     MainActivity.this.runOnUiThread(new Runnable() { 
      public void run() { 
       peripheralTextView.append("Service disovered: "+uuid+"\n"); 
      } 
     }); 
     new ArrayList<HashMap<String, String>>(); 
     List<BluetoothGattCharacteristic> gattCharacteristics = 
       gattService.getCharacteristics(); 

     // Loops through available Characteristics. 
     for (BluetoothGattCharacteristic gattCharacteristic : 
       gattCharacteristics) { 

      final String charUuid = gattCharacteristic.getUuid().toString(); 
      System.out.println("Characteristic discovered for service: " + charUuid); 
      MainActivity.this.runOnUiThread(new Runnable() { 
       public void run() { 
        peripheralTextView.append("Characteristic discovered for service: "+charUuid+"\n"); 
       } 
      }); 

     } 
    } 
} 

\、私はこのコードを実行します。 ..

スキャンと接続が完了しました。 しかしonCharacteristicReadは動作しません。!( は私が

+1

あなたが応答を受信するために、BLEデバイスに任意のコマンドを送信している –

+0

@HiteshGehlotはい、私はデータを送信する周辺機器をチェックし...;( –

+0

私はあなたがどこでも「readCharacteristic」と呼ぶが表示されません。.. 。 – Emil

答えて

0

コールonServicesDiscoveredでこの方法をあなたの助けのプラザを必要とし、このメソッドがtrueを返すならば、BLEデバイスにコマンドを送信

public Boolean enableTXNotification() { 
     /* 
     if (mBluetoothGatt == null) { 
      showMessage("mBluetoothGatt null" + mBluetoothGatt); 
      broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART); 
      return; 
     } 
      */ 
     BluetoothGattService RxService = mBluetoothGatt.getService(Lock.RX_SERVICE_UUID); 
     if (RxService == null) { 
      showMessage("Rx service not found!"); 
      broadcastUpdate(Lock.DEVICE_DOES_NOT_SUPPORT_UART); 
      return false; 
     } 
     BluetoothGattCharacteristic TxChar = RxService.getCharacteristic(Lock.TX_CHAR_UUID); 
     if (TxChar == null) { 
      showMessage("Tx charateristic not found!"); 
      broadcastUpdate(Lock.DEVICE_DOES_NOT_SUPPORT_UART); 
      return false; 
     } 
     mBluetoothGatt.setCharacteristicNotification(TxChar, true); 

     BluetoothGattDescriptor descriptor = TxChar.getDescriptor(Lock.CCCD); 
     descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 
     mBluetoothGatt.writeDescriptor(descriptor); 
     return true; 
    } 

は、あなたが持っているなら、私に知らせて?何の疑いも

関連する問題