*CompileSDK = API25(7.1)
*MinSDK = API18(4.3)
*TargetSDK = API22(5.1)
저는 BLE 통신을 하는 앱을 제로베이스(자바도 처음, 안드로이드스튜디오도 처음)로 개발중인데요ㅠ_ㅠ
질문의 의도는, 어떻게 제가 선택한 service의 characteristic에 값을 write할 수 있는가, 입니다.
github에서 찾은 BluetoothLeGatt 프로젝트를 사용하면, scan-connect-read까지 할 수 있는데
제가 사용하는 BLE 통신을 할 모듈은 하나의 service를 가지고 있고
그 service에는 3개의 characteristic이 들어 있습니다.
2개는 센서에서 data를 받아오고, 나머지 하나는 enable용으로 사용합니다.
configuration을 담당하는 characteristic에 0x01을 적어줘야만 해당 서비스 안의 다른 measurement를 담당하는 characteristic이 enable되기 때문에 꼭 write를 해야하는데, 그 방법을 모르겠습니다.
github나 google 등에서 ble write를 한 예제를 찾아보려해도 습득하기가 어려운데 도움을 부탁드립니다.
+제가 사용한 프로젝트는 https://github.com/googlesamples/android-BluetoothLeGatt 에서 받았습니다.
---------------------------------------------------------------
추가로 https://github.com/TokenJan/BLE_Android_App 에서 read/write를 제공한다고 해서 돌려봤는데
실제로 저는 read도 안 되고, write도 제대로 안 된다고 생각해서 코드를 수정하다가
private void setIntensity(final BluetoothGattCharacteristic characteristic){
LayoutInflater li = LayoutInflater.from(DeviceControlActivity.this);
View promptsView = li.inflate(R.layout.prompt, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(DeviceControlActivity.this);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
final int intensity_val = Integer.parseInt(userInput.getText().toString());
//characteristic.setValue(intensity, format, 0);
mBluetoothLeService.writeCharacteristic(characteristic,intensity_val);
dialog.cancel();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_WRITE) ==10) {
if (mNotifyCharacteristic != null) {
mBluetoothLeService.setCharacteristicNotification(
mNotifyCharacteristic, false);
mNotifyCharacteristic = null;
}
setIntensity(characteristic);
mBluetoothLeService.readCharacteristic(characteristic);
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status){
if(status == BluetoothGatt.GATT_SUCCESS){
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
}
public void writeCharacteristic(BluetoothGattCharacteristic characteristic,int intensity) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
// characteristic.setValue(intensity, BluetoothGattCharacteristic.FORMAT_UINT8, 0);
// mBluetoothGatt.writeCharacteristic(characteristic);
byte[] value = new byte[1];
value[0] = 0x01;
byte value2 = 0x01;
value2 = 0x01;
characteristic.setValue(value);
Log.w(TAG, characteristic.getUuid().toString());
boolean status = mBluetoothGatt.writeCharacteristic(characteristic);
Log.w(TAG, "status"+status);
}
write함수를 들어가는지 status로 확인해보다가 겨우 true를 return 받게 되었는데 그러면 disconnect이 됩니다. 도와주세요ㅠㅡㅠ
