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

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

问题描述

我是Android的编程新手,我在设备扫描活动中面临一个问题。我想在附近的扫描所有蓝牙设备,并在UI上打印。但是,与接收蓝牙发现的广播错误。请帮助我调试我的代码。

  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自动生成的方法存根
super.onResume();
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter == null){
//设备不支持蓝牙
Toast.makeText(这个,设备不支持蓝牙!!!,Toast.LENGTH_LONG)。显示();
return;
}
if(!mBluetoothAdapter.isEnabled()){
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent,REQUEST_ENABLE_BT);
}
scanStart();
}

public void scanStart(){
// TODO自动生成的方法存根

//将配对设备添加到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();
}

//注册BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(requestReciever,filter); //在onDestroy中不要忘记取消注册

try {
Thread.sleep(12000);
} catch(InterruptedException e){
// TODO自动生成的catch块
e.printStackTrace();
}

//在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自动生成的方法存根
//获取配对设备
设置< BluetoothDevice> pairsDevices = mBluetoothAdapter.getBondedDevices();
//如果配对设备
if(pairsDevices.size()> 0){
//循环配对设备
(BluetoothDevice设备:pairsDevices){
//将名称和地址添加到数组适配器中以在ListView中显示
deviceArrayList.add(device.getName()+\\\
+ device.getAddress()+\\\
+ device.getUuids()[0] .getUuid());
deviceList.add(device);
}
}
}


private BroadcastReceiver requestReciever = new BroadcastReceiver(){

public void onReceive(Context context ,意图意图){
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。添加(device.getName()+ \\\
+ device.getAddress()+ \\\
+ device.getUuids()[0] .getUuid());
deviceList.add(device);
}
}
}
};
};


@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
// TODO自动生成的方法stub
super。 onActivityResult(requestCode,resultCode,data);

if(requestCode == REQUEST_ENABLE_BT){
if(resultCode == Activity.RESULT_CANCELED){
Toast.makeText(this,Uesr不想打开蓝牙!!!,Toast.LENGTH_LONG).show();
return;
}
}
}



@Override
protected void onDestroy(){
// TODO自动生成的方法stub
super.onDestroy();
if(mBluetoothAdapter!= null){
mBluetoothAdapter.cancelDiscovery();
}
if(requestReciever!= null)
unregisterReceiver(requestReciever);
Toast.makeText(this,Bluetooth App Stopped,Toast.LENGTH_LONG).show();
}
}


解决方案

code> device.getUuids() - 这里你有一个Null,这就是为什么你在这里有一个错误。
检查这个变量是否使用它。


Hi I am new to android programming and 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. Please, please please help me to debug my code.

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() - here you have a Null, that is why you have an error here. Check this variable beforе using it.

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

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