2016-04-09 14 views
0

私はArduinoのBluetoothデバイスに接続するアプリを書いています。目標は、Androidユーザーが電話がArduinoの範囲を離れるとプッシュ通知を受け取ることです。これは、アプリケーションがフォアグラウンドにあるかどうかに関係なく発生するはずです。これを行うために、私は現在Android ManifestでBroadcastReceiverを使用しています。しかし、私はそのような通知を受けていません。ここでAndroid - Bluetooth接続の消失を確認しますか?

はBroadcastReceiverを実装レシーバクラスである:

public class Receiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 
     String action = intent.getAction(); 
     if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) { 
      if (adapter.getState() == BluetoothAdapter.STATE_OFF) { 
       pushNotification(context); 
      } 
     } 
    } 

    public void pushNotification(Context context) { 

     NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 
     builder.setSmallIcon(R.mipmap.ic_launcher); 

     builder.setAutoCancel(true); 

     builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher)); 

     builder.setContentTitle("This is a notification!"); 
     builder.setContentText("This is the notification text!"); 
     builder.setSubText("This is the notification subtext!"); 

     NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); 
     notificationManager.notify(1, builder.build()); 
    } 
} 

とのAndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> 

    <uses-permission android:name="android.permission.BLUETOOTH"/> 
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 
    <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <receiver 
      android:name=".Receiver" 
      android:enabled="true" 
      android:exported="true"> 
      <intent-filter> 
       <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" /> 
      </intent-filter> 
     </receiver> 
    </application> 

</manifest> 

私は、問題は、私は私のロジックで使用していた定数であることをかなり確信しています。しかし、私はどちらを使うべきかわからない。つまり、Android Bluetoothに何らかの状態変化が起こったときに受信機が作動しますが、Android Bluetooth受信機の状態変更と関係のない接続が失われたときに通知するだけです。 これらの条件でpushNotification()が呼び出されるようにするにはどうすればよいですか?

+0

[Android:シャットダウン/ Bluetooth接続の切断またはファイルの受信 - >何かをする](http://stackoverflow.com/questions/2905240/android-shut-down-loss-of-bluetooth-connection) -or-ファイル受信者ive-do-somethin) –

答えて

0

あなたは誤った意図を登録しています。

<action android:name="android.bluetooth.adapter.action.STATE_CHANGED" /> 

この意図は状態だけを接続して、あなたが行動に従わ使用する必要がありますが、Bluetoothデバイスを受信したいBluetoothが上とoff.Ifですwheather示します

:あなたonRecive方法で

<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" /> 
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" /> 

if(TextUtils.equals(action,BluetoothDevice.ACTION_ACL_DISCONNECTED)) { 
    BluetoothDevice device = intent.getExtras() 
            .getParcelable(BluetoothDevice.EXTRA_DEVICE); 
    if (isYourDevice(device)) { 
     // to push your notification 
    } 
} 
関連する問題