1

我正嘗試從我的android應用程序連接到Bleno Periphecal GATT服務器。Android應用程序:使用UUID連接到GATT服務器

GATT服務器具有自定義服務和具有唯一UUID的特性。

如何確定連接到此服務器併發送一些文本?

最小的SDK是21,目標SDK是24,所以舊的BluetoothLE掃描方法已被廢棄,現在我需要使用BluetoothLEScanner。

回答

1

對於連接Ble,只需使用此方法傳遞您的BT設備的MAC地址即可。

private boolean connectGatt(final String address) { 
    if (mBluetoothAdapter == null || address == null) { 
     Log.w(TAG, "BluetoothAdapter not initialized or unspecified address."); 
     return false; 
    } 

    if (mBluetoothGatt != null) { 
     Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection."); 
     if (mBluetoothGatt.connect()) { 
      return true; 
     } else { 
      return false; 
     } 
    } 

    final BluetoothDevice device = mBluetoothAdapter 
      .getRemoteDevice(address); 
    if (device == null) { 
     Log.w(TAG, "Device not found. Unable to connect."); 
     return false; 
    } 

    mBluetoothGatt = device.connectGatt(mContext, false, mGattCallback); 
    Log.d(TAG, "Trying to create a new connection."); 
    return mBluetoothGatt.connect(); 
} 

你應該註冊一個回調以知道連接是否成功。

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { 
    @Override 
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 

     if (newState == BluetoothProfile.STATE_CONNECTED) { 
      //bluetooth is connected so discover services 
      mBluetoothGatt.discoverServices(); 

     } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { 
      //Bluetooth is disconnected 
     } 
    } 

    @Override 
    public void onServicesDiscovered(BluetoothGatt gatt, int status) { 
     if (status == BluetoothGatt.GATT_SUCCESS) { 
      // services are discoverd 
     } 


    } 

    @Override 
    public void onCharacteristicRead(BluetoothGatt gatt, 
            BluetoothGattCharacteristic characteristic, 
            int status) { 
     if (status == BluetoothGatt.GATT_SUCCESS) { 

     } 
    } 

    @Override 
    public void onCharacteristicChanged(BluetoothGatt gatt, 
             BluetoothGattCharacteristic characteristic) { 

    } 

    @Override 
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
     super.onCharacteristicWrite(gatt, characteristic, status); 

    } 
}; 

一旦發現服務,您可以寫入或讀取服務。

寫入到一個服務使用這種方法

private boolean writeRXCharacteristic(byte[] value) { 
    BluetoothGattService RxService = mBluetoothGatt.getService(/*Place service UUID*/); 
    if (RxService == null) { 
     //Service not supported 
     return false; 
    } 
    BluetoothGattCharacteristic RxChar = RxService.getCharacteristic(/*RX_CHAR_UUID*/); 
    if (RxChar == null) { 
     // service not supported 
     return false; 
    } 
    RxChar.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE); 
     RxChar.setValue(arr); 
     return iGatt.writeCharacteristic(iChar); 
} 
+0

謝謝你的建議。 iGatt,iChar和arr從哪裏來?我對這些事情並不熟悉。我需要在writeRXCharacteristic函數中初始化這些值嗎?另外什麼時候應該調用writeRXCharacteristic函數? –

相關問題