Il progetto BTSwitch nasce dalla necessità di poter controllare l’accensione e lo spegnimento di dispositivi elettrici (lampade…) tramite uno smartphone Android.
Ecco un filmato descrittivo del progetto, nelle prossime pagine troverete invece i dettagli tecnici… buona visione e buona lettura!
Ciao Luca,
Trovo molto interessante questo tuo tutoria,
ache io ho fatto alcuni programmini Android per comandare degli attuatori via SMS con l’eventuale stato delle uscite.
Premetto che non sono molto esperto nella programmazione Android, e solo da qualche mese che ho cominciato.
Ho scaricato il tuo sorgente e aprendolo con Eclipse, non ho nessun problema, quando lo invio all’SDK per la simulazione, l’app si richiude immediatamente previo finestra di dialogo.
Sul LogCat non riesco a capire il motivo, e nel codice non mi sembra che vi siano errori.
Puoi darmi una dritta?
Saluti
Ciao Domenico!
purtroppo l’emulatore incluso nell’SDK non ha il modulo Bluetooth. La mia app come prima cosa verifica la sua presenza e, non trovandolo, si chiude.
Ciao Luca,
In effetti a questo non ci avevo pensato.
Domani creo l’apk e lo metto nello smart e riprovo.
SALUTI
Ciao Luca,
In effetti era così, creato l’apk e inserendolo nel mio smartphone, funziona tutto.
Saluti
Nice project, but using Arduino for it is wating of money – simple AT Tiny 45 with software serial would be far enough and you can also keep same program.
Hi Ivan
thanks for your feedback!
Ciao Luca,
Sono di nuovo io, per chiederti info su un caso singolare.
Istallato il BTSwitch .apk sul mio Galaxy s2, l’applicazione funziona correttamente, mentre su un Galaxy tab 2 P3110, nonostate abbia stabilito la connessione come dispositivo associato, nell’aplicazione esce un toast con ” non nè possibile effettuare la connessione” dopo aver inserito la passward. Mi sono letto le caratteristiche tecniche ed entrambi i dispositivi hanno un modulo bluetooth ver. 3.0.
Ora no riesco a capire questa differenza, nel terminal rimane bloccato a:
Incoming data -> EADY.
mentre con il galaxy s2 si connette visualizzandomi:
Incoming data -> RING 0 84:25:db:1d:a4:08 1 RFCOMM.
Sai darmi una spiegazione?
Ti rigrazio in anticipo.
Saluti.
Ciao Domenico,
veramente strano, sembra che il modulo non riceva mai la richiesta di connessione.
Puoi fare questa prova? Scarica l’app BlueTerm e prova a collegarti con quella. Una volta connesso invia il carattere ?, dovrebbe risponderti con !
fammi sapere!
Ciao luca,
Niente da fare, anche col Blue term, ho lo stesso effetto, riconosce il dispositivo e dopo avermi chiesto la pass, dice che non è possibile collegarsi; ma l’associazione, è stata eseguita correttamente perchè lo vedo nelle impostazioni BlueTooth.
Ora no so come fare.
ciao
davvero strano… ho provato anche con un tablet Nexus7 di un collega e tutto funziona. Che versione di Android ha il tuo?
Ciao Luca,
Ha la 4.0.3 me è uguale a quella del mio galaxy S2 che funziona regolarmente.
Saluti
Ciao Luca,
Ho letto un pò in giro, e sembrerebbe che l’android 4.0.3 ICS, che è quello che monta il galaxy tab 2 ma non quello dell’S2, abbia in effetti qualcosa di diverso nella gestione bluetooth durante il riconoscimento della periferica.
Ti risulta? Ma anche se fosse come possiamo ovviare al problema secondo te ?
Saluti
Ciao Domenico,
prova così: metti come API level almeno 10, quindi cambia la riga
btSocket = device.createRfcommSocketToServiceRecord(SPP_UUID);
in
btSocket = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
fammi sapere, sembra proprio un bug di Android!
Ciao Luca,
Ora l’applicazione si interrompe con un toast che dice “Il probramma BTSwitch sarà arrestato”.
Ho provato a modificare ne progetto compilandolo con android 4.0.3 API 15 ma il risultato è lo stesso.
No so
Ciao Luca,
Ora funziona tutto, chiedo umilmente scusa, avevo fatto uno stupido errore di Copia/Incolla.
Ti ringrazio tantissimo, ma se è lecito, posso sapere come hai fatto a trovare la soluzione ?
Saluti e grazie ancora.
Ciao!
ricordavo un differente metodo per stabilire la connessione Bluetooth con le API > 10: in effetti tutte le versioni di Android tranne la 4.0.3 “traducono” il vecchio metodo in quello che ti ho indicato. La 4.0.3 ha invece un bug che si aggira appunto chiamando esplicitamente il metodo nuovo.
bye
Hi Luca
I tried to follow your instructions. But my bluetooth module is not answering.
Maybe the problem is the module. Your WT11 is no longer available. I ordered then a WT11i-A-AI5. Its the current WT11 with iWRAP 5 firmware. Was this the wrong module?
I tested it on a Arduino Uno and Arduino Mega2560. On the serial port of WT11i will be send these lines:
SET BT NAME BTSwitch
SET PROFILE SPP ON
SET BT AUTH * 1234
RESET
SET
But the request wt11Serial.available() is always 0. Because of this my Android only finds this module WT11i-A (the name is not changed to BTSwitch).
Do you have an idea how to fix it?
Thank you in advance.
Peter
Hi Peter
your bluetooth module should work fine… have you an usb->serial converter you can use to connect WT11i to your PC? It’s easier to debug using a terminal emulator instead of using Arduino like in my tutorial about that module.
Hi Luca
Thank you for your fast reply. The serial adapter helps a lot.
I solved it now partly. In my module was the baud rate 115200 by default. I only had to change this in your code.
But now I have the problem that my Nexus 4 Android phone don’t ask for the PIN while pairing. I don’t know if it is because one setting of the WT11 or caused by the Nexus 4.
When I open your app it will show me these lines and will not connect:
RING 0 10:68:3f:56:9e:98 1 RFCOMM
?
NO CARRIER 0 ERROR 0
Could you maybe post our WT11 settings? On my module it is the following:
SET BT BDADDR 00:07:80:5e:35:44
SET BT NAME BTSwitch
SET BT CLASS 001f00
SET BT AUTH * 1234
SET BT IDENT BT:47 f000 5.0.0 Bluegiga iWRAP
SET BT LAP 9e8b33
SET BT PAGEMODE 4 2000 1
SET BT PAIR 10:68:3f:56:9e:98 7d53dfa33182b65ccf47e5b85f4bde66
SET BT PAIR 00:1c:26:e8:89:c4 847c391b95231a5ae863031279f8ef1c
SET BT POWER 17 17 17
SET BT ROLE 0 f 7d00
SET BT SNIFF 0 20 1 8
SET BT SSP 3 0
SET BT MTU 667
SET CONTROL BAUD 115200,8n1
SET CONTROL CD 00 0
SET CONTROL ECHO 7
SET CONTROL ESCAPE 43 00 1
SET CONTROL GAIN 0 5
SET CONTROL MSC DTE 00 00 00 00 00 00
SET CONTROL PIO 00 00
SET CONTROL READY 00
SET PROFILE SPP Bluetooth Serial Port
Thank you in advance.
Peter
Hi Luca
I solved it now.
1. WT11 is only working fine trough software serial if the baud rate is smaller than 115200 baud. Now I also use 9600. With hardware serial a higher baud rate is no problem.
2. I use a Arduino Mega -> Software Serial can not be PIN 2 and 3. I used 10 and 11 and it works fine.
3. In Android 4.2 on the Nexus 4 there are “new” Pairing mechanism. There is no pairing where I had to type the PIN. I tried other pairing mechanism with this command “SET BT SSP”. But there the Arduino has to accept the phone.
Okay now it is running. Thanks
Thanks Peter for your update… about the pairing problem, it changed the way an android app has to pair to a new device, so my app doesn’t work for Android 4x. I’m going to update it, please try the new version when it’ll be published on the market and let me know if it works for you
ciao Luca, complimenti per il tuo tutorial, è possibile utilizzare una scheda arduino uno nel progetto BTSwitch?
nel caso fosse possibile, potresti gentilmente inoltrarmi sia lo schema che i collegamenti?
grazie in anticipo
Dino
Ciao Dino! Intendi al posto dell’Arduino Mini che ho utilizzato? Assolutamente sì, i collegamenti sono uguali.
Non vedo l’ora di poter vedere il tuo tutorial sulla gestione dei dispositivi bluetooth con Android