无法在android线程中

时间:2016-04-13 09:04:28

标签: android bluetooth

我在一个帖子中使用广播接收器,但我不知道为什么我不能使用registerReciever方法,我得到了这个错误"无法解析方法寄存器接收器&# 34;那我该怎么办? 我正在使用这个线程来扫描蓝牙设备

package com.example.fatma.listviewcst;

import android.app.Application;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * Created by FATMA on 06/04/2016.
 */
  class ConnectedThread extends Thread{
    ArrayList<BluetoothDevice> mArrayAdapter;
    BroadcastReceiver mReceiver;
    public ArrayList<BluetoothDevice> sendList(){
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mArrayAdapter=new ArrayList<BluetoothDevice>();
        if (bluetoothAdapter == null) {
            Toast t=null;
            t.setText("Sorry your phone do not support Bluetooth");
            t.show();
        } else {

            //   bluetoothDev=new BluetoothDev();

            //added
            // Create a BroadcastReceiver for ACTION_FOUND
            if(bluetoothAdapter.isEnabled()){
                bluetoothAdapter.startDiscovery();
               mReceiver= new BroadcastReceiver() {


                    public void onReceive(Context context, Intent intent) {
                        String action = intent.getAction();
                        // When discovery finds a device
                        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                            // Get the BluetoothDevice object from the Intent
                            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                            // Add the name and address to an array adapter to show in a ListView
                            mArrayAdapter.add(device);


                        }
                        for (int i=0;i<mArrayAdapter.size();i++){
                            Log.w("devv ", mArrayAdapter.get(i).getName().toString().toString());
                        }

                    }

                };
                // Register the BroadcastReceiver
            IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);

             //   ContextWrapper cw = new ContextWrapper();
       mReceiver.registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy

                if(mArrayAdapter.size()==0)
                   Log.w("error", "not found");
                else
                {
                    Log.w("valid", " found");
                }



               }
        }
        // Register the BroadcastReceiver
      //  IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
       // registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
        return mArrayAdapter;

    }

    @Override
    public void run() {
        super.run();
        while(mArrayAdapter.size()==0){
            this.sendList();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

registerReceiver需要一个上下文

将上下文从活动传递到ConnectedThread类,然后使用

context.registerReceiver(mReceiver, filter);