IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

API standards et tierces Android Discussion :

Bluetooth: Activation réussite / Désactivation failed


Sujet :

API standards et tierces Android

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2014
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2014
    Messages : 5
    Points : 5
    Points
    5
    Par défaut Bluetooth: Activation réussite / Désactivation failed
    Bonjour,

    J'arrive à activer le bluetooth mais pas à le désactiver et ça commence à me rendre fou si vous pouviez m'aider

    Voici mon code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    import android.os.Bundle;
    import android.provider.MediaStore.Audio;
    import android.app.Activity;
    import android.content.Context;
    import android.view.*;
    import android.widget.*;
    import android.media.*;
    import android.telephony.*;
    import android.bluetooth.*;
    import android.content.*;
     
    public class MainActivity extends Activity implements View.OnClickListener{
    	Button b1,b2,b3,b4;
    	LinearLayout llt;
    	String send;
    	AudioManager audio;
    	BluetoothAdapter bt;
    	Intent enableBtIntent,disableIntent;
     
     
     
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
     
    		llt= new LinearLayout(this);
    		llt.setGravity(Gravity.TOP);
    		llt.setOrientation(LinearLayout.VERTICAL);
     
     
    		audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    		bt = BluetoothAdapter.getDefaultAdapter();  
    		enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    		disableIntent  = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED);
     
     
     
    		TextView tv1=new TextView(this);
    		tv1.setText("Controle du Bluetooth");
    		llt.addView(tv1);
     
    		b3=new Button(this);
    		b3.setText("Bluetooth ON");
    		b3.setOnClickListener(this);
    		b3.setId(3);
    		llt.addView(b3);
     
    		b4=new Button(this);
    		b4.setText("Bluetooth OFF");
    		b4.setOnClickListener(this);
    		b4.setId(4);
    		llt.addView(b4);
     
     
    		setContentView(llt);
    	}
    	public void onClick(View v){
     
     
    		// Bluetooth
     
    		if(v.getId()==3){
    			send=("Bluetooth activé");
    			if (!bt.isEnabled()) {
    	            startActivityForResult(enableBtIntent, BluetoothAdapter.STATE_TURNING_ON);
    	        }
    			}
     
    			if(v.getId()==4){
    				send=("Bluetooth désactivé");
    				if (bt.isEnabled()) {
    			        bt.disable();
    			}
                             }
     
     
     
     
     
    		TextView txt= new TextView(this);
    		txt.setText(send);
    		llt.addView(txt);
    	}
     
    }
    J'ai essayé de l'activer avec bt.enable(); Mais ça ne fonctionne pas.

    Acton56

  2. #2
    Expert éminent

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Points : 9 149
    Points
    9 149
    Par défaut
    Salut,

    bt.enable(); Ne jamais utiliser !!!

    public boolean enable ()

    Added in API level 5
    Turn on the local Bluetooth adapter—do not use without explicit user action to turn on Bluetooth.

    This powers on the underlying Bluetooth hardware, and starts all Bluetooth system services.

    Bluetooth should never be enabled without direct user consent. If you want to turn on Bluetooth in order to create a wireless connection, you should use the ACTION_REQUEST_ENABLE Intent, which will raise a dialog that requests user permission to turn on Bluetooth. The enable() method is provided only for applications that include a user interface for changing system settings, such as a "power manager" app.

    This is an asynchronous call: it will return immediately, and clients should listen for ACTION_STATE_CHANGED to be notified of subsequent adapter state changes. If this call returns true, then the adapter state will immediately transition from STATE_OFF to STATE_TURNING_ON, and some time later transition to either STATE_OFF or STATE_ON. If this call returns false then there was an immediate problem that will prevent the adapter from being turned on - such as Airplane mode, or the adapter is already turned on.

    Requires the BLUETOOTH_ADMIN permission

    Returns
    true to indicate adapter startup has begun, or false on immediate error
    Appelle par conséquent l'intent sur ACTION_REQUEST_ENABLE et ajoute la permission BLUETOOTH_ADMIN.
    Responsable Android de Developpez.com (Twitter et Facebook)
    Besoin d"un article/tutoriel/cours sur Android, consulter la page cours
    N'hésitez pas à consulter la FAQ Android et à poser vos questions sur les forums d'entraide mobile d'Android.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Activation et désactivation javascript
    Par LudwigVon88 dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 27/01/2006, 22h34
  2. Réponses: 4
    Dernier message: 29/10/2005, 18h03
  3. Activer et désactiver des composant graphiques?
    Par redabadache3 dans le forum Débuter
    Réponses: 1
    Dernier message: 16/08/2005, 17h50
  4. activer ou désactiver des items d'un menu
    Par gcore dans le forum Composants
    Réponses: 3
    Dernier message: 08/06/2004, 14h07
  5. Réponses: 2
    Dernier message: 12/02/2004, 13h07

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo