Error receiving broadcast Intent { act=android.bluetooth.device.action.FOUND flg=0x10}

▼魔方 西西 提交于 2019-12-23 20:22:12

问题


I searched and i didn't find anything symilar. I'm developing a connection between two defices using Bluetooth in Android. And on ACTION_FOUND method, when i tried to make the device information appear on listView, i got that error on logcat.

DesafioActivity: public class DesafioActivity extends Activity {

  private TextView nomeDispositivo;
  private TextView MAC_Adress;
  private BluetoothAdapter bthAdapter = BluetoothAdapter.getDefaultAdapter();
  private ArrayAdapter<String> bthDispositivosArea;

  private ListView lv_devicesArea;
  private IntentFilter filter;

  private Button btn_servidor, btn_cliente;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_desafio);

    nomeDispositivo = (TextView)findViewById(id.tv_apresentaNomeDispositivo);
    MAC_Adress = (TextView)findViewById(id.tv_apresentaMacAdress);
    lv_devicesArea = (ListView)findViewById(id.lv_apresentaDispositivosDescobertos);

    VerificarEstadoBth();

    nomeDispositivo.setText(bthAdapter.getName());
    MAC_Adress.setText(bthAdapter.getAddress());

    //Regista o BroadcastReceiver

    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothDevice.ACTION_UUID);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    registerReceiver(HandleDiscovery, filter); // Don't forget to unregister during onDestroy

    bthAdapter.startDiscovery();
  }

  public void VerificarEstadoBth(){

    //Bluetooth não suportado 
    if (bthAdapter == null) {
      Toast.makeText(this, "DEVIDE DOESN'T SUPPORT", Toast.LENGTH_LONG).show();
    }
    // Ativa e torna o dispositivo visível para conexão
    if (!bthAdapter.isDiscovering()) {
      Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
      startActivityForResult(getVisible, 0);
      Toast.makeText(getApplicationContext(),"AVAIABLE FOR CONNECTION" ,Toast.LENGTH_LONG).show();
    }
    else{
      Toast.makeText(getApplicationContext(),"AVAIABLE FOR CONNECTION",Toast.LENGTH_SHORT).show();
    }
  }

  private final BroadcastReceiver HandleDiscovery = 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);
        Toast.makeText(getApplicationContext(),"DEVICE FOUND! " ,Toast.LENGTH_LONG).show();
        bthDispositivosArea.add("\n  Device: " + device.getName() + ", " + device);
        lv_devicesArea.setAdapter(bthDispositivosArea);

      } else {
        if(BluetoothDevice.ACTION_UUID.equals(action)) {
          BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
          Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
          for (int i=0; i<uuidExtra.length; i++) {
            //out.append("\n  Device: " + device.getName() + ", " + device + ", Service: " + uuidExtra[i].toString());
          }
        } else {
          if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            Toast.makeText(getApplicationContext(),"START DISCOVERY!",Toast.LENGTH_SHORT).show();
          }
        }
      }
    }
  };

  @Override
  public void onDestroy() {
    unregisterReceiver(HandleDiscovery);

    super.onDestroy();
  }
}

LogCat:

12-30 16:33:24.657: E/AndroidRuntime(12794): FATAL EXCEPTION: main
12-30 16:33:24.657: E/AndroidRuntime(12794): Process: com.AMOV.mr.fit, PID: 12794
12-30 16:33:24.657: E/AndroidRuntime(12794): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.bluetooth.device.action.FOUND flg=0x10 (has extras) } in com.AMOV.mr.fit.DesafioActivity$1@41a98b68
12-30 16:33:24.657: E/AndroidRuntime(12794):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:776)
12-30 16:33:24.657: E/AndroidRuntime(12794):    at android.os.Handler.handleCallback(Handler.java:733)
12-30 16:33:24.657: E/AndroidRuntime(12794):    at android.os.Handler.dispatchMessage(Handler.java:95)
12-30 16:33:24.657: E/AndroidRuntime(12794):    at android.os.Looper.loop(Looper.java:136)
12-30 16:33:24.657: E/AndroidRuntime(12794):    at android.app.ActivityThread.main(ActivityThread.java:5146)
12-30 16:33:24.657: E/AndroidRuntime(12794):    at java.lang.reflect.Method.invokeNative(Native Method)
12-30 16:33:24.657: E/AndroidRuntime(12794):    at java.lang.reflect.Method.invoke(Method.java:515)
12-30 16:33:24.657: E/AndroidRuntime(12794):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
12-30 16:33:24.657: E/AndroidRuntime(12794):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
12-30 16:33:24.657: E/AndroidRuntime(12794):    at dalvik.system.NativeStart.main(Native Method)
12-30 16:33:24.657: E/AndroidRuntime(12794): Caused by: java.lang.NullPointerException
12-30 16:33:24.657: E/AndroidRuntime(12794):    at com.AMOV.mr.fit.DesafioActivity$1.onReceive(DesafioActivity.java:112)
12-30 16:33:24.657: E/AndroidRuntime(12794):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:766)
12-30 16:33:24.657: E/AndroidRuntime(12794):    ... 9 more

回答1:


bthDispositivosArea, your ArrayAdapter is null because you haven't initialized it anywhere. And you're trying to add something to a null object.



来源:https://stackoverflow.com/questions/27709194/error-receiving-broadcast-intent-act-android-bluetooth-device-action-found-flg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!