2017-10-20 27 views
1

Je crée une application qui doit se connecter via Bluetooth à un périphérique spécifique.Association d'un périphérique Bluetooth à Android Studio

Je souhaite que mon application se connecte à cet appareil, qu'il soit déjà couplé ou non.

Pour l'instant j'ai ce

private void findDevice() { 
    Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices(); 
    if (pairedDevices.size() > 0) { 
     for (BluetoothDevice device : pairedDevices) { 
      if (device.getName().equals(DEVICE_NAME)) { 
       bluetoothDevice = device; 
       deviceFound = true; 
       break; 
      } 
     } 
    } 
} 

Mais cette fonction se connecte uniquement aux appareils associés. Si mon appareil n'est pas déjà couplé, je souhaite l'associer. Je n'ai aucune idée de comment faire cela.

Quelqu'un peut-il me donner des conseils s'il vous plaît?

+0

Avez-vous demandé l'autorisation BLUETOOTH_ADMIN? – nhoxbypass

+0

oui, j'ai demandé – Kirchhoff1415

+0

Avez-vous essayé: https://stackoverflow.com/questions/14228289/android-pair-devices-via-bluetooth-programmatically – nhoxbypass

Répondre

1

Première demande BLUETOOTH_ADMIN permission.

Faites ensuite votre appareil découvrable:

private void makeDiscoverable() { 
     Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); 
     discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); 
     startActivity(discoverableIntent); 
     Log.i("Log", "Discoverable "); 
    } 

Ensuite, créez un BroadcastReceiver écouter à l'action du système:

private BroadcastReceiver myReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      Message msg = Message.obtain(); 
      String action = intent.getAction(); 
      if(BluetoothDevice.ACTION_FOUND.equals(action)){ 
       //Found, add to a device list 
      }   
     } 
    }; 

et commencer à rechercher des appareils en vous inscrivant ce BoardcastReceiver:

private void startSearching() { 
     Log.i("Log", "in the start searching method"); 
     IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
     BluetoothDemo.this.registerReceiver(myReceiver, intentFilter); 
     bluetoothAdapter.startDiscovery(); 
    } 

Après appareils viennent du BroadcastReceiver dans une liste, sélectionnez votre appareil dans la liste et createBond() avec ceci:

public boolean createBond(BluetoothDevice btDevice) 
    throws Exception 
    { 
     Class class1 = Class.forName("android.bluetooth.BluetoothDevice"); 
     Method createBondMethod = class1.getMethod("createBond"); 
     Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice); 
     return returnValue.booleanValue(); 
    } 

Ensuite, utilisez votre code ci-dessus pour gérer avec des périphériques liés.

+0

homme, merci beaucoup, semble légitime. Mais ayant un problème avec BroadcastReciever. Android Studio indique que je ne peux pas résoudre un symbole 'BroadcastReciever'. Im une recherche sur Google pour la réponse, mais ne peux pas trouver un :( – Kirchhoff1415

+0

@ Kirchhoff1415 ce problème – nhoxbypass

+0

ok, il a obtenu, je manquais android.content.BroadcastReceiver d'importation;? partie – Kirchhoff1415