Issue
I made a connections between a Arduino and Android application.
I used bluetoothGATT connection successfully, but there is a question why it shows me
many characteristic service when I connect to Arduino. I just set Arduino's charateristic
as 19b10001-e8f2-537e-4f6c-d104768a1214
. So I thought it will show only 19b10001-e8f2-537e-4f6c-d104768a1214
if I connect with BLE. Contrary to what I think, it shows me other different characteristic.
this is my results on screen.
This is my codes on MainActivity.java's BluetoothGattCallback
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@SuppressLint("MissingPermission")
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
runOnUiThread(new Runnable() {
public void run() {
logAdapter.add("BluetoothCallBack - newState : " + String.valueOf(newState));
}
});
switch (newState){
case 0:
runOnUiThread(new Runnable() {
public void run() {
logAdapter.add("device disconnected");
}
});
break;
case 2:
runOnUiThread(new Runnable() {
public void run() {
logAdapter.add("device connected");
}
});
btGatt.discoverServices();
break;
default:
runOnUiThread(new Runnable() {
public void run() {
logAdapter.add("unknown error : " + String.valueOf(newState));
}
});
break;
}
logAdapter.notifyDataSetChanged();
}
@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() {
tvState.setText("device services have been discovered");
}
});
displayGattServices(btGatt.getServices());
}
};
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() {
logAdapter.add("Service discovered: "+uuid);
logAdapter.notifyDataSetChanged();
}
});
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() {
logAdapter.add("Characteristic discovered for service: "+charUuid+"\n");
logAdapter.notifyDataSetChanged();
}
});
}
}
}
and this is my Arduino's code
#include <ArduinoBLE.h>
enum {
GESTURE_NONE = -1,
GESTURE_UP = 0,
GESTURE_DOWN = 1,
GESTURE_LEFT = 2,
GESTURE_RIGHT = 3
};
const char* deviceServiceUuid = "19b10000-e8f2-537e-4f6c-d104768a1214";
const char* deviceServiceCharacteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1214";
int gesture = -1;
BLEService gestureService(deviceServiceUuid);
BLEByteCharacteristic gestureCharacteristic(deviceServiceCharacteristicUuid, BLERead | BLEWrite);
void setup() {
Serial.begin(9600);
while (!Serial);
if (!BLE.begin()) {
Serial.println("- Starting Bluetooth® Low Energy module failed!");
while (1);
}
BLE.setLocalName("Arduino");
BLE.setAdvertisedService(gestureService);
gestureService.addCharacteristic(gestureCharacteristic);
BLE.addService(gestureService);
gestureCharacteristic.writeValue(-1);
BLE.advertise();
Serial.println("Nano 33 BLE (Peripheral Device)");
Serial.println(" ");
Serial.println("- Discovering central device...");
}
void loop() {
BLEDevice central = BLE.central();
delay(500);
gesture = 10;
if (central) {
Serial.println("* Connected to central device!");
Serial.print("* Device MAC address: ");
Serial.println(central.address());
Serial.println(" ");
while (central.connected()) {
if (gestureCharacteristic.written()) {
gesture = gestureCharacteristic.value();
}
// gestureCharacteristic.writeValue((byte)gesture);
// Serial.println("* Writing value to gesture_type characteristic done!");
// Serial.println(" ");
// delay(2000);
}
Serial.println("* Disconnected to central device!");
}
}
Can someone explain me why there are many charateristic discovered??
Solution
Some services might be enabled by default on your Arduino through the Bluetooth Stack you are using. When using a generic BLE scanner app such as nRF Connect it is possible to decode their purpose without looking up the UUID manually. For example:
The service with the UUID 00001801-0000-1000-8000-00805f9b34fb
would show up as Generic Access Service.
You can find more pre-defined numbers over at the Assigned Numbers section of the Bluetooth specification. The document "16-bit UUIDs" contains the short version (16-bit) of services including 0x1801 for the Generic Access Service mentioned earlier.
You can simply ignore all services that you do not need (and did not create yourself)
Answered By - Michael Kotzjan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.