接收广播Intent时出错{act = android.bluetooth.device.action.FOUND flg = 0x10}

我搜索了,没有发现任何类似的东西.我正在使用Android中的蓝牙开发两个设备之间的连接.在ACTION_FOUND方法上,当我尝试使设备信息出现在listView上时,我在logcat上收到了该错误.

DesafioActivity:
     公共类DesafioActivity扩展了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

解决方法:

bthDispositivosArea,您的ArrayAdapter为null,因为您尚未在任何地方对其进行初始化.您正在尝试向空对象添加内容.

上一篇:C#:确定蓝牙适配器是否已打开/关闭(以编程方式)使用的堆栈类型


下一篇:java-在近距离1手机和2平板电脑之间通信的最有效方法?