2015-04-24 2 views
3

J'essaie de faire vibrer mon téléphone et d'afficher une notification si ma tablette se déconnecte du téléphone via Bluetooth. Cela fonctionne si je déconnecte ma tablette de mon téléphone via le menu Bluetooth de ma tablette, à la fois lorsque l'écran de mon téléphone est éteint (j'ai éteint mon téléphone pendant une demi-heure puis déconnecté ma tablette du menu Bluetooth de ma tablette) et il fonctionne). Cela fonctionne également si je déconnecte mon téléphone de ma tablette via le menu Bluetooth de mon téléphone. Cela fonctionne aussi si je m'éloigne de ma tablette en tenant mon téléphone et en gardant l'écran de mon téléphone éteint. Cependant, si je m'éloigne de ma tablette avec l'écran de mon téléphone éteint, aucune vibration ne se produit. La notification apparaît cependant, car je vérifie mon téléphone après quelques minutes et la notification est là avec l'horodatage correct (ainsi la notification n'apparaît pas seulement quand je réveille mon téléphone). Je suis complètement déconcerté.Vibreur après la déconnexion du Bluetooth ne fonctionne pas toujours

Voici mon code correspondant:

public class BluetoothService extends Service { 

public final BroadcastReceiver BluetoothScanner = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String trueAction = intent.getAction(); 
     if(BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(trueAction)){ 

      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      String deviceName = device.getName(); 
       dcNotify(deviceName); 

       Toast.makeText(context, deviceName + " has disconnected", Toast.LENGTH_LONG).show(); 

     } 

    } 

}; 


public void dcNotify(String s) { 
    Log.d("status", "commenced"); 
    int notificationId = 1; 
    NotificationCompat.Builder notificationBuilder = 
      new NotificationCompat.Builder(this) 
        .setContentTitle("Device Disconnected") 
        .setSmallIcon(R.mipmap.ic_launcher); 

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); 

    String contentText = "Disconnected from " + s; 

     notificationBuilder.setContentText(contentText); 

     long[] pattern = { 0, 100, 500, 100, 500, 100, 500}; 
     Notification notification = notificationBuilder.build(); 
     notification.vibrate = pattern; 
     notification.defaults |= Notification.DEFAULT_VIBRATE; 
     notificationManager.notify(notificationId, notification); 
} 
} 
+0

Juste curieux de savoir si vous avez essayé cela avec un autre téléphone Android? Se pourrait-il que vos paramètres particuliers remplacent votre application? – DigitalNinja

+0

ouais j'ai essayé ceci avec un Oppo Find 7 et un Nexus 4 aussi (mon appareil principal est un OnePlus One) – user2524995

Répondre

0

Ne pas oublier d'activer le réglage de vibration pour sonnerie et notifications. Allez dans Paramètres -> Son. Cochez "Sonner une vibration".

https://stackoverflow.com/a/13905212/972311

également sans rapport avec mais utiliser le modèle de constructeur:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this) 
    .setContentTitle("Device Disconnected") 
    .setSmallIcon(R.mipmap.ic_launcher) 
    .setContentText(contentText) 
    .setVibrate(pattern) 
    .setDefaults(Notification.DEFAULT_VIBRATE); 
+0

C'était en fait ma mise en œuvre initiale et il a exactement le même problème – user2524995