Lo sketch Arduino e' stato modificato per esporre una caratteristica 2A30 in modalita' lettura/scrittura, settare il valore a 3, leggere il valore e se e' differente da 3 spengere il Led di Arduino
=================================================================
#include <ArduinoBLE.h>
BLEService numero("180F");
BLEUnsignedCharCharacteristic casuale("2A19",BLERead | BLENotify);
BLEUnsignedCharCharacteristic scrittura("2A30",BLERead | BLEWrite);
long previousMillis = 0;
byte test =3 ;
void setup() {
Serial.begin(9600);
while (!Serial);
pinMode(LED_BUILTIN, OUTPUT);
if (!BLE.begin()) {
while (1);
}
BLE.setLocalName("Random");
BLE.setAdvertisedService(numero);
numero.addCharacteristic(casuale);
numero.addCharacteristic(scrittura);
BLE.addService(numero);
casuale.writeValue(0); // set initial value for this characteristic
scrittura.writeValue(2);
BLE.advertise();
}
void loop() {
int i = 0;
BLEDevice central = BLE.central();
if (central) {
while (central.connected()) {
long currentMillis = millis();
if (currentMillis - previousMillis >= 20) {
previousMillis = currentMillis;
scrittura.readValue(test);
if(test ==3)
{
Serial.println(test);
digitalWrite(LED_BUILTIN, HIGH);
}
else
{
Serial.println(test);
digitalWrite(LED_BUILTIN, LOW);
}
}
}
digitalWrite(LED_BUILTIN, LOW);
}
}
=================================================================
Per Android ho usato la libreria a questo indirizzo . Ne esistono di piu' complete ma
in questo caso avevo bisogno di semplicita'
=================================================================
package com.ble.ellamma.ble; import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log; import com.ble.ellamma.bleand.Ble;
import java.util.UUID;
public class MainActivity extends AppCompatActivity { private static final String MAC_ADDRESS = "ED:CB:86:2A:68:C1"; private static final UUID BLE_NOTIFY_CHARACTER = UUID.fromString("00002a19-0000-1000-8000-00805f9b34fb"); //.00002a19-0000-1000-8000-00805f9b34fb--18 private static final UUID BLE_SERVICE = UUID.fromString("0000180f-0000-1000-8000-00805f9b34fb"); private static final UUID CLIENT_CCD = UUID.fromString("00002a30-0000-1000-8000-00805f9b34fb"); ArrayList<String> getDevicess; List<BluetoothDevice> getConnectedDevicess; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); getDevicess = new ArrayList<>();
getConnectedDevicess = new ArrayList<>(); Ble ble = new Ble(this,getApplicationContext(),".Myreceiver"); ble.enableBle();
//ble.scanLeDevice(true,1000);
//ble.getDevices(); ble.connectBLE(MAC_ADDRESS);
byte dati[] = "A".getBytes();
ble.writeCharacteristics(dati,BLE_SERVIVE,CLIENT_CCD,true);
ble.disConnectBLE(); } public void onReceive(Context context, Intent intent) { //String data = intent.getStringExtra("data");
//Log.w("Luca", "Request Recieved On MyReciever ::" + data); } }
Nessun commento:
Posta un commento