Android Studio - 搜索蓝牙设备

时间:2018-03-26 22:37:44

标签: android android-studio bluetooth

我想编写一个应用程序,可以搜索蓝牙设备,将已创建的设备添加到ListView,连接其中一个设备并发送一些文本。但现在我无法搜索设备。当我忽略日志时,我注意到有时搜索工作。但即使它工作也没有添加到ListView。

这很重要 - 我有Android 4.2和5.1的设备。 有人能帮我找到我的代码中的错误吗?

这是我的代码:

public class MainActivity extends Activity {

    Button btnSearch;
    Button btnConnect;
    Button btnSend;
    ListView lvDeviceList;
    TextView tvOutgoingMessage;
    TextView tvIncomingMessage;

    BluetoothAdapter bluetoothAdapter;
    BroadcastReceiver broadcastReceiver;

    public ArrayList<String> BTDevices = new ArrayList<>();
    public ListAdapter deviceListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "ONCREATE");
        btnSearch = (Button) findViewById(R.id.btnSearch);
        btnConnect = (Button) findViewById(R.id.btnConnect);
        btnSend = (Button) findViewById(R.id.btnSend);
        lvDeviceList = (ListView) findViewById(R.id.devicesList);
        tvOutgoingMessage = (TextView) findViewById(R.id.outgoingMessage);
        tvIncomingMessage = (TextView) findViewById(R.id.incommingMessage);

        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                 if (checkIfEnable()) {
                     Log.d(TAG, "ONCREATE - IF");
                     Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                     discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
                     startActivity(discoverableIntent);

                     discoverDevices();


                     connect();
                 }

            }
        });
    }

    public boolean checkIfEnable() {
        boolean existance = true;
        Log.d(TAG, "CHECK IF ENABLE");
        if (bluetoothAdapter == null) {
            Log.d(TAG, "IN 1ST IF");
            Toast.makeText(getApplicationContext(), "Your device doesn't support Bluetooth", Toast.LENGTH_LONG).show();
            existance = false;
        }
        if (bluetoothAdapter != null && !bluetoothAdapter.isEnabled()) {
            Log.d(TAG, "IN 2ND IF");
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, 1);
        }

        return existance;
    }

    private void discoverDevices() {

        Log.d(TAG, "DISCOVER DEVICES");
        bluetoothAdapter.startDiscovery();
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(broadcastReceiver, filter);



        // Create a BroadcastReceiver for ACTION_FOUND.
        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.d(TAG, "ON RECEIVE");
                String action = intent.getAction();
                if (action.equals(BluetoothDevice.ACTION_FOUND)) {
                    // Discovery has found a device. Get the BluetoothDevice
                    // object and its info from the Intent.
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    String deviceName = device.getName();
                    String deviceHardwareAddress = device.getAddress(); // MAC address
                    BTDevices.add(deviceName);
                    lvDeviceList.setAdapter(deviceListAdapter);
                    Log.d(TAG, "onReceive: " + device.getName() + ": " + device.getAddress());
                }
            }
        };



    }

    private void connect() {
        Log.d(TAG, "CONNECT");
    }

提前致谢。

1 个答案:

答案 0 :(得分:0)

首先,您不应在找到的每台设备上拨打lvDeviceList.setAdapter(deviceListAdapter)。将此行移至onCreate()。第二个 - 您应该将BTDevices传递给您的适配器(这部分很重要,而且代码中缺少这部分),并且在每个BTDevices.add(deviceName)上,您应该在适配器上调用notifyDataSetChanged()