2016-09-07 15 views
0

アプリケーションの再起動時に、既に接続されているBluetoothデバイスに自動接続するようにします。以下は私が実行している手順です: -Android:ブロードキャストレシーバは、アプリケーションの再起動時にBluetoothDevice.ACTION_ACL_CONNECTEDを受信しません。

  1. [最初に] Bluetoothデバイスが「ON」の場合:アプリケーションの起動時に起動します。
    は、[動作] - > Bluetoothデバイスがペアリングされ、正常に接続された(意図「ACTION_ACL_CONNECTED」が受信される)を取得

  2. Bluetoothデバイスが「ON」である:再びアプリを開始し、その後、アプリケーションを閉じました。
    - > Bluetooth設定で表示されているように接続されていても、放送受信側は意図 'ACTION_ACL_CONNECTED'を受信しません。

注: - アプリケーションを閉じると、Bluetooth接続が切断されません。 これで、接続に成功するとすぐにホームスクリーンに移動します。それ以外の場合、アプリケーションはボタンを持った画面に移動し、Bluetooth設定(下のコードにあるonClickListener)に移動します。

私はアンドロイドの開発に慣れていないので、どこが間違っているのか分かりません。私は同様の問題の解決策を探して適用しましたが、効果はありません。

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_index); 
     mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED); 
     registerReceiver(mReceiver, filter); 
     IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED); 
     this.registerReceiver(mReceiver, filter1); 
     m_app = (BtApp) getApplication(); 
     imagebt = (ImageView) this.findViewById(R.id.imagebt); 

     imagebt.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       final Toast tag = Toast.makeText(getApplicationContext(), "Connect to device", Toast.LENGTH_LONG); 
       tag.show(); 

       new CountDownTimer(1000, 1000) 
       { 

        public void onTick(long millisUntilFinished) {tag.show();} 
        public void onFinish() { 
         //tag.show(); 
        } 

       }.start(); 

       if(mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()){ 
        mBluetoothAdapter.startDiscovery(); 
       } 

       Intent intentBluetooth = new Intent(); 
       intentBluetooth.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS); 
       startActivity(intentBluetooth); 

       } 
     }); 

    } 

private BroadcastReceiver mReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 
      if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) { 
       m_app.m_main.setupCommPort(); 
       device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
       m_app.m_device = device; 
       isconnected = true; 
       new Timer().schedule(new TimerTask() { 
        @Override 
        public void run() { 
         if (m_app.m_main.m_BtService != null && m_app.m_main.m_BtService.getState() != BluetoothRFCommService.STATE_CONNECTED) { 
          m_app.m_main.m_BtService.connect(device, false); 
         } 
        } 
       }, 3500); 

      } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) { 
       isconnected = false; 
       m_app.m_main.tabHost.setCurrentTab(0); 
      } 

     } 
    }; 


@Override 
    protected void onStop() 
    { 
     unregisterReceiver(mReceiver); 
     super.onStop(); 
    } 

答えて

0

デバイスがまだ接続されているため、BluetoothDevice.ACTION_ACL_CONNECTEDイベントは発生しません。イベントは、切断状態から接続状態へのデバイス状態の変更時にのみ発生します。

2つの選択肢があります。

  1. あなたはServiceにあなたのBluetoothDevice.ACTION_ACL_CONNECTEDBroadcastReceiverBluetoothDevice.ACTION_ACL_DISCONNECTEDフィルタを入れて、バックグラウンドでのデバイスの接続状態を追跡することができます。あなたのアプリの起動時に、あなたにデバイスの現在の状態を伝えるようにサービスに頼むことができます。

  2. 一部のBluetoothプロファイルに、接続されているデバイスのリストにデバイス名が含まれているかどうかを確認できます。あなたは

    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() { 
        public void onServiceConnected(int profile, BluetoothProfile proxy) { 
         for (BluetoothDevice device : proxy.getConnectedDevices()) { 
          if (device.getName().contains("DEVICE_NAME")) { 
           deviceConnected = true; 
          } 
         } 
    
         if (!deviceConnected) { 
          Toast.makeText(getActivity(), "DEVICE NOT CONNECTED", Toast.LENGTH_SHORT).show(); 
         } 
         mBluetoothAdapter.closeProfileProxy(profile, proxy); 
    
        } 
    
        public void onServiceDisconnected(int profile) { 
         // TODO 
        } 
    }; 
    
    mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.A2DP); 
    
    (各Bluetoothプロファイルのために)次のスニペットを使用することができる18の下にAPIのためBluetoothManager#getConnectedDevices()を使用することができ18+ APIについては

関連する問題