2

我正在编写一个 android 程序来与 BLUETOOTH 硬件配对和通信。我可以打开和关闭蓝牙设备......但我无法搜索范围内的蓝牙设备并存储或显示(我是 android 和 java 新手)。我无法理解 developer.android.com 中的代码。所以我在这里发帖...请帮助..谢谢你。

这是我的代码..

public void onToggleClicked(View view) {
        if (!((Switch) findViewById(R.id.switch1)).isChecked()) {
            mBluetoothAdapter.disable();
        } else {
            Intent enableBtIntent = new Intent(
                    BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Check which request we're responding to
        if (requestCode == REQUEST_ENABLE_BT) {
            // Make sure the request was successful
            if (resultCode == RESULT_OK) {
                Toast.makeText(this.getBaseContext(),
                        "Bluetooth IS  switched ON now...", Toast.LENGTH_SHORT)
                        .show();
            } else if (resultCode == RESULT_CANCELED) {
                Toast.makeText(this.getBaseContext(),
                        "Bluetooth IS  switched OFF now...", Toast.LENGTH_SHORT)
                        .show();
                ((Switch) findViewById(R.id.switch1)).setChecked(false);
            }
        }
    }
4

0 回答 0