蓝牙发现在ANDROID中无法正常工作

时间:2017-07-24 09:44:03

标签: java android bluetooth

我正在尝试创建一个找到蓝牙设备的应用,然后连接它们并对这些设备造成一些问题。我有自己的java类,我称之为Device,它带到字符串:nameaddress。另外,我有自己的适配器,我称之为DeviceAdapter,它有两个参数:contextlist<Device>。我想要做的是我获取我的应用程序在发现操作后找到的设备的名称和地址,并将这些值放入我的班级。然后我通过列表视图显示这些值,该列表视图具有我使用自己的适配器为设备名称和地址设计的自定义布局。在主要活动中,我有3个按钮:

  1. 启用(启用蓝牙功能)
  2. 列出保税设备(我还没能配对任何设备。)
  3. 查找设备(实现发现操作并在我的应用附近找到蓝牙设备。)
  4. 我遇到第三个问题。问题是,ACTION_DISCOVERY_STARTEDACTION_DISCOVERY_FINISHED正常工作,因为我可以使用if语句检查当前操作并显示Toasts。谈到ACTION_FOUND我试图获取设备的名称和地址。我提出了一些断点,确保程序进入ACTION_FOUND部分。此外,我可以看到device.getName()device.getAddress()返回的内容与null不同。但在取得价值后,程序因为我不知道的原因而崩溃。

    我无法找到这样的情况所以我找不到任何答案。我尽我所能,但没有解决方案。这是代码:

    EDITED

    因为我在我的Device.java公开中制作变量,程序崩溃了。但现在,它并没有进入广播接收器。

    FoundDevicesActivity

    public class FoundDevicesActivity extends AppCompatActivity {
    BluetoothAdapter bluetoothAdapter;
    ListView foundedDevicesListView;
    DeviceAdapter deviceAdapter;
    List<Device> foundedDevicesList;
    ProgressBar pb;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_found_devices);
    
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        foundedDevicesListView = (ListView) findViewById(R.id.foundDevicesListview);
        pb = new ProgressBar(FoundDevicesActivity.this);
        pb.setEnabled(false);
    
        if (bluetoothAdapter.isDiscovering()) {
            bluetoothAdapter.cancelDiscovery();
        }
    
        bluetoothAdapter.startDiscovery();
    
        IntentFilter filter = new IntentFilter();
    
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    
        registerReceiver(receiver, filter);
    }
    
    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
                pb.setIndeterminate(true);
                Toast.makeText(FoundDevicesActivity.this, "Discovery is started !", Toast.LENGTH_SHORT).show();
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                pb.setVisibility(View.GONE);
                Toast.makeText(FoundDevicesActivity.this, "Discovery is done successfully !", Toast.LENGTH_SHORT).show();
            } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                Device dvc = new Device();
                if (device.getName().equals(null)) {
                    dvc.name = "UNDEFINED NAME";
                } else {
                    dvc.name = device.getName();
                }
    
                if (device.getAddress().equals(null)) {
                    dvc.address = "UNDEFINED ADDRESS";
                } else {
                    dvc.address = device.getAddress();
                }
                foundedDevicesList.add(dvc);
                deviceAdapter = new DeviceAdapter(FoundDevicesActivity.this, foundedDevicesList);
                foundedDevicesListView.setAdapter(deviceAdapter);
                deviceAdapter.notifyDataSetChanged();
            }
        }
    };
    
    @Override
    public void onDestroy() {
        unregisterReceiver(receiver);
        super.onDestroy();
    }
    

    }

    Device.java

    public class Device {
        public String name;
        public String address;
    

    }

    DeviceAdapter.java

    public class DeviceAdapter extends BaseAdapter {
    Context ctx;
    List<Device> DeviceList;
    
    public DeviceAdapter(Context context, List<Device> DvcList) {
        ctx = context;
        DeviceList = DvcList;
    }
    @Override
    public int getCount() {
        return DeviceList.size();
    }
    
    @Override
    public Object getItem(int position) {
        return DeviceList.get(position);
    }
    
    @Override
    public long getItemId(int position) {
        return 0;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Device Dvc = DeviceList.get(position);
        final View gorunum = LayoutInflater.from(ctx).inflate(R.layout.devices_listview_layout, null);
        TextView deviceAddress = (TextView) gorunum.findViewById(R.id.deviceAddress);
        deviceAddress.setText(Dvc.address);
        TextView deviceName = (TextView) gorunum.findViewById(R.id.deviceName);
        deviceName.setText(Dvc.name);
        return gorunum;
    }
    

    }

    有什么想法吗?

1 个答案:

答案 0 :(得分:0)

此字符串错误并导致代码崩溃:  if(device.getName().equals(null))if(device.getAddress().equals(null)) 因为如果device为null,它不理解.getName()和.getAddress()方法,如果方法.getName()返回null,则表示null.equals(null),但在这种情况下null不知道方法.equals();,所以有意义的是将你的字符串更改为: if(device != null && ("null").equals(""+device.getName()))和。{ if(device != null && ("null").equals(""+device.getAddress())) 显然,如果在其他检查之前设备== null,则需要处理这种情况。