接收蓝牙发现的广播错误-通过startDiscovery()方法接收意图的onReceive()方法中的错误 [英] Receiving broadcast error for Bluetooth discovery - Error in onReceive() method that receives intents by startDiscovery() Method

查看:501
本文介绍了接收蓝牙发现的广播错误-通过startDiscovery()方法接收意图的onReceive()方法中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设备扫描活动中遇到问题.我想在附近所有scan scan并将它们打印在UI上.但是我遇到了与接收到蓝牙发现的广播错误相同的运行时错误接收广播错误以进行蓝牙发现.

I am facing a problem in device scan activity. I want to scan all bluetooth devices in vicinity and print them on UI. But i am getting the same run time error as in Receiving broadcast error for Bluetooth discovery.

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();
    }
}

推荐答案

device.getUuids()-这里有一个Null,这就是为什么这里有错误. 在使用该变量之前,请先对其进行检查.

device.getUuids() - here you have a Null, that is why you have an error here. Check this variable beforе using it.

这篇关于接收蓝牙发现的广播错误-通过startDiscovery()方法接收意图的onReceive()方法中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆