我在设备扫描活动中遇到问题。我想要scan
附近的所有bluetooth devices
并在UI上打印它们。但我得到的运行时错误与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();
}
}
答案 0 :(得分:0)
device.getUuids()
- 这里你有一个Null,这就是你在这里有错误的原因。
请使用它来检查此变量。