2017-12-09 3 views
0

私の仕事は、&を私のアプリケーションからオフにして、ペアリングされたデバイスを検索することです。ペアのデバイスが見つかった場合は、そのデバイスに接続します。それ以外の場合は、近くにある使用可能なデバイスを検出し、それらの信号強度とともにリストします。これらの信号強度をN秒ごとに更新します。ユーザーが特定のデバイスを選択すると、接続が確立されます。BroadcastReceiverのonReceiveメソッドは決して呼び出されません

package surendra.example.com.mybluetooth; 

import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.content.SharedPreferences; 
import android.hardware.camera2.params.BlackLevelPattern; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

import org.w3c.dom.Text; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.Set; 

public class MainActivity extends AppCompatActivity { 

    protected static final int DISCOVERY_REQUEST = 1; 

    private BluetoothDevice device; 

    //Variable for text display 
    public TextView statusUpdate; 
    public TextView BTHostDetails; 
    public TextView BTDevicesAvailable; 

    //List of buttons 
    public Button turnONBT; 
    public Button turnOFFBT; 
    public Button scanBTDevices; 
    public Button seeRSSI; 
    public Button back; 

    ArrayAdapter<String> btArrayAdapter; 
    //BluetoothAdapter object that initializes the BT hardware on the device 
    private BluetoothAdapter mBluetoothAdapter; 

    //Broadcast the Bluetooth activities 
    BroadcastReceiver bluetoothState = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      String prevStateExtra = BluetoothAdapter.EXTRA_PREVIOUS_STATE; 
      String stateExtra = BluetoothAdapter.EXTRA_STATE; 

      int state = intent.getIntExtra(stateExtra, -1); 
      int previousState = intent.getIntExtra(prevStateExtra, -1); 

      String toastText = ""; 

      switch (state) { 
       case(BluetoothAdapter.STATE_TURNING_ON) : { 
        //Already turned ON 
        toastText = "Turning ON Bluetooth"; 
        Toast.makeText(MainActivity.this, toastText, Toast.LENGTH_SHORT).show(); 
        break; 
       } 
       case(BluetoothAdapter.STATE_ON): { 
        //Turned ON 
        toastText = "Bluetooth turned ON"; 
        Toast.makeText(MainActivity.this, toastText, Toast.LENGTH_SHORT).show(); 
        setupUI(); 
        break; 
       } 
       case(BluetoothAdapter.STATE_TURNING_OFF) : { 
        //Already turned OFF 
        toastText = "Turning OFF Bluetooth"; 
        Toast.makeText(MainActivity.this, toastText, Toast.LENGTH_SHORT).show(); 
        break; 
       } 
       case(BluetoothAdapter.STATE_OFF) : { 
        //Turned OFF 
        toastText = "Bluetooth turned OFF"; 
        Toast.makeText(MainActivity.this, toastText, Toast.LENGTH_SHORT).show(); 
        setupUI(); 
        break; 
       } 
      } 
     } 
    }; 

    //Represents a remote BT device 
    private BluetoothDevice remoteDevice; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //Get default BT adapter 
     mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

     setupUI(); 
    } //end onCreate method 

    //Method to setup UI 
    private void setupUI() { 
     //Get references of each button and text view from the UI 
     final TextView statusUpdate = (TextView) findViewById(R.id.statusUpdate); 
     final TextView BTHostDetails = (TextView) findViewById(R.id.BTHostDetails); 
     final Button turnONBT = (Button) findViewById(R.id.turnONBT); 
     final Button turnOFFBT = (Button) findViewById(R.id.turnOFFBT); 
     final Button scanBTDevices = (Button) findViewById(R.id.scanBTDevices); 
     final Button seeRSSI = (Button) findViewById(R.id.seeRSSI); 
     final Button back = (Button) findViewById(R.id.back); 

     //Set visibilities of each button 
     turnOFFBT.setVisibility(View.GONE); 
     seeRSSI.setVisibility(View.GONE); 
     back.setVisibility(View.GONE); 
     scanBTDevices.setVisibility(View.GONE); 
     BTHostDetails.setVisibility(View.GONE); 

     //Check if the BT adapter is enabled 
     if(mBluetoothAdapter.isEnabled()) { 

      //If the BT adapter is enabled, get the name & address of the device 
      String devName = mBluetoothAdapter.getName(); 
      String devAddr = mBluetoothAdapter.getAddress(); 

      String statusText = devName + " : " + devAddr; 
      BTHostDetails.setText(statusText); 

      //set the visibilities of the buttons based upon BT status 
      BTHostDetails.setVisibility(View.VISIBLE); 
      turnONBT.setVisibility(View.GONE); 
      turnOFFBT.setVisibility(View.VISIBLE); 
      seeRSSI.setVisibility(View.GONE); 
      back.setVisibility(View.VISIBLE); 
      scanBTDevices.setVisibility(View.VISIBLE); 
     } else { 
      String tmp = "Bluetooth turned OFF"; 
      statusUpdate.setText(tmp); 
     } 

     // We should keep on listening to the TURN ON BLUETOOTH button. 
     // When the button turn on bluetooth is clicked, we should proceed to turn Bluetooth ON 
     turnONBT.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       //This makes our device discoverable 
       String beDiscoverable = BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE; 

       //Intent is the pop up that we see when requesting for permission to use 
       //certain interface on the device just like permission for location access etc. 
       //Using the intent filter, we can filter the devices whose permissions are changed 
       //when the user updates on the intent 
       IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 

       //This registers a broadcast receiver 
       registerReceiver(bluetoothState, filter); 

       //If the intent changes from non-discoverable to discoverable, with the filter 
       // DISCOVERY_REQUEST, then start the activity. 
       startActivityForResult(new Intent(beDiscoverable), DISCOVERY_REQUEST); 

       //Change the buttons views 
       BTHostDetails.setVisibility(View.VISIBLE); 
       turnOFFBT.setVisibility(View.VISIBLE); 
       turnONBT.setVisibility(View.INVISIBLE); 
       scanBTDevices.setVisibility(View.VISIBLE); 
       seeRSSI.setVisibility(View.VISIBLE); 
       back.setVisibility(View.INVISIBLE); 

       String tmp = "Bluetooth turned ON"; 
       statusUpdate.setText(tmp); 
      } 
     }); 

     //scan devices 
     scanBTDevices.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       //start discovery of new devices 
       mBluetoothAdapter.startDiscovery(); 

       //see if this device is in a list of paired available devices 
       Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
       //Toast.makeText(MainActivity.this, "Debug text 0", Toast.LENGTH_SHORT).show(); 
       if(pairedDevices.size() > 0) { 
        //This means that there are paired devices. 
        // Get the name & address of each device. 
        for(BluetoothDevice device : pairedDevices) { 
         String devName = device.getName(); 
         String devAddr = device.getAddress(); 
        } 

        List<String> s = new ArrayList<String>(); 
        for(BluetoothDevice bt : pairedDevices) { 
         s.add(bt.getName()); 
        } 
       } 
       BroadcastReceiver discoveryResult = new BroadcastReceiver() { 

        @Override 
        public void onReceive(Context context, Intent intent) { 
         String action = intent.getAction(); 

         if(BluetoothDevice.ACTION_FOUND.equals(action)) { 

          //BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
          device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
          short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE); 
          String devName = device.getName(); 
          String devAddr = device.getAddress(); 

          String tmp = devName + " : " + devAddr + " - " + rssi; 
          statusUpdate.setText(tmp); 
         } 
        } 
       }; 
      } 
     }); 

     //Setup a listener for turning of Bluetooth i.e., for TURN OFF BLUETOOTH button 
     turnOFFBT.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       //Change the buttons views 
       BTHostDetails.setVisibility(View.INVISIBLE); 
       turnOFFBT.setVisibility(View.INVISIBLE); 
       turnONBT.setVisibility(View.VISIBLE); 
       scanBTDevices.setVisibility(View.INVISIBLE); 
       seeRSSI.setVisibility(View.INVISIBLE); 
       back.setVisibility(View.INVISIBLE); 

       String tmp = "Bluetooth turned OFF"; 
       statusUpdate.setText(tmp); 

       mBluetoothAdapter.disable(); 
      } 
     }); 

     //Back button activity 
     back.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       setupUI(); 
      } 
     }); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if(requestCode == DISCOVERY_REQUEST) { 
      String tmp = "Discovery is in progress"; 
      Toast.makeText(MainActivity.this, tmp , Toast.LENGTH_SHORT).show(); 
      setupUI(); 
     } 
    } 
} 

デバッグ時に、私はBroadcastReceiverのonReceiveメソッドが呼び出されないことがわかりました。ただし、Bluetoothは有効です。私は何が欠けていますか?

PS:すべてのBTアクティビティをMainActivityだけに限定したいと思います。

+0

あなたは2つの 'BroadcastReceivers'を持っています。 1つの 'registerReceiver()'コールしかありません。 – CommonsWare

+0

これはJavaやAndroidのいずれかでやっている最初のコードなので、これを自分の仕様に合わせて管理する方法を教えてもらえれば、本当にとても役に立ちます。ありがとうございました! –

答えて

2

デバッグ時に、BroadcastReceiverのonReceiveメソッドが呼び出されないことがわかりました。ただし、Bluetoothは有効です。

最初に、Bluetoothが有効になっていて、Bluetoothの状態が変更されていない場合は、ブロードキャストを受信するとは思われません。 「BluetoothAdapter.ACTION_STATE_CHANGEDブロードキャストis documentedは、「ローカルBluetoothアダプタの状態が変更されました」と送信されます。変更がなければブロードキャストを意味しません。

第2に、ユーザーがturnONBTをクリックした場合、bluetoothStateBroadcastReceiverとして登録します。その前に状態が変わった場合、その放送は受信されません。

第3に、間違った操作のためにbluetoothStateを登録します。あなたはBluetoothDevice.ACTION_FOUNDのためにそれを登録しています。そのアクションは、the documentationで説明されているように、Bluetooth検出用です。ブルートゥース状態の変更の動作はBluetoothAdapter.ACTION_STATE_CHANGEDです。あなたのdiscoveryResultは発見のためのようであり、その受信機を登録することは決してありません。

これは、私はどちらかのJavaやAndroid

をやっている初のコードであるように私はは、Bluetoothを含むものは、JavaやAndroidであなたの最初のプロジェクトであることをお勧めしません。

+0

あなたが言及した2番目のポイントの解決方法は? –

+0

@surendranath: 'onCreate()'のような他のポイントで登録します。 – CommonsWare

関連する問題