===================================Py - Scan dispositivi BLE================================= import asyncio from bleak import BleakScanner async def main(): devices = await BleakScanner.discover() for device in devices: print(device) asyncio.run(main()) ===================================Risultato=================================================== 44:7C:63:97:6D:3D: [TV] UE48|S8500 48:69:31:F1:1D:2D: [LG] webOS TV UP78006LB 5A:C1:D7:5C:BE:07: None 25:4B:03:3C:02:5F: None F4:12:FA:6F:76:D9: R4 ====================Py Stampa i servizi e le caratteristiche di un dispositivo BLE================= import asyncio from bleak import * MAC = 'F4:12:FA:6F:76:D9' async def bleGATT(): client = BleakClient(MAC) await client.connect() for service in client.services: print("[Service] {0}: {1}".format(service.uuid, service.description)) for char in service.characteristics: print("\t[characteristic] ", char) for prp in char.properties: print ("\t\t[property] ",prp) await client.disconnect() asyncio.run(bleGATT()) ===============Risultato================================================================= [Service] 00001800-0000-1000-8000-00805f9b34fb: Generic Access Profile [characteristic] 00002a00-0000-1000-8000-00805f9b34fb (Handle: 2): Device Name [property] read [characteristic] 00002a01-0000-1000-8000-00805f9b34fb (Handle: 4): Appearance [property] read [Service] 00001801-0000-1000-8000-00805f9b34fb: Generic Attribute Profile [characteristic] 00002a05-0000-1000-8000-00805f9b34fb (Handle: 7): Service Changed [property] indicate [Service] 19b10000-e8f2-537e-4f6c-d104768a1214: Unknown [characteristic] 19b10001-e8f2-537e-4f6c-d104768a1214 (Handle: 11): Unknown [property] read [property] write ====================Py Invio Dati e risposta===================================================== #in caso di sent string controllare il max size sul ricevitore async def bleWriteData(CUSTOM_SERVICE_CHAR_UUID,data): client = BleakClient(MAC) await client.connect() await client.write_gatt_char(CUSTOM_SERVICE_CHAR_UUID,data=data) value = await client.read_gatt_char(CUSTOM_SERVICE_CHAR_UUID)###...se voglio la risposta dal device print(value) ### await client.disconnect() asyncio.run(bleWriteData("19B10001-E8F2-537E-4F6C-D104768A1214",b"hello")) ============================Risultato========================================= Disconnected From central: 3c:a0:67:8f:e2:5a Connected to central: 3c:a0:67:8f:e2:5a Disconnected From central: 3c:a0:67:8f:e2:5a Connected to central: 3c:a0:67:8f:e2:5a hello Disconnected From central: 3c:a0:67:8f:e2:5a Connected to central: 3c:a0:67:8f:e2:5a Disconnected From central: 3c:a0:67:8f:e2:5a Connected to central: 3c:a0:67:8f:e2:5a hello wo ###Ho superato la dimensione della caratteristica che era 8 caratteri Disconnected From central: 3c:a0:67:8f:e2:5a ==========================Risultato================================================= #include // Define the name of the Bluetooth peripheral const char* peripheralName = "R4"; // Create a BLE Service BLEService fileTransferService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Create a BLE Characteristic for receiving data const char* dataCharacteristicUUID = "19B10001-E8F2-537E-4F6C-D104768A1214"; //BLECharacteristic dataCharacteristic(dataCharacteristicUUID, BLEWrite | BLERead, ""); //BLEByteCharacteristic dataCharacteristic(dataCharacteristicUUID, BLEWrite | BLERead);//Se voglio comunicare Bytes BLEStringCharacteristic dataCharacteristic(dataCharacteristicUUID, BLEWrite | BLERead,8);//Se voglio comunicare Stringhe void setup() { // Initialize the Serial communication for debugging Serial.begin(9600); // Initialize the BLE library if (!BLE.begin()) { Serial.println("Starting BLE failed!"); while (1); } // Set the local name of the Bluetooth peripheral BLE.setLocalName(peripheralName); BLE.setAdvertisedService(fileTransferService); // Add the characteristics to the service fileTransferService.addCharacteristic(dataCharacteristic); // add service BLE.addService(fileTransferService); // set the initial value for the characteristic: //dataCharacteristic.writeValue(0); // Start advertising BLE.advertise(); Serial.println("Bluetooth peripheral advertising..."); } void printHex(uint8_t num) { char hexCar[2]; sprintf(hexCar, "[%02X]", num); Serial.print(hexCar); } void loop() { BLEDevice central = BLE.central(); if (central) { Serial.print("Connected to central: "); Serial.println(central.address()); while (central.connected()) { if (dataCharacteristic.written()) { ////dataCharacteristic.writeValue((byte)0x01);//Sevoglio rispondere con byte dataCharacteristic.writeValue("RESP"); //Rsipondo con stringa Serial.println( dataCharacteristic.value()); } } Serial.print(F("Disconnected From central: ")); Serial.println(central.address()); } }