0

我在我的項目中使用了android.bluetooth包,但我嘗試實現IBluetoothGatt的讀寫特性。但我有一個像下面IBluetoothGatt接口Android藍牙低能耗Gatt服務執行錯誤

public final class BluetoothGatt implements BluetoothProfile { 
    private static final String TAG = "BluetoothGatt"; 
    private static final boolean DBG = true; 
    private static final boolean VDBG = false; 

    private IBluetoothGatt mService; // IBluetoothGatt red highlights. Some functions in IBluetoothGatt interface just work by put breakpoints. 

    private BluetoothGattCallback mCallback; 
    private int mClientIf; 
    private boolean mAuthRetry = false; 
    private BluetoothDevice mDevice; 
    private boolean mAutoConnect; 
    private int mConnState; 
    private final Object mStateLock = new Object(); 
    private Boolean mDeviceBusy = false; 
    private int mTransport; 

    private static final int CONN_STATE_IDLE = 0; 
    private static final int CONN_STATE_CONNECTING = 1; 
    private static final int CONN_STATE_CONNECTED = 2; 
    private static final int CONN_STATE_DISCONNECTING = 3; 
    private static final int CONN_STATE_CLOSED = 4; 

    private List<BluetoothGattService> mServices; 

writeCharacteristic紅色亮點

public void onCharacteristicWrite(String address, int status, int handle) { 
      if (VDBG) Log.d(TAG, "onCharacteristicWrite() - Device=" + address 
         + " handle=" + handle + " Status=" + status); 

      if (!address.equals(mDevice.getAddress())) { 
       return; 
      } 

      synchronized(mDeviceBusy) { 
       mDeviceBusy = false; 
      } 

      BluetoothGattCharacteristic characteristic = getCharacteristicById(mDevice, handle); 
      if (characteristic == null) return; 

      if ((status == GATT_INSUFFICIENT_AUTHENTICATION 
       || status == GATT_INSUFFICIENT_ENCRYPTION) 
       && mAuthRetry == false) { 
       try { 
        mAuthRetry = true; 
        mService.writeCharacteristic(mClientIf, address, handle, 
         characteristic.getWriteType(), AUTHENTICATION_MITM, 
         characteristic.getValue()); 
        return; 
       } catch (RemoteException e) { 
        Log.e(TAG,"",e); 
       } 
      } 
+1

你到底想要做什麼 –

+0

我想用寫入特徵方法與ble設備進行通信。但在Ibluetoothgatt接口(在android.bluetooth中)編寫特徵方法「無法解決」,我不能這樣做。 – Hilal

+0

編輯您的代碼並編寫完整的活動和服務代碼。 在寫入數據之前,您需要掃描並連接到設備。 –

回答

0

使用這篇文章是非常有用的一些問題。如果您有任何問題

這裏 BLE

評論是連接到BLE裝置的代碼。

public boolean connect(final String address) 
{ 

    if (mBluetoothAdapter == null || address == null) 
    { 
     Log.e(TAG, "BluetoothAdapter not initialized or unspecified address."); 
     return false; 
    } 
    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); 
    if (device == null) 
    { 
     Log.e(TAG, "Device not found. Unable to connect."); 
     return false; 
    } 
    // We want to directly connect to the device, so we are setting the autoConnect 
    // parameter to false. 
    mBluetoothGatt = device.connectGatt(this, false, mGattCallback); 

    return true; 
} 
+0

我試過你的建議設備已連接,但我在BluetothGatt.java中有同樣的錯誤,就像定義IBluetoothGatt(紅色),clientConnect在IBluetoothGattCallback方法中是紅色的,我無法寫入任何設備。 – Hilal

+0

IBluetoothGatt在Android中是內部的,而不是公共API的一部分。因此你不應該在IDE中打開這個文件。 BluetoothGatt也不是您應該在IDE中打開的文件。如果你不能編譯你的項目,你應該再看看你的SDK設置。 – Emil