Java 蓝牙发现接收广播错误-通过startDiscovery()方法接收意图的onReceive()方法出错

Java 蓝牙发现接收广播错误-通过startDiscovery()方法接收意图的onReceive()方法出错,java,android,eclipse,bluetooth,Java,Android,Eclipse,Bluetooth,我在设备扫描活动中遇到问题。我想扫描附近的所有蓝牙设备,并在UI上打印它们。但是我得到了与中相同的运行时错误 公共类MainActivity扩展了ActionBarActivity{ 蓝牙适配器mBluetoothAdapter; 专用int请求\u启用\u BT=1000; public ArrayList deviceArrayList=new ArrayList(); public ArrayList deviceList=new ArrayList(); @凌驾 创建时受保护的void(

我在设备扫描活动中遇到问题。我想
扫描附近的所有
蓝牙设备
,并在UI上打印它们。但是我得到了与中相同的运行时错误

公共类MainActivity扩展了ActionBarActivity{
蓝牙适配器mBluetoothAdapter;
专用int请求\u启用\u BT=1000;
public ArrayList deviceArrayList=new ArrayList();
public ArrayList deviceList=new ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@凌驾
受保护的void onResume(){
//TODO自动生成的方法存根
super.onResume();
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter==null){
//设备不支持蓝牙
Toast.makeText(此“设备不支持蓝牙!!!”,Toast.LENGTH\u LONG.show();
回来
}
如果(!mBluetoothAdapter.isEnabled()){
Intent enablebintent=新意图(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(启用BTIntent、请求\启用\ BT);
}
扫描开始();
}
public void scanStart(){
//TODO自动生成的方法存根
//将配对设备添加到ArrayList
addPairedDevices();
if(mBluetoothAdapter.isDiscovering()){
mBluetoothAdapter.cancelDiscovery();
}
if(mBluetoothAdapter.startDiscovery()==true){
Toast.makeText(这是“搜索…”,Toast.LENGTH_LONG.show();
}
否则{
Toast.makeText(此“蓝牙适配器获得空值”,Toast.LENGTH_SHORT.show();
}
//注册广播接收器
IntentFilter筛选器=新的IntentFilter(BluetoothDevice.ACTION\u已找到);
this.registerReceiver(RequestReceiver,filter);//不要忘记在onDestroy期间取消注册
试一试{
睡眠(12000);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//在UI上打印设备列表
ListView lv=新的ListView(本);
ArrayAdapter listAdapter=新的ArrayAdapter(这个,R.layout.print\u device\u列表,deviceArrayList);
lv.设置适配器(列表适配器);
setContentView(lv);
if(deviceArrayList.size()0){
//循环通过配对设备
用于(蓝牙设备:pairedDevices){
//将名称和地址添加到要在列表视图中显示的阵列适配器
deviceArrayList.add(device.getName()+“\n”+device.getAddress()+“\n”+device.getUuids()[0].getUuid());
添加(设备);
}
}
}
private BroadcastReceiver RequestReceiver=新的BroadcastReceiver(){
公共void onReceive(上下文、意图){
if(intent==null){
回来
}
String action=intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(ACTION)){
日志d(“BT”,“发现设备”);
BluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA\u设备);
如果(设备!=null){
if(device.getName()!=null&&device.getName().length()>0){
deviceArrayList.add(device.getName()+“\n”+device.getAddress()+“\n”+device.getUuids()[0].getUuid());
添加(设备);
}
}
}
};
};
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
//TODO自动生成的方法存根
super.onActivityResult(请求代码、结果代码、数据);
if(requestCode==请求启用){
if(resultCode==Activity.RESULT\u已取消){
Toast.makeText(这是“Uesr不想打开蓝牙!!!”,Toast.LENGTH\u LONG.show();
回来
}
}
}
@凌驾
受保护的空onDestroy(){
//TODO自动生成的方法存根
super.ondestory();
if(mBluetoothAdapter!=null){
mBluetoothAdapter.cancelDiscovery();
}
if(requestreceiver!=null)
未注册接收人(请求接收人);
Toast.makeText(此“蓝牙应用程序已停止”,Toast.LENGTH_LONG.show();
}
}
device.getUuids()
-这里有一个空值,这就是为什么这里有一个错误。
请在使用前检查此变量。

将错误日志张贴在此处。Thanx可获得回复。。。错误日志与此帖子相同-
public class MainActivity extends ActionBarActivity {

    BluetoothAdapter mBluetoothAdapter;
    private int REQUEST_ENABLE_BT = 1000;
    public ArrayList<String> deviceArrayList = new ArrayList<>();
    public ArrayList<BluetoothDevice> deviceList = new ArrayList<>();

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

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            // Device does not support Bluetooth
            Toast.makeText(this, "Device Doesn't Support Bluetooth!!!", Toast.LENGTH_LONG).show();
            return;
        }
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT );
        }
        scanStart();
    }

    public void scanStart() {
        // TODO Auto-generated method stub

        //Adding Paired Devices into ArrayList
        addPairedDevices();

        if(mBluetoothAdapter.isDiscovering()){
            mBluetoothAdapter.cancelDiscovery();
        }
        if(mBluetoothAdapter.startDiscovery() == true){
            Toast.makeText(this, "Searching...", Toast.LENGTH_LONG).show();
        }
        else{
            Toast.makeText(this, "Bluetooth Adapter got Null Value", Toast.LENGTH_SHORT).show();
        }

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

        try {
            Thread.sleep(12000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //Printing Device List on UI
        ListView lv = new ListView(this);
        ArrayAdapter<String> listAdapter = new ArrayAdapter<>(this, R.layout.print_device_list, deviceArrayList);
        lv.setAdapter(listAdapter);
        setContentView(lv);
        if(deviceArrayList.size()<=0){
            Toast.makeText(this, "No Devices Found...", Toast.LENGTH_LONG).show();
        }
    }


    public void addPairedDevices() {
        // TODO Auto-generated method stub
        // get paired devices
        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        // If there are paired devices
        if (pairedDevices.size() > 0) {
            // Loop through paired devices
            for (BluetoothDevice device : pairedDevices) {
                // Add the name and address to an array adapter to show in a ListView
                deviceArrayList.add(device.getName() + "\n" + device.getAddress() + "\n" + device.getUuids()[0].getUuid());
                deviceList.add(device);
            }
        }
    }


    private BroadcastReceiver requestReciever = new BroadcastReceiver(){

        public void onReceive(Context context, Intent intent) {
            if (intent == null) {
                return;
            }
            String action = intent.getAction();

            if(BluetoothDevice.ACTION_FOUND.equals(action)){
                Log.d("BT", "Device Found");
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                if(device!=null){
                    if(device.getName() != null && device.getName().length() > 0){
                        deviceArrayList.add(device.getName()+"\n"+device.getAddress()+"\n"+device.getUuids()[0].getUuid());
                        deviceList.add(device);
                    }
                }
            }
        };
    };


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == REQUEST_ENABLE_BT){
            if(resultCode == Activity.RESULT_CANCELED){
                Toast.makeText(this, "Uesr Doesn't want to switch on Bluetooth!!!", Toast.LENGTH_LONG).show();
                return;
            }
        }
    }



    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if(mBluetoothAdapter != null){
            mBluetoothAdapter.cancelDiscovery();
        }
        if(requestReciever!=null)
            unregisterReceiver(requestReciever);
        Toast.makeText(this, "Bluetooth App Stopped", Toast.LENGTH_LONG).show();
    }
}